Ejemplo n.º 1
0
 public function execute($key, $value, QueryFilter $queryFilter)
 {
     if (!preg_match(static::$commandRegex, $value, $matches)) {
         return false;
     }
     $data = trim($matches[1]);
     $queryFilter->addCriteria(new Criteria(Criteria::TYPE_CONDITION_EQUAL, $key, $data));
     return true;
 }
Ejemplo n.º 2
0
 public function execute(array $query, QueryFilter $queryFilter)
 {
     if (!isset($query[$this->commandName])) {
         return false;
     }
     $value = $query[$this->commandName];
     $queryFilter->addCriteria(new Criteria(Criteria::TYPE_SPECIAL_LIMIT, null, (int) $value));
     return true;
 }
Ejemplo n.º 3
0
 public function execute($key, $value, QueryFilter $queryFilter)
 {
     $value = explode(',', $value);
     $value = array_map('trim', $value);
     $value = array_filter($value, function ($el) {
         return $el !== '';
     });
     $queryFilter->addCriteria(new Criteria(Criteria::TYPE_CONDITION_IN_ARRAY, $key, $value));
     return true;
 }
Ejemplo n.º 4
0
 public function execute(array $query, QueryFilter $queryFilter)
 {
     if (!isset($query[$this->commandName])) {
         return false;
     }
     $value = $query[$this->commandName];
     $value = explode(',', $value);
     $value = array_map('trim', $value);
     $queryFilter->addCriteria(new Criteria(Criteria::TYPE_SPECIAL_FIELDS, $value, null));
     return true;
 }
Ejemplo n.º 5
0
 public function testVisit_WhenFilterByTypeThatIsNotSupported()
 {
     $queryBuilder = 'can be anything';
     $entityFieldNames = ['id', 'title', 'price'];
     $criteriaMock = $this->getMockBuilder(Criteria::class)->disableOriginalConstructor()->getMock();
     $criteriaMock->expects($this->exactly(2))->method('getType')->willReturn('notSupportedType');
     $criteriaMock->expects($this->once())->method('getKey')->willReturn('title');
     $this->queryFilterMock->expects($this->once())->method('getCriteria')->willReturn([$criteriaMock]);
     $this->commandCollectionMock->expects($this->once())->method('offsetGet')->with('notSupportedType')->willReturn(null);
     $this->setExpectedException('BusinessLogicLibrary\\QueryFilter\\Exception\\UnsupportedTypeException', 'Tried to filter by field "notSupportedType" that is not supported');
     $result = $this->testedObject->visit($queryBuilder, $entityFieldNames);
     $this->assertSame($queryBuilder, $result);
 }
Ejemplo n.º 6
0
 /**
  * @param mixed $queryBuilder
  * @param array $entityFieldNames
  * @return mixed
  * @throws Exception\UnsupportedTypeException
  */
 public function visit($queryBuilder, array $entityFieldNames)
 {
     /** @var $criteria Criteria */
     foreach ($this->queryFilter->getCriteria() as $criteria) {
         $this->checkCriteriaKey($criteria->getKey(), $entityFieldNames);
         /** @var CommandInterface $command */
         if (!($command = $this->commandCollection->offsetGet($criteria->getType()))) {
             throw new Exception\UnsupportedTypeException(sprintf('Tried to filter by field "%s" that is not supported', $criteria->getType()));
         }
         $command->execute($queryBuilder, $criteria);
     }
     return $queryBuilder;
 }
Ejemplo n.º 7
0
 public function execute(array $query, QueryFilter $queryFilter)
 {
     if (!isset($query[$this->commandName])) {
         return false;
     }
     $value = $query[$this->commandName];
     /** @var Criteria $limitCriteria */
     if ($limitCriteria = $queryFilter->getCriteria(Criteria::TYPE_SPECIAL_LIMIT)) {
         $limitValue = $limitCriteria->getValue();
         $offsetValue = $limitValue * ((int) $value - 1);
         $queryFilter->addCriteria(new Criteria(Criteria::TYPE_SPECIAL_OFFSET, null, $offsetValue));
     }
     return true;
 }
