33
44class Utils
55{
6+ static $ typeMap = [
7+ 'boolean ' => 'boolean ' ,
8+ 'string ' => 'string ' ,
9+ 'NULL ' => 'null ' ,
10+ 'double ' => 'number ' ,
11+ 'float ' => 'number ' ,
12+ 'integer ' => 'number '
13+ ];
14+
615 /**
716 * Gets the JMESPath type equivalent of a PHP variable.
817 *
@@ -12,39 +21,32 @@ class Utils
1221 */
1322 public static function type ($ arg )
1423 {
15- static $ map = [
16- 'boolean ' => 'boolean ' ,
17- 'string ' => 'string ' ,
18- 'NULL ' => 'null ' ,
19- 'double ' => 'number ' ,
20- 'float ' => 'number ' ,
21- 'integer ' => 'number '
22- ];
23-
2424 $ type = gettype ($ arg );
25- if (isset ($ map [$ type ])) {
26- return $ map [$ type ];
27- }
28-
29- if ($ type == 'object ' ) {
30- if ($ arg instanceof \stdClass) {
31- return 'object ' ;
32- } elseif ($ arg instanceof \Closure) {
33- return 'expression ' ;
34- } elseif ($ arg instanceof \ArrayAccess
35- && $ arg instanceof \Countable
36- ) {
37- return count ($ arg ) == 0 || $ arg ->offsetExists (0 ) ? 'array ' : 'object ' ;
38- } elseif (method_exists ($ arg , '__toString ' )) {
39- return 'string ' ;
40- } else {
41- throw new \InvalidArgumentException (
42- 'Unable to determine JMESPath type from ' . get_class ($ arg )
43- );
25+ if (isset (self ::$ typeMap [$ type ])) {
26+ return self ::$ typeMap [$ type ];
27+ } elseif ($ type === 'array ' ) {
28+ if (empty ($ arg )) {
29+ return 'array ' ;
4430 }
31+ reset ($ arg );
32+ return key ($ arg ) === 0 ? 'array ' : 'object ' ;
33+ } elseif ($ arg instanceof \stdClass) {
34+ return 'object ' ;
35+ } elseif ($ arg instanceof \Closure) {
36+ return 'expression ' ;
37+ } elseif ($ arg instanceof \ArrayAccess
38+ && $ arg instanceof \Countable
39+ ) {
40+ return count ($ arg ) == 0 || $ arg ->offsetExists (0 )
41+ ? 'array '
42+ : 'object ' ;
43+ } elseif (method_exists ($ arg , '__toString ' )) {
44+ return 'string ' ;
4545 }
4646
47- return !$ arg || array_keys ($ arg )[0 ] === 0 ? 'array ' : 'object ' ;
47+ throw new \InvalidArgumentException (
48+ 'Unable to determine JMESPath type from ' . get_class ($ arg )
49+ );
4850 }
4951
5052 /**
0 commit comments