1 | <?php |
||
18 | class Types |
||
19 | { |
||
20 | const TYPE_ARRAY = 'array'; |
||
21 | const TYPE_BOOLEAN = 'boolean'; |
||
22 | const TYPE_INTEGER = 'integer'; |
||
23 | const TYPE_NUMBER = 'number'; |
||
24 | const TYPE_NULL = 'null'; |
||
25 | const TYPE_OBJECT = 'object'; |
||
26 | const TYPE_STRING = 'string'; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $phpToJson = [ |
||
32 | 'array' => self::TYPE_ARRAY, |
||
33 | 'boolean' => self::TYPE_BOOLEAN, |
||
34 | 'double' => self::TYPE_NUMBER, |
||
35 | 'integer' => self::TYPE_INTEGER, |
||
36 | 'NULL' => self::TYPE_NULL, |
||
37 | 'object' => self::TYPE_OBJECT, |
||
38 | 'string' => self::TYPE_STRING, |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private static $jsonToPhp = [ |
||
45 | self::TYPE_ARRAY => 'array', |
||
46 | self::TYPE_BOOLEAN => 'boolean', |
||
47 | self::TYPE_INTEGER => 'integer', |
||
48 | self::TYPE_NUMBER => 'double', |
||
49 | self::TYPE_NULL => 'NULL', |
||
50 | self::TYPE_OBJECT => 'object', |
||
51 | self::TYPE_STRING => 'string', |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Maps PHP native types to set of compatible JSON types. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | private static $typeCompatibility = [ |
||
60 | 'array' => ['array' => true], |
||
61 | 'boolean' => ['boolean' => true], |
||
62 | 'double' => ['number' => true], |
||
63 | 'integer' => ['integer' => true, 'number' => true], |
||
64 | 'NULL' => ['null' => true], |
||
65 | 'object' => ['object' => true], |
||
66 | 'resource' => [], |
||
67 | 'string' => ['string' => true], |
||
68 | 'unknown type' => [], |
||
69 | ]; |
||
70 | |||
71 | /** |
||
72 | * Returns the type of an instance according to JSON Schema Core 3.5. |
||
73 | * |
||
74 | * @param mixed $instance |
||
75 | * |
||
76 | * @return string |
||
77 | * |
||
78 | * @throws UnsupportedTypeException |
||
79 | */ |
||
80 | 356 | public static function getPrimitiveTypeOf($instance) |
|
90 | |||
91 | /** |
||
92 | * Returns whether an instance matches a given JSON type. |
||
93 | * |
||
94 | * @param mixed $instance |
||
95 | * @param string $type |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | 245 | public static function isA($instance, $type) |
|
103 | |||
104 | /** |
||
105 | * Returns whether an instance matches at least one of given JSON types. |
||
106 | * |
||
107 | * @param mixed $instance |
||
108 | * @param string[] $types |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | 15 | public static function isOneOf($instance, array $types) |
|
124 | |||
125 | /** |
||
126 | * Returns whether a type is part of the list of primitive types |
||
127 | * defined by the specification. |
||
128 | * |
||
129 | * @param string $type |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | 204 | public static function isPrimitive($type) |
|
137 | } |
||
138 |