vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Sortable/ArraySubscriber.php line 49

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Sortable;
  3. use Knp\Component\Pager\Event\ItemsEvent;
  4. use Knp\Component\Pager\PaginatorInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
  8. use Symfony\Component\PropertyAccess\PropertyAccess;
  9. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  10. class ArraySubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var string the field used to sort current object array list
  14.      */
  15.     private $currentSortingField;
  16.     /**
  17.      * @var string the sorting direction
  18.      */
  19.     private $sortDirection;
  20.     /**
  21.      * @var PropertyAccessorInterface|null
  22.      */
  23.     private $propertyAccessor;
  24.     /**
  25.      * @var Request
  26.      */
  27.     private $request;
  28.     public function __construct(Request $request nullPropertyAccessorInterface $accessor null)
  29.     {
  30.         if (!$accessor && class_exists(PropertyAccess::class)) {
  31.             $accessor PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor();
  32.         }
  33.         $this->propertyAccessor $accessor;
  34.         // check needed because $request must be nullable, being the second parameter (with the first one nullable)
  35.         if (null === $request) {
  36.             throw new \InvalidArgumentException('Request must be initialized.');
  37.         }
  38.         $this->request $request;
  39.     }
  40.     public function items(ItemsEvent $event): void
  41.     {
  42.         // Check if the result has already been sorted by an other sort subscriber
  43.         $customPaginationParameters $event->getCustomPaginationParameters();
  44.         if (!empty($customPaginationParameters['sorted']) ) {
  45.             return;
  46.         }
  47.         $sortField $event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME];
  48.         if (!is_array($event->target) || null === $sortField || !$this->request->query->has($sortField)) {
  49.             return;
  50.         }
  51.         $event->setCustomPaginationParameter('sorted'true);
  52.         if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($this->request->query->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
  53.             throw new \UnexpectedValueException("Cannot sort by: [{$this->request->query->get($sortField)}] this field is not in allow list.");
  54.         }
  55.         $sortFunction $event->options['sortFunction'] ?? [$this'proxySortFunction'];
  56.         $sortField $this->request->query->get($sortField);
  57.         // compatibility layer
  58.         if ($sortField[0] === '.') {
  59.             $sortField substr($sortField1);
  60.         }
  61.         call_user_func_array($sortFunction, [
  62.             &$event->target,
  63.             $sortField,
  64.             $this->getSortDirection($event->options),
  65.         ]);
  66.     }
  67.     private function getSortDirection(array $options): string
  68.     {
  69.         if (!$this->request->query->has($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME])) {
  70.             return 'desc';
  71.         }
  72.         $direction $this->request->query->get($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]);
  73.         if (strtolower($direction) === 'asc') {
  74.             return 'asc';
  75.         }
  76.         return 'desc';
  77.     }
  78.     private function proxySortFunction(&$target$sortField$sortDirection): bool
  79.     {
  80.         $this->currentSortingField $sortField;
  81.         $this->sortDirection $sortDirection;
  82.         return usort($target, [$this'sortFunction']);
  83.     }
  84.     /**
  85.      * @param mixed $object1 first object to compare
  86.      * @param mixed $object2 second object to compare
  87.      *
  88.      * @return int
  89.      */
  90.     private function sortFunction($object1$object2): int
  91.     {
  92.         if (null === $this->propertyAccessor) {
  93.             throw new \UnexpectedValueException('You need symfony/property-access component to use this sorting function');
  94.         }
  95.         if (!$this->propertyAccessor->isReadable($object1$this->currentSortingField) || !$this->propertyAccessor->isReadable($object2$this->currentSortingField)) {
  96.             return 0;
  97.         }
  98.         try {
  99.             $fieldValue1 $this->propertyAccessor->getValue($object1$this->currentSortingField);
  100.         } catch (UnexpectedTypeException $e) {
  101.             return -$this->getSortCoefficient();
  102.         }
  103.         try {
  104.             $fieldValue2 $this->propertyAccessor->getValue($object2$this->currentSortingField);
  105.         } catch (UnexpectedTypeException $e) {
  106.             return $this->getSortCoefficient();
  107.         }
  108.         if (is_string($fieldValue1)) {
  109.             $fieldValue1 mb_strtolower($fieldValue1);
  110.         }
  111.         if (is_string($fieldValue2)) {
  112.             $fieldValue2 mb_strtolower($fieldValue2);
  113.         }
  114.         if ($fieldValue1 === $fieldValue2) {
  115.             return 0;
  116.         }
  117.         return ($fieldValue1 $fieldValue2 : -1) * $this->getSortCoefficient();
  118.     }
  119.     private function getSortCoefficient(): int
  120.     {
  121.         return $this->sortDirection === 'asc' : -1;
  122.     }
  123.     public static function getSubscribedEvents(): array
  124.     {
  125.         return [
  126.             'knp_pager.items' => ['items'1],
  127.         ];
  128.     }
  129. }