<commit_before><?php

namespace AppBundle\Facade;

use AppBundle\Entity\Category;
use AppBundle\Repository\ProductRepository;
use \Helpers\Parameter\FilterType;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class ProductFacade {

	private $productRepository;

	public function __construct(ProductRepository $productRepository) {
		$this->productRepository = $productRepository;
	}

	public function findByCategory(Category $category, $limit, $offset) {
		return $this->productRepository->findByCategory($category)
			->setFirstResult($offset)
			->setMaxResults($limit)
			->getQuery()->getResult();
	}
	
	public function findByCategoryAndFilter(Category $category, $filter, $limit, $offset) {
		$query =  $this->productRepository->findByCategory($category);		
		$query = $this->createFilterCondition($filter, $query);
		
		$query->setFirstResult($offset)
			->setMaxResults($limit)
			->groupBy('p');
		return $query->getQuery()->getResult();
	}	

	public function getCountByCategory(Category $category, $filter) {
		$query =  $this->productRepository->findByCategory($category)
			->select('COUNT(p.id)');
		$query = $this->createFilterCondition($filter, $query);
		return $query->getQuery()->getSingleScalarResult();
	}

	public function getBySlug($slug) {
		return $this->productRepository->findOneBy([
			"slug" => $slug,
		]);
	}

	public function getAll($limit, $offset) {
		return $this->productRepository->findBy(
			[],
			[
				"rank" => "desc"
			],
			$limit,
			$offset
		);
	}
	
	public function getAllByFilter($limit, $offset, $filter) {
		if(empty($filter)) {
			return $this->getAll($limit, $offset);
		}
		$query = $this->productRepository->getAll();
		$query = $this->createFilterCondition($filter, $query);	
		
		$query->setFirstResult($offset)
			->setMaxResults($limit);
			
		$query->orderBy('p.rank', 'desc');
		$query->groupBy('p');
		return $query->getQuery()->getResult();
	}
	
	public function countAllProducts($filter) {
		$query = $this->productRepository->getAll();
		$query->select('COUNT(p.id)');
		
		$query = $this->createFilterCondition($filter, $query);
		return $query->getQuery()->getSingleScalarResult();		
	}

	private function createFilterCondition($filter, $query) {
		foreach ($filter as $k => $v) {
			$param = $v['param'];
			$values = $v['values']; 
						
			$query->join('AppBundle\Entity\ProductParameter', 'pp'.$k.'', 'WITH', 'p = pp'.$k.'.product')
			->join('AppBundle\Entity\Parameter', 'param'.$k.'', 'WITH', 'param'.$k.' = pp'.$k.'.parameter');
			
			$query->andWhere('param'.$k.' = :param'.$k)
				->setParameter('param'.$k.'', $param);
			
			$dataType = ucfirst($param->getDataType());
			if($param->getFilterType() == 'multiselect') {
				$values = explode(',', $values);	
				$query->andWhere('pp'.$k.'.value'.$dataType.' IN (:values'.$k.')')
				->setParameter('values'.$k.'', $values);			
			} elseif($param->getFilterType() == 'yesno') {
				$query->andWhere('pp'.$k.'.value'.$dataType.' = (:values'.$k.')')
				->setParameter('values'.$k, (int)$values);			
			} elseif($param->getFilterType() == 'range') {
				$values = explode(',', $values);
				$query->andWhere('pp'.$k.'.value'.$dataType.' >= :min'.$k.' and pp'.$k.'.value'.$dataType.' <= :max'.$k)
				->setParameter('min'.$k, $values[0])
				->setParameter('max'.$k, $values[1]);			
			}	
		}<commit_msg>- uprava parseru pro filtr - uprava metody createFilterCondition()<commit_after><?php

namespace AppBundle\Facade;

use AppBundle\Entity\Category;
use AppBundle\Repository\ProductRepository;
use \Helpers\Parameter\FilterType;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class ProductFacade {

	private $productRepository;

	public function __construct(ProductRepository $productRepository) {
		$this->productRepository = $productRepository;
	}

	public function findByCategory(Category $category, $limit, $offset) {
		return $this->productRepository->findByCategory($category)
			->setFirstResult($offset)
			->setMaxResults($limit)
			->getQuery()->getResult();
	}
	
	public function findByCategoryAndFilter(Category $category, $filter, $limit, $offset) {
		$query =  $this->productRepository->findByCategory($category);		
		$query = $this->createFilterCondition($filter, $query);
		
		$query->setFirstResult($offset)
			->setMaxResults($limit)
			->groupBy('p');
		return $query->getQuery()->getResult();
	}	

	public function getCountByCategory(Category $category, $filter) {
		$query =  $this->productRepository->findByCategory($category)
			->select('COUNT(p.id)');
		$query = $this->createFilterCondition($filter, $query);
		return $query->getQuery()->getSingleScalarResult();
	}

	public function getBySlug($slug) {
		return $this->productRepository->findOneBy([
			"slug" => $slug,
		]);
	}

	public function getAll($limit, $offset) {
		return $this->productRepository->findBy(
			[],
			[
				"rank" => "desc"
			],
			$limit,
			$offset
		);
	}
	
	public function getAllByFilter($limit, $offset, $filter) {
		if(empty($filter)) {
			return $this->getAll($limit, $offset);
		}
		$query = $this->productRepository->getAll();
		$query = $this->createFilterCondition($filter, $query);	
		
		$query->setFirstResult($offset)
			->setMaxResults($limit);
			
		$query->orderBy('p.rank', 'desc');
		$query->groupBy('p');
		return $query->getQuery()->getResult();
	}
	
	public function countAllProducts($filter) {
		$query = $this->productRepository->getAll();
		$query->select('COUNT(p.id)');
		
		$query = $this->createFilterCondition($filter, $query);
		return $query->getQuery()->getSingleScalarResult();		
	}

	private function createFilterCondition($filter, $query) {
		foreach ($filter as $k => $v) {
			$param = $v['param'];
			$values = $v['values']; 

			$query->join('AppBundle\Entity\ProductParameter', 'pp'.$k.'', 'WITH', 'p = pp'.$k.'.product')
			->join('AppBundle\Entity\Parameter', 'param'.$k.'', 'WITH', 'param'.$k.' = pp'.$k.'.parameter');
			
			$query->andWhere('param'.$k.' = :param'.$k)
				->setParameter('param'.$k.'', $param);
			
			$dataType = ucfirst($param->getDataType());
			if($param->getFilterType() == 'multiselect') {	
				$query->andWhere('pp'.$k.'.value'.$dataType.' IN (:values'.$k.')')
				->setParameter('values'.$k.'', $values);			
			} elseif($param->getFilterType() == 'yesno') {
				$query->andWhere('pp'.$k.'.value'.$dataType.' = (:values'.$k.')')
				->setParameter('values'.$k, (int)$values);			
			} elseif($param->getFilterType() == 'range') {
				$query->andWhere('pp'.$k.'.value'.$dataType.' >= :min'.$k.' and pp'.$k.'.value'.$dataType.' <= :max'.$k)
				->setParameter('min'.$k, $values[0])
				->setParameter('max'.$k, $values[1]);			
			}	
		}
{'fraction_non_alphanumeric': 0.21574170510340723, 'fraction_numerical': 0.0005951495313197441, 'mean_word_length': 4.48734693877551, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3504646', 'n_tokens_mistral': 2473, 'n_tokens_neox': 2233, 'n_words': 355}