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

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