|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the JVal package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace JVal; |
|
11
|
|
|
|
|
12
|
|
|
use JVal\Exception\UnsupportedTypeException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Wraps the list of primitive types defined in the JSON Schema Core |
|
16
|
|
|
* specification and provides utility methods to deal with them. |
|
17
|
|
|
*/ |
|
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
|
|
|
public 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
|
|
|
public 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
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
public static $phpJsonCompatibility = [ |
|
58
|
|
|
'arrayarray' => true, |
|
59
|
|
|
'booleanboolean' => true, |
|
60
|
|
|
'doublenumber' => true, |
|
61
|
|
|
'integerinteger' => true, |
|
62
|
|
|
'integernumber' => true, |
|
63
|
|
|
'NULLnull' => true, |
|
64
|
|
|
'objectobject' => true, |
|
65
|
|
|
'stringstring' => true, |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns the type of an instance according to JSON Schema Core 3.5. |
|
70
|
|
|
* |
|
71
|
|
|
* @param mixed $instance |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
* |
|
75
|
|
|
* @throws UnsupportedTypeException |
|
76
|
|
|
*/ |
|
77
|
356 |
|
public static function getPrimitiveTypeOf($instance) |
|
78
|
|
|
{ |
|
79
|
356 |
|
$phpType = gettype($instance); |
|
80
|
|
|
|
|
81
|
356 |
|
if (isset(self::$phpToJson[$phpType])) { |
|
82
|
355 |
|
return self::$phpToJson[$phpType]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
throw new UnsupportedTypeException($phpType); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns whether a type is part of the list of primitive types |
|
90
|
|
|
* defined by the specification. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $jsonType |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
204 |
|
public static function isPrimitive($jsonType) |
|
97
|
|
|
{ |
|
98
|
204 |
|
return isset(self::$jsonToPhp[$jsonType]); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|