Skip to content

Commit

Permalink
Merge pull request #207 from KnpLabs/fix/unaccessible-property
Browse files Browse the repository at this point in the history
[RFR] be able to sort on NULL relation with PropertyAccessor
  • Loading branch information
nicolasmure authored Sep 11, 2018
2 parents f0f8303 + 84eb450 commit fc1755b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Knp\Component\Pager\Event\ItemsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Knp\Component\Pager\PaginatorInterface;
Expand Down Expand Up @@ -78,16 +79,25 @@ private function proxySortFunction(&$target, $sortField, $sortDirection) {
* @param mixed $object1 first object to compare
* @param mixed $object2 second object to compare
*
* @return boolean
* @return int
*/
private function sortFunction($object1, $object2)
{
if (!$this->propertyAccessor) {
throw new \UnexpectedValueException('You need symfony/property-access component to use this sorting function');
}

$fieldValue1 = $this->propertyAccessor->getValue($object1, $this->currentSortingField);
$fieldValue2 = $this->propertyAccessor->getValue($object2, $this->currentSortingField);
try {
$fieldValue1 = $this->propertyAccessor->getValue($object1, $this->currentSortingField);
} catch (UnexpectedTypeException $e) {
return -1 * $this->getSortCoefficient();
}

try {
$fieldValue2 = $this->propertyAccessor->getValue($object2, $this->currentSortingField);
} catch (UnexpectedTypeException $e) {
return 1 * $this->getSortCoefficient();
}

if (is_string($fieldValue1)) {
$fieldValue1 = mb_strtolower($fieldValue1);
Expand All @@ -101,7 +111,12 @@ private function sortFunction($object1, $object2)
return 0;
}

return ($fieldValue1 > $fieldValue2 ? 1 : -1) * ($this->sortDirection === 'asc' ? 1 : -1);
return ($fieldValue1 > $fieldValue2 ? 1 : -1) * $this->getSortCoefficient();
}

private function getSortCoefficient()
{
return $this->sortDirection === 'asc' ? 1 : -1;
}

public static function getSubscribedEvents()
Expand Down
30 changes: 30 additions & 0 deletions tests/Test/Pager/Subscriber/Sortable/ArraySubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,34 @@ public function shouldSortWithCustomCallback()
$this->assertEquals('hot', $array[0]['name']);

}

/**
* @test
*/
public function shouldSortEvenWhenTheSortPropertyIsNotAccessible()
{
$array = array(
array('entry' => array('sortProperty' => 2)),
array('entry' => array()),
array('entry' => array('sortProperty' => 1)),
);

$itemsEvent = new ItemsEvent(0, 10);
$itemsEvent->target = &$array;
$itemsEvent->options = array(PaginatorInterface::SORT_FIELD_PARAMETER_NAME => 'sort', PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME => 'ord');

$arraySubscriber = new ArraySubscriber();

// test asc sort
$_GET = array('sort' => '[entry][sortProperty]', 'ord' => 'asc');
$arraySubscriber->items($itemsEvent);
$this->assertEquals(false, isset($array[0]['entry']['sortProperty']));

$itemsEvent->unsetCustomPaginationParameter('sorted');

// test desc sort
$_GET ['ord'] = 'desc';
$arraySubscriber->items($itemsEvent);
$this->assertEquals(2, $array[0]['entry']['sortProperty']);
}
}

0 comments on commit fc1755b

Please sign in to comment.