4
4
5
5
use Knp \Component \Pager \Event \ItemsEvent ;
6
6
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
7
+ use Symfony \Component \PropertyAccess \Exception \UnexpectedTypeException ;
7
8
use Symfony \Component \PropertyAccess \PropertyAccess ;
8
9
use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
9
10
use Knp \Component \Pager \PaginatorInterface ;
@@ -78,16 +79,25 @@ private function proxySortFunction(&$target, $sortField, $sortDirection) {
78
79
* @param mixed $object1 first object to compare
79
80
* @param mixed $object2 second object to compare
80
81
*
81
- * @return boolean
82
+ * @return int
82
83
*/
83
84
private function sortFunction ($ object1 , $ object2 )
84
85
{
85
86
if (!$ this ->propertyAccessor ) {
86
87
throw new \UnexpectedValueException ('You need symfony/property-access component to use this sorting function ' );
87
88
}
88
89
89
- $ fieldValue1 = $ this ->propertyAccessor ->getValue ($ object1 , $ this ->currentSortingField );
90
- $ fieldValue2 = $ this ->propertyAccessor ->getValue ($ object2 , $ this ->currentSortingField );
90
+ try {
91
+ $ fieldValue1 = $ this ->propertyAccessor ->getValue ($ object1 , $ this ->currentSortingField );
92
+ } catch (UnexpectedTypeException $ e ) {
93
+ return -1 * $ this ->getSortCoefficient ();
94
+ }
95
+
96
+ try {
97
+ $ fieldValue2 = $ this ->propertyAccessor ->getValue ($ object2 , $ this ->currentSortingField );
98
+ } catch (UnexpectedTypeException $ e ) {
99
+ return 1 * $ this ->getSortCoefficient ();
100
+ }
91
101
92
102
if (is_string ($ fieldValue1 )) {
93
103
$ fieldValue1 = mb_strtolower ($ fieldValue1 );
@@ -101,7 +111,12 @@ private function sortFunction($object1, $object2)
101
111
return 0 ;
102
112
}
103
113
104
- return ($ fieldValue1 > $ fieldValue2 ? 1 : -1 ) * ($ this ->sortDirection === 'asc ' ? 1 : -1 );
114
+ return ($ fieldValue1 > $ fieldValue2 ? 1 : -1 ) * $ this ->getSortCoefficient ();
115
+ }
116
+
117
+ private function getSortCoefficient ()
118
+ {
119
+ return $ this ->sortDirection === 'asc ' ? 1 : -1 ;
105
120
}
106
121
107
122
public static function getSubscribedEvents ()
0 commit comments