Skip to content

Commit 9e8df46

Browse files
authored
Merge pull request #192 from KnpLabs/fix/elastic-sort
Fix Elastica & Solarium sort
2 parents b513b79 + bf57220 commit 9e8df46

File tree

9 files changed

+75
-10
lines changed

9 files changed

+75
-10
lines changed

src/Knp/Component/Pager/Event/ItemsEvent.php

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public function getCustomPaginationParameters()
5757
return $this->customPaginationParams;
5858
}
5959

60+
public function unsetCustomPaginationParameter($name)
61+
{
62+
if (isset($this->customPaginationParams[$name])) {
63+
unset($this->customPaginationParams[$name]);
64+
}
65+
}
66+
6067
public function getLimit()
6168
{
6269
return $this->limit;

src/Knp/Component/Pager/Event/Subscriber/Paginate/ElasticaQuerySubscriber.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ public function items(ItemsEvent $event)
1919
list($searchable, $query) = $event->target;
2020

2121
$query->setFrom($event->getOffset());
22-
$query->setLimit($event->getLimit());
22+
$query->setSize($event->getLimit());
2323
$results = $searchable->search($query);
2424

2525
$event->count = $results->getTotalHits();
26-
//Faceting is being replaced by aggregations
26+
2727
if ($results->hasAggregations()) {
2828
$event->setCustomPaginationParameter('aggregations', $results->getAggregations());
29-
} elseif ($results->hasFacets()) {
30-
$event->setCustomPaginationParameter('facets', $results->getFacets());
3129
}
30+
3231
$event->setCustomPaginationParameter('resultSet', $results);
3332
$event->items = $results->getResults();
3433
$event->stopPropagation();

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

+8
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,18 @@ public function __construct(PropertyAccessorInterface $accessor = null)
3636

3737
public function items(ItemsEvent $event)
3838
{
39+
// Check if the result has already been sorted by an other sort subscriber
40+
$customPaginationParameters = $event->getCustomPaginationParameters();
41+
if (!empty($customPaginationParameters['sorted']) ) {
42+
return;
43+
}
44+
3945
if (!is_array($event->target) || empty($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
4046
return;
4147
}
4248

49+
$event->setCustomPaginationParameter('sorted', true);
50+
4351
if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST]) && !in_array($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]], $event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
4452
throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]]}] this field is not in whitelist");
4553
}

src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ODM/MongoDB/QuerySubscriber.php

