@@ -24,35 +24,6 @@ public static function getInstance()
2424 return $ instance ;
2525 }
2626
27- /**
28- * Gets the JMESPath type equivalent of a PHP variable.
29- *
30- * @param mixed $arg PHP variable
31- * @return string Returns the JSON data type
32- */
33- public static function type ($ arg )
34- {
35- static $ map = [
36- 'boolean ' => 'boolean ' ,
37- 'string ' => 'string ' ,
38- 'NULL ' => 'null ' ,
39- 'double ' => 'number ' ,
40- 'integer ' => 'number ' ,
41- 'object ' => 'object '
42- ];
43-
44- if (is_callable ($ arg )) {
45- return 'expression ' ;
46- }
47-
48- $ type = gettype ($ arg );
49- if (isset ($ map [$ type ])) {
50- return $ map [$ type ];
51- }
52-
53- return !$ arg || array_keys ($ arg )[0 ] === 0 ? 'array ' : 'object ' ;
54- }
55-
5627 /**
5728 * @param string $fn Function name.
5829 * @param array $args Function arguments.
@@ -204,7 +175,7 @@ private function fn_sort(array $args)
204175 {
205176 $ this ->validate ('sort ' , $ args , [['array ' ]]);
206177 $ valid = ['string ' , 'number ' ];
207- return self ::stableSort ($ args [0 ], function ($ a , $ b ) use ($ valid ) {
178+ return Utils ::stableSort ($ args [0 ], function ($ a , $ b ) use ($ valid ) {
208179 $ this ->validateSeq ('sort:0 ' , $ valid , $ a , $ b );
209180 return strnatcmp ($ a , $ b );
210181 });
@@ -215,7 +186,7 @@ private function fn_sort_by(array $args)
215186 $ this ->validate ('sort_by ' , $ args , [['array ' ], ['expression ' ]]);
216187 $ expr = $ args [1 ];
217188 $ valid = ['string ' , 'number ' ];
218- return self ::stableSort (
189+ return Utils ::stableSort (
219190 $ args [0 ],
220191 function ($ a , $ b ) use ($ expr , $ valid ) {
221192 $ va = $ expr ($ a );
@@ -226,29 +197,6 @@ function ($a, $b) use ($expr, $valid) {
226197 );
227198 }
228199
229- /**
230- * JMESPath requires a stable sorting algorithm, so here we'll implement
231- * a simple Schwartzian transform that uses array index positions as tie
232- * breakers.
233- *
234- * @param array $data List or map of data to sort
235- * @param callable $sortFn Callable used to sort values
236- *
237- * @return array Returns the sorted array
238- * @link http://en.wikipedia.org/wiki/Schwartzian_transform
239- */
240- private function stableSort (array $ data , callable $ sortFn )
241- {
242- // Decorate each item by creating an array of [value, index]
243- array_walk ($ data , function (&$ v , $ k ) { $ v = [$ v , $ k ]; });
244- // Sort by the sort function and use the index as a tie-breaker
245- uasort ($ data , function ($ a , $ b ) use ($ sortFn ) {
246- return $ sortFn ($ a [0 ], $ b [0 ]) ?: ($ a [1 ] < $ b [1 ] ? -1 : 1 );
247- });
248- // Undecorate each item and return the resulting sorted array
249- return array_map (function ($ v ) { return $ v [0 ]; }, array_values ($ data ));
250- }
251-
252200 private function fn_starts_with (array $ args )
253201 {
254202 $ this ->validate ('starts_with ' , $ args , [['string ' ], ['string ' ]]);
@@ -259,7 +207,7 @@ private function fn_starts_with(array $args)
259207 private function fn_type (array $ args )
260208 {
261209 $ this ->validateArity ('type ' , count ($ args ), 1 );
262- return self ::type ($ args [0 ]);
210+ return Utils ::type ($ args [0 ]);
263211 }
264212
265213 private function fn_to_string (array $ args )
@@ -272,7 +220,7 @@ private function fn_to_number(array $args)
272220 {
273221 $ this ->validateArity ('to_number ' , count ($ args ), 1 );
274222 $ value = $ args [0 ];
275- $ type = self ::type ($ value );
223+ $ type = Utils ::type ($ value );
276224 if ($ type == 'number ' ) {
277225 return $ value ;
278226 } elseif ($ type == 'string ' && is_numeric ($ value )) {
@@ -398,13 +346,13 @@ private function validate($from, $args, $types = [])
398346 private function validateType ($ from , $ value , array $ types )
399347 {
400348 if ($ types [0 ] == 'any '
401- || in_array (self ::type ($ value ), $ types )
349+ || in_array (Utils ::type ($ value ), $ types )
402350 || ($ value === [] && in_array ('object ' , $ types ))
403351 ) {
404352 return ;
405353 }
406354 $ msg = 'must be one of the following types: ' . implode (', ' , $ types )
407- . '. ' . self ::type ($ value ) . ' found ' ;
355+ . '. ' . Utils ::type ($ value ) . ' found ' ;
408356 $ this ->typeError ($ from , $ msg );
409357 }
410358
@@ -419,8 +367,8 @@ private function validateType($from, $value, array $types)
419367 */
420368 private function validateSeq ($ from , array $ types , $ a , $ b )
421369 {
422- $ ta = self ::type ($ a );
423- $ tb = self ::type ($ b );
370+ $ ta = Utils ::type ($ a );
371+ $ tb = Utils ::type ($ b );
424372
425373 if ($ ta != $ tb ) {
426374 $ msg = "encountered a type mismatch in sequence: {$ ta }, {$ tb }" ;
0 commit comments