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

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