Skip to content

Commit d8098bd

Browse files
authored
Merge pull request #129 from devgiants/master
- Add event subscriber to allow array to be sortable
2 parents bc49e73 + 2c2d3f5 commit d8098bd

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Knp\Component\Pager\Event\Subscriber\Sortable;
4+
5+
use \ReflectionClass;
6+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7+
use Knp\Component\Pager\Event\ItemsEvent;
8+
use Doctrine\ORM\Query;
9+
10+
class ArraySubscriber implements EventSubscriberInterface
11+
{
12+
13+
/**
14+
* @var string the field used to sort current object array list
15+
*/
16+
private $currentSortingFieldGetter;
17+
18+
/**
19+
* @var string the sorting direction
20+
*/
21+
private $sortDirection;
22+
23+
public function items(ItemsEvent $event)
24+
{
25+
if (is_array($event->target) && count($event->target) > 1)
26+
{
27+
if (isset($_GET[$event->options['sortFieldParameterName']])) {
28+
$this->sortDirection = isset($_GET[$event->options['sortDirectionParameterName']]) && strtolower($_GET[$event->options['sortDirectionParameterName']]) === 'asc' ? 'asc' : 'desc';
29+
30+
// TODO add whitelist
31+
// if (isset($event->options['sortFieldWhitelist'])) {
32+
// if (!in_array($_GET[$event->options['sortFieldParameterName']], $event->options['sortFieldWhitelist'])) {
33+
// throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options['sortFieldParameterName']]}] this field is not in whitelist");
34+
// }
35+
// }
36+
37+
$sortFieldParameterName = explode('.', $_GET[$event->options['sortFieldParameterName']]);
38+
if(isset($sortFieldParameterName[1])) {
39+
// Capitalize first letter in order to prepare getter construction
40+
$sortFieldName = ucfirst($sortFieldParameterName[1]);
41+
42+
$this->currentSortingFieldGetter = "get{$sortFieldName}";
43+
44+
// Getter detection
45+
$class = new ReflectionClass(get_class($event->target[0]));
46+
if($class->hasMethod($this->currentSortingFieldGetter)) {
47+
// Sort
48+
usort($event->target, array($this, "sort" . ucfirst($this->sortDirection)));
49+
}
50+
}
51+
}
52+
}
53+
}
54+
55+
/**
56+
* @param $object1 first object to compare
57+
* @param $object2 second object to compare
58+
*/
59+
private function sortAsc($object1, $object2)
60+
{
61+
$fieldValue1 = strtolower($object1->{$this->currentSortingFieldGetter}());
62+
$fieldValue2 = strtolower($object2->{$this->currentSortingFieldGetter}());
63+
if ($fieldValue1 == $fieldValue2) {
64+
return 0;
65+
}
66+
return ($fieldValue1 > $fieldValue2) ? +1 : -1;
67+
}
68+
69+
/**
70+
* @param $object1 first object to compare
71+
* @param $object2 second object to compare
72+
*/
73+
private function sortDesc($object1, $object2)
74+
{
75+
$fieldValue1 = strtolower($object1->{$this->currentSortingFieldGetter}());
76+
$fieldValue2 = strtolower($object2->{$this->currentSortingFieldGetter}());
77+
if ($fieldValue1 == $fieldValue2) {
78+
return 0;
79+
}
80+
return ($fieldValue1 > $fieldValue2) ? -1 : +1;
81+
}
82+
83+
public static function getSubscribedEvents()
84+
{
85+
return array(
86+
'knp_pager.items' => array('items', 1)
87+
);
88+
}
89+
}

src/Knp/Component/Pager/Event/Subscriber/Sortable/SortableSubscriber.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function before(BeforeEvent $event)
1616
$disp->addSubscriber(new ElasticaQuerySubscriber());
1717
$disp->addSubscriber(new PropelQuerySubscriber());
1818
$disp->addSubscriber(new SolariumQuerySubscriber());
19+
$disp->addSubscriber(new ArraySubscriber());
1920
}
2021

2122
public static function getSubscribedEvents()

0 commit comments

Comments
 (0)