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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\PropertyAccess\PropertyAccess;
  8. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  9. use Knp\Component\Pager\PaginatorInterface;
  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
  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('Symfony\Component\PropertyAccess\PropertyAccess')) {
  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.         if (!is_array($event->target) || !$this->request->query->has($event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME])) {
  48.             return;
  49.         }
  50.         $event->setCustomPaginationParameter('sorted'true);
  51.         if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST]) && !in_array($this->request->query->get($event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]), $event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
  52.             throw new \UnexpectedValueException("Cannot sort by: [{$this->request->query->get($event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME])}] this field is not in whitelist");
  53.         }
  54.         $sortFunction = isset($event->options['sortFunction']) ? $event->options['sortFunction'] : [$this'proxySortFunction'];
  55.         $sortField $this->request->query->get($event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]);
  56.         // compatibility layer
  57.         if ($sortField[0] === '.') {
  58.             $sortField substr($sortField1);
  59.         }
  60.         call_user_func_array($sortFunction, [
  61.             &$event->target,
  62.             $sortField,
  63.             $this->getSortDirection($event->options)
  64.         ]);
  65.     }
  66.     private function getSortDirection(array $options): string
  67.     {
  68.         if (!$this->request->query->has($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME])) {
  69.             return 'desc';
  70.         }
  71.         $direction $this->request->query->get($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]);
  72.         if (strtolower($direction) === 'asc') {
  73.             return 'asc';
  74.         }
  75.         return 'desc';
  76.     }
  77.     private function proxySortFunction(&$target$sortField$sortDirection): bool
  78.     {
  79.         $this->currentSortingField $sortField;
  80.         $this->sortDirection $sortDirection;
  81.         return usort($target, [$this'sortFunction']);
  82.     }
  83.     /**
  84.      * @param mixed $object1 first object to compare
  85.      * @param mixed $object2 second object to compare
  86.      *
  87.      * @return int
  88.      */
  89.     private function sortFunction($object1$object2): int
  90.     {
  91.         if (!$this->propertyAccessor) {
  92.             throw new \UnexpectedValueException('You need symfony/property-access component to use this sorting function');
  93.         }
  94.         if (!$this->propertyAccessor->isReadable($object1$this->currentSortingField) || !$this->propertyAccessor->isReadable($object2$this->currentSortingField)) {
  95.             return 0;
  96.         }
  97.         try {
  98.             $fieldValue1 $this->propertyAccessor->getValue($object1$this->currentSortingField);
  99.         } catch (UnexpectedTypeException $e) {
  100.             return -$this->getSortCoefficient();
  101.         }
  102.         try {
  103.             $fieldValue2 $this->propertyAccessor->getValue($object2$this->currentSortingField);
  104.         } catch (UnexpectedTypeException $e) {
  105.             return $this->getSortCoefficient();
  106.         }
  107.         if (is_string($fieldValue1)) {
  108.             $fieldValue1 mb_strtolower($fieldValue1);
  109.         }
  110.         if (is_string($fieldValue2)) {
  111.             $fieldValue2 mb_strtolower($fieldValue2);
  112.         }
  113.         if ($fieldValue1 === $fieldValue2) {
  114.             return 0;
  115.         }
  116.         return ($fieldValue1 $fieldValue2 : -1) * $this->getSortCoefficient();
  117.     }
  118.     private function getSortCoefficient(): int
  119.     {
  120.         return $this->sortDirection === 'asc' : -1;
  121.     }
  122.     public static function getSubscribedEvents(): array
  123.     {
  124.         return [
  125.             'knp_pager.items' => ['items'1]
  126.         ];
  127.     }
  128. }