+8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ class QuerySubscriber implements EventSubscriberInterface
1111
{
1212
public function items(ItemsEvent $event)
1313
{
14+
// Check if the result has already been sorted by an other sort subscriber
15+
$customPaginationParameters = $event->getCustomPaginationParameters();
16+
if (!empty($customPaginationParameters['sorted']) ) {
17+
return;
18+
}
19+
1420
if ($event->target instanceof Query) {
21+
$event->setCustomPaginationParameter('sorted', true);
22+
1523
if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
1624
$field = $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
1725
$dir = strtolower($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) == 'asc' ? 1 : -1;

src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ORM/QuerySubscriber.php

+8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ class QuerySubscriber implements EventSubscriberInterface
1313
{
1414
public function items(ItemsEvent $event)
1515
{
16+
// Check if the result has already been sorted by an other sort subscriber
17+
$customPaginationParameters = $event->getCustomPaginationParameters();
18+
if (!empty($customPaginationParameters['sorted']) ) {
19+
return;
20+
}
21+
1622
if ($event->target instanceof Query) {
23+
$event->setCustomPaginationParameter('sorted', true);
24+
1725
if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
1826
$dir = isset($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) && strtolower($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) === 'asc' ? 'asc' : 'desc';
1927

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

+8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ class ElasticaQuerySubscriber implements EventSubscriberInterface
1212
{
1313
public function items(ItemsEvent $event)
1414
{
15+
// Check if the result has already been sorted by an other sort subscriber
16+
$customPaginationParameters = $event->getCustomPaginationParameters();
17+
if (!empty($customPaginationParameters['sorted']) ) {
18+
return;
19+
}
20+
1521
if (is_array($event->target) && 2 === count($event->target) && reset($event->target) instanceof SearchableInterface && end($event->target) instanceof Query) {
22+
$event->setCustomPaginationParameter('sorted', true);
23+
1624
list($searchable, $query) = $event->target;
1725

1826
if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {

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

+8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ class PropelQuerySubscriber implements EventSubscriberInterface
1010
{
1111
public function items(ItemsEvent $event)
1212
{
13+
// Check if the result has already been sorted by an other sort subscriber
14+
$customPaginationParameters = $event->getCustomPaginationParameters();
15+
if (!empty($customPaginationParameters['sorted']) ) {
16+
return;
17+
}
18+
1319
$query = $event->target;
1420
if ($query instanceof \ModelCriteria) {
21+
$event->setCustomPaginationParameter('sorted', true);
22+
1523
if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
1624
$part = $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
1725
$directionParam = $event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME];

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

+8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ class SolariumQuerySubscriber implements EventSubscriberInterface
1515
{
1616
public function items(ItemsEvent $event)
1717
{
18+
// Check if the result has already been sorted by an other sort subscriber
19+
$customPaginationParameters = $event->getCustomPaginationParameters();
20+
if (!empty($customPaginationParameters['sorted']) ) {
21+
return;
22+
}
23+
1824
if (is_array($event->target) && 2 == count($event->target)) {
25+
$event->setCustomPaginationParameter('sorted', true);
26+
1927
$values = array_values($event->target);
2028
list($client, $query) = $values;
2129

tests/Test/Pager/Subscriber/Sortable/ArraySubscriberTest.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ public function shouldSort()
2323
$itemsEvent = new ItemsEvent(0, 10);
2424
$itemsEvent->target = &$array;
2525
$itemsEvent->options = array(PaginatorInterface::SORT_FIELD_PARAMETER_NAME => 'sort', PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME => 'ord');
26-
$_GET = array('sort' => '[entry][sortProperty]', 'ord' => 'asc');
2726

28-
$this->assertEquals(2, $array[0]['entry']['sortProperty']);
2927
$arraySubscriber = new ArraySubscriber();
28+
29+
// test asc sort
30+
$_GET = array('sort' => '[entry][sortProperty]', 'ord' => 'asc');
3031
$arraySubscriber->items($itemsEvent);
3132
$this->assertEquals(1, $array[0]['entry']['sortProperty']);
33+
34+
$itemsEvent->unsetCustomPaginationParameter('sorted');
35+
36+
// test desc sort
3237
$_GET ['ord'] = 'desc';
3338
$arraySubscriber->items($itemsEvent);
3439
$this->assertEquals(3, $array[0]['entry']['sortProperty']);
@@ -52,20 +57,26 @@ public function shouldSortWithCustomCallback()
5257
PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME => 'ord',
5358
'sortFunction' => function (&$target, $sortField, $sortDirection) {
5459
usort($target, function($object1, $object2) use ($sortField, $sortDirection) {
55-
if ($object1[$sortField] == $object2[$sortField]) {
60+
if ($object1[$sortField] === $object2[$sortField]) {
5661
return 0;
5762
}
5863

59-
return ($object1[$sortField] == 'hot' ? 1 : -1) * ($sortDirection == 'asc' ? 1 : -1);
64+
return ($object1[$sortField] === 'hot' ? 1 : -1) * ($sortDirection === 'asc' ? 1 : -1);
6065
});
6166
},
6267
);
63-
$_GET = array('sort' => '.name', 'ord' => 'asc');
6468

65-
$this->assertEquals('hot', $array[0]['name']);
6669
$arraySubscriber = new ArraySubscriber();
70+
71+
// test asc sort
72+
$_GET = array('sort' => '.name', 'ord' => 'asc');
6773
$arraySubscriber->items($itemsEvent);
6874
$this->assertEquals('cold', $array[0]['name']);
75+
76+
77+
$itemsEvent->unsetCustomPaginationParameter('sorted');
78+
79+
// test desc sort
6980
$_GET['ord'] = 'desc';
7081
$arraySubscriber->items($itemsEvent);
7182
$this->assertEquals('hot', $array[0]['name']);

0 commit comments

Comments
 (0)