/**
  * @param Builder       $query
  * @param SortInterface $sort
  */
 public static function sort(Builder $query, SortInterface $sort)
 {
     /** @var Order $order */
     foreach ($sort->orders() as $propertyName => $order) {
         $query->getQuery()->orderBy($propertyName, $order->isAscending() ? 'ASC' : 'DESC');
     }
 }
Exemplo n.º 2
0
 /**
  * @param QueryBuilder  $queryBuilder
  * @param SortInterface $sort
  * @param Mapping       $mapping
  */
 public static function sort(QueryBuilder $queryBuilder, SortInterface $sort, Mapping $mapping)
 {
     $columns = $mapping->map();
     foreach ($sort->orders() as $propertyName => $order) {
         self::guardColumnExists($columns, $propertyName);
         $queryBuilder->orderBy($columns[$propertyName], $order->isAscending() ? Order::ASCENDING : Order::DESCENDING);
     }
 }
Exemplo n.º 3
0
 /**
  * @param array $results
  * @param Sort  $sort
  *
  * @return array
  *
  * @throws \Exception
  */
 public static function sort(array $results, Sort $sort) : array
 {
     $sortOrder = array_reverse($sort->orders(), true);
     /** @var Order $sortDirection */
     foreach ($sortOrder as $propertyName => $sortDirection) {
         if ($sortDirection->isAscending()) {
             self::stableUasort($results, function ($a, $b) use($propertyName) {
                 $value1 = (string) PropertyValue::get($a, $propertyName);
                 $value2 = (string) PropertyValue::get($b, $propertyName);
                 return (int) (strcmp($value1, $value2) >= 0);
             });
         } else {
             self::stableUasort($results, function ($a, $b) use($propertyName) {
                 $value1 = (string) PropertyValue::get($a, $propertyName);
                 $value2 = (string) PropertyValue::get($b, $propertyName);
                 return (int) (strcmp($value1, $value2) < 0);
             });
         }
     }
     return array_values($results);
 }
Exemplo n.º 4
0
 /**
  * @param SortInterface $sort
  *
  * @return bool
  */
 public function equals(SortInterface $sort) : bool
 {
     return $sort->orders() == $this->orders();
 }
 /**
  * {@inheritdoc}
  */
 public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null) : array
 {
     $hashFilter = $filter ? serialize($filter->filters()) : '';
     $hashSort = $sort ? serialize($sort->orders()) : '';
     $hashFields = $distinctFields ? serialize($distinctFields->get()) : '';
     $key = $this->cacheNamespaceFindByDistinct . md5($hashFilter . $hashSort . $hashFields);
     $cachedItem = $this->cache->getItem($key);
     if ($cachedItem->isHit() && null !== ($result = $cachedItem->get())) {
         return (array) $result;
     }
     $result = $this->repository->findByDistinct($distinctFields, $filter, $sort);
     $this->saveToCache($cachedItem, $result);
     return (array) $result;
 }