/**
  * @param PagingContext $context
  * @param PagingProperties $pagingConfiguration
  * @return array
  */
 private function reconstructQueryParams(PagingContext $context, PagingProperties $pagingConfiguration)
 {
     $originalQueryParams = $context->getQuery();
     $reconstructedParams = array();
     foreach ($pagingConfiguration->toArray() as $key => $value) {
         if (array_key_exists($key, $originalQueryParams)) {
             $reconstructedParams[$key] = sprintf('%s=%s', $key, $value);
         }
     }
     if (!array_key_exists('limit', $reconstructedParams)) {
         $reconstructedParams['limit'] = sprintf('limit=%s', $pagingConfiguration->getLimit());
     }
     return $reconstructedParams;
 }
 /**
  * @test
  */
 public function testCreatePagingLinks()
 {
     $pathToLinkMapping = [[$this->createPageLink(1), self::DUMMY_SERVER . $this->createPageLink(1)], [$this->createPageLink(6), self::DUMMY_SERVER . $this->createPageLink(6)], [$this->createPageLink(4), self::DUMMY_SERVER . $this->createPageLink(4)], [$this->createPageLink(2), self::DUMMY_SERVER . $this->createPageLink(2)]];
     $this->pagingInfo->expects($this->once())->method('getPagingConfig')->will($this->returnValue($this->pagingProperties));
     $this->pagingProvider->expects($this->once())->method('getContext')->will($this->returnValue($this->pagingContext));
     $this->pagingContext->expects($this->once())->method('getQuery')->will($this->returnValue($this->getStubQueryParams()));
     $this->pagingProperties->expects($this->atLeastOnce())->method('toArray')->will($this->returnValue($this->getStubPagingConfig()));
     $this->pagingProperties->expects($this->once())->method('getLimit')->will($this->returnValue(25));
     $this->pagingContext->expects($this->once())->method('getPathInfo')->will($this->returnValue(self::DUMMY_PATHINFO));
     $this->pagingContext->expects($this->atLeastOnce())->method('getUriForPath')->will($this->returnValueMap($pathToLinkMapping));
     $this->pagingInfo->expects($this->once())->method('getFirstPage')->will($this->returnValue(1));
     $this->pagingInfo->expects($this->once())->method('getLastPage')->will($this->returnValue(6));
     $this->pagingInfo->expects($this->once())->method('getNextPage')->will($this->returnValue(4));
     $this->pagingInfo->expects($this->once())->method('getPreviousPage')->will($this->returnValue(2));
     $actualLinks = $this->apiPagingServiceImpl->createPagingLinks($this->pagingInfo);
     $expectedLinks = $this->getLinksString($pathToLinkMapping);
     $this->assertEquals($expectedLinks, $actualLinks);
 }
 /**
  * @param array $conditions
  * @param PagingProperties $pagingProperties
  * @param ApiUser $user
  * @return \Doctrine\Common\Collections\Collection|static
  * @throws \Exception
  */
 public function filter(array &$conditions, PagingProperties $pagingProperties, $user = null)
 {
     $qb = $this->_em->createQueryBuilder();
     $orderByField = sprintf('%s.%s', self::SELECT_ALIAS, $pagingProperties->getSort());
     $offset = ($pagingProperties->getPage() - 1) * $pagingProperties->getLimit();
     $qb->select(self::SELECT_ALIAS)->from($this->_entityName, self::SELECT_ALIAS);
     foreach ($conditions as $condition) {
         $whereExpression = $this->buildWhereExpression($qb, $condition);
         $qb->orWhere($whereExpression);
     }
     if (!$this->userState->isOroUser()) {
         $publicComments = sprintf('%s.private = false', self::SELECT_ALIAS);
         $qb->andWhere($publicComments);
     }
     $qb->addOrderBy($orderByField, $pagingProperties->getOrder());
     $qb->setFirstResult($offset);
     $qb->setMaxResults($pagingProperties->getLimit());
     $query = $qb->getQuery();
     try {
         $result = $query->getResult(Query::HYDRATE_OBJECT);
     } catch (\Exception $e) {
         $result = null;
     }
     return $result;
 }
 /**
  * Sorting the mysql result.
  * Implemented because Doctrine ORM not support select from subQuery
  *
  * @param $result
  * @param PagingProperties $pagingProperties
  * @return array
  */
 public function sortByKey($result, PagingProperties $pagingProperties)
 {
     if (!$result || empty($result)) {
         return $result;
     }
     usort($result, function ($a, $b) use($pagingProperties) {
         $reflectionA = new \ReflectionClass($a);
         $reflectionB = new \ReflectionClass($b);
         $sortBy = $pagingProperties->getSort();
         $orderBy = $pagingProperties->getOrder();
         if (!$reflectionA->getProperty($sortBy) || !$reflectionB->getProperty($sortBy)) {
             return 0;
         }
         $propertyA = $reflectionA->getProperty($sortBy);
         $propertyB = $reflectionB->getProperty($sortBy);
         $propertyA->setAccessible(true);
         $propertyB->setAccessible(true);
         $valueA = (string) $a->getKey();
         $valueB = (string) $b->getKey();
         if (is_string($valueA) || is_string($valueB)) {
             $sortableArray = array($valueA, $valueB);
             $originalSortableArray = $sortableArray;
             asort($sortableArray);
             if ($orderBy == 'desc') {
                 return $sortableArray !== $originalSortableArray;
             } else {
                 return $sortableArray === $originalSortableArray;
             }
         }
         return 0;
     });
     return $result;
 }
 /**
  * @param array $conditions
  * @param PagingProperties $pagingProperties
  * @return \Doctrine\ORM\QueryBuilder
  */
 protected function createFilterQuery(array $conditions, PagingProperties $pagingProperties)
 {
     $qb = $this->_em->createQueryBuilder();
     $orderByField = sprintf('%s.%s', self::SELECT_ALIAS, $pagingProperties->getSort());
     $offset = ($pagingProperties->getPage() - 1) * $pagingProperties->getLimit();
     $qb->select(self::SELECT_ALIAS)->from($this->_entityName, self::SELECT_ALIAS);
     foreach ($conditions as $condition) {
         $whereExpression = $this->buildWhereExpression($qb, $condition);
         $qb->andWhere($whereExpression);
     }
     $qb->addOrderBy($orderByField, $pagingProperties->getOrder());
     $qb->setFirstResult($offset);
     $qb->setMaxResults($pagingProperties->getLimit());
     return $qb;
 }
 /**
  * @param array $conditions
  * @param PagingProperties $pagingProperties
  * @return \Doctrine\ORM\QueryBuilder
  */
 protected function orderByTicketKey(array $conditions, PagingProperties $pagingProperties)
 {
     $qb = $this->_em->createQueryBuilder();
     $offset = ($pagingProperties->getPage() - 1) * $pagingProperties->getLimit();
     $qb->select(self::SELECT_ALIAS)->addSelect('CONCAT(b.key, \'-\', ' . self::SELECT_ALIAS . '.sequenceNumber) AS HIDDEN ticketKey')->from('DiamanteDeskBundle:Ticket', self::SELECT_ALIAS)->join(self::SELECT_ALIAS . '.branch', 'b');
     foreach ($conditions as $condition) {
         $whereExpression = $this->buildWhereExpression($qb, $condition);
         $qb->andWhere($whereExpression);
     }
     $qb->addOrderBy('ticketKey', $pagingProperties->getOrder());
     $qb->setFirstResult($offset);
     $qb->setMaxResults($pagingProperties->getLimit());
     return $qb;
 }