Ejemplo n.º 8
0
 public function execute($key, $value, QueryFilter $queryFilter)
 {
     if (!preg_match(static::$commandRegex, $value, $matches)) {
         return false;
     }
     $command = trim($matches[1]);
     $start = trim($matches[2]);
     $end = trim($matches[3]);
     if ($start > $end) {
         list($start, $end) = [$end, $start];
     }
     $queryFilter->addCriteria(new Criteria($command, $key, [$start, $end]));
     return true;
 }
Ejemplo n.º 9
0
 private function setOrderBy(QueryFilter $queryFilter, $value)
 {
     $value = explode(',', $value);
     $value = array_map('trim', $value);
     $value = array_filter($value, function ($el) {
         return $el !== '';
     });
     foreach ($value as $sortColumn) {
         $order = 'asc';
         if ($sortColumn[0] === '-') {
             $sortColumn = substr($sortColumn, 1);
             $order = 'desc';
         }
         $queryFilter->addCriteria(new Criteria(Criteria::TYPE_SPECIAL_SORT, $sortColumn, $order));
     }
 }
Ejemplo n.º 10
0
 public function indexAction()
 {
     try {
         $filterParams = $this->paginatorInfoFactory->prepareFilterParams($this->params()->fromQuery());
         $this->queryFilter->setQueryParameters($filterParams);
         /** @var PaginatorAdapter $paginator */
         $paginator = $this->service->getFilteredResults($this->queryFilter);
         $paginatorInfo = $this->paginatorInfoFactory->create($paginator->count());
         $paginatorInfo->preparePagesToShow();
         return ['books' => $paginator->getIterator(), 'paginator' => $paginatorInfo, 'route' => 'library/books/*', 'query' => $filterParams];
     } catch (QueryFilterException $e) {
         $this->flashMessenger()->addErrorMessage($e->getMessage());
     } catch (PaginationException $e) {
         $this->flashMessenger()->addErrorMessage($e->getMessage());
     }
     return array('books' => array());
 }
Ejemplo n.º 11
0
 public function testSetQueryParametersWithAllPossibleOptions()
 {
     $data = ['$fields' => 'id,author,title', '$sort' => '-year,price', '$limit' => '5', '$page' => '1', 'year' => '$between(2014,2005)', 'price' => ['$min(20)', '$max(50)'], 'name' => ['$startswith("a")', '$endswith("z")'], 'status' => 'packaging,shipping', 'author' => '"Robert C. Marting"'];
     $this->testedObject->setQueryParameters($data);
     $resultCriteria = $this->testedObject->getCriteria();
     $this->assertCount(12, $resultCriteria);
     $this->assertNotNull($this->testedObject->getCriteria(Criteria::TYPE_SPECIAL_LIMIT));
     $this->assertNotNull($this->testedObject->getCriteria(Criteria::TYPE_SPECIAL_OFFSET));
 }
Ejemplo n.º 12
0
 public function indexAction()
 {
     try {
         $this->queryFilter->setQueryParameters($this->paginatorInfoFactory->prepareFilterParams($this->params()->fromQuery()));
         /** @var PaginatorAdapter $paginator */
         $paginator = $this->service->getFilteredResults($this->queryFilter, $hydrationMode = Query::HYDRATE_ARRAY);
         $paginatorInfo = $this->paginatorInfoFactory->create($paginator->count());
         $data = array('data' => $paginator->getIterator());
         if ($paginatorInfo->shouldDisplay()) {
             $data['pagination'] = $paginatorInfo->toArray();
         }
         return new JsonModel($data);
     } catch (QueryFilterException $e) {
         throw new Exception\BadRequestException($e->getMessage(), Response::STATUS_CODE_400, $e);
     } catch (PaginationException $e) {
         throw new Exception\BadRequestException($e->getMessage(), Response::STATUS_CODE_400, $e);
     } catch (\PDOException $e) {
         throw new Exception\PDOServiceUnavailableException();
     }
 }