1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\JsonGuard; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A helper function to quickly build an error from a validator instance. |
7
|
|
|
* |
8
|
|
|
* @param string $message |
9
|
|
|
* @param \League\JsonGuard\Validator $validator |
10
|
|
|
* |
11
|
|
|
* @return \League\JsonGuard\ValidationError |
12
|
|
|
*/ |
13
|
|
|
function error($message, Validator $validator) |
14
|
|
|
{ |
15
|
92 |
|
return new ValidationError( |
16
|
46 |
|
$message, |
17
|
92 |
|
$validator->getCurrentKeyword(), |
18
|
92 |
|
$validator->getCurrentParameter(), |
19
|
92 |
|
$validator->getData(), |
20
|
92 |
|
$validator->getDataPath(), |
21
|
92 |
|
$validator->getSchema(), |
22
|
92 |
|
$validator->getSchemaPath() |
23
|
46 |
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $string |
28
|
|
|
* @param string $charset |
29
|
|
|
* |
30
|
|
|
* @return int |
31
|
|
|
*/ |
32
|
|
|
function strlen($string, $charset = 'UTF-8') |
33
|
|
|
{ |
34
|
12 |
|
if (function_exists('iconv_strlen')) { |
35
|
12 |
|
return iconv_strlen($string, $charset); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (function_exists('mb_strlen')) { |
39
|
|
|
return mb_strlen($string, $charset); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (function_exists('utf8_decode') && $charset === 'UTF-8') { |
43
|
|
|
$string = utf8_decode($string); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return \strlen($string); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the string representation of a value. |
51
|
|
|
* |
52
|
|
|
* @param mixed $value |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
function as_string($value) |
56
|
|
|
{ |
57
|
7 |
|
switch (true) { |
58
|
14 |
|
case is_scalar($value): |
59
|
8 |
|
$result = (string) $value; |
60
|
8 |
|
break; |
61
|
12 |
|
case is_resource($value): |
62
|
2 |
|
$result = '<RESOURCE>'; |
63
|
2 |
|
break; |
64
|
5 |
|
default: |
65
|
10 |
|
$result = (string) json_encode($value, JSON_UNESCAPED_SLASHES); |
66
|
5 |
|
} |
67
|
|
|
|
68
|
14 |
|
if (\strlen($result) > 100) { |
69
|
|
|
$result = substr($result, 0, 97) . '...'; |
70
|
|
|
} |
71
|
|
|
|
72
|
14 |
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the properties matching $pattern from the $data. |
77
|
|
|
* |
78
|
|
|
* @param string $pattern |
79
|
|
|
* @param array|object $data |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
function properties_matching_pattern($pattern, $data) |
83
|
|
|
{ |
84
|
|
|
// If an object is supplied, extract an array of the property names. |
85
|
6 |
|
if (is_object($data)) { |
86
|
6 |
|
$data = array_keys(get_object_vars($data)); |
87
|
3 |
|
} |
88
|
|
|
|
89
|
6 |
|
return preg_grep(delimit_pattern($pattern), $data); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Delimit a regular expression pattern. |
94
|
|
|
* |
95
|
|
|
* The regular expression syntax used for JSON schema is ECMA 262, from Javascript, |
96
|
|
|
* and does not use delimiters. Since the PCRE functions do, this function will |
97
|
|
|
* delimit a pattern and escape the delimiter if found in the pattern. |
98
|
|
|
* |
99
|
|
|
* @see http://json-schema.org/latest/json-schema-validation.html#anchor6 |
100
|
|
|
* @see http://php.net/manual/en/regexp.reference.delimiters.php |
101
|
|
|
* |
102
|
|
|
* @param string $pattern |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
function delimit_pattern($pattern) |
107
|
|
|
{ |
108
|
8 |
|
return '/' . str_replace('/', '\\/', $pattern) . '/'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Determines if the value is an integer or an integer that was cast to a string |
113
|
|
|
* because it is larger than PHP_INT_MAX. |
114
|
|
|
* |
115
|
|
|
* @param mixed $value |
116
|
|
|
* @return boolean |
117
|
|
|
*/ |
118
|
|
|
function is_json_integer($value) |
119
|
|
|
{ |
120
|
122 |
|
if (is_string($value) && \strlen($value) && $value[0] === '-') { |
121
|
6 |
|
$value = substr($value, 1); |
122
|
3 |
|
} |
123
|
|
|
|
124
|
122 |
|
return is_int($value) || (is_string($value) && ctype_digit($value) && bccomp($value, PHP_INT_MAX) === 1); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Determines if the value is a number. A number is a float, integer, or a number that was cast |
129
|
|
|
* to a string because it is larger than PHP_INT_MAX. |
130
|
|
|
* |
131
|
|
|
* @param mixed $value |
132
|
|
|
* |
133
|
|
|
* @return boolean |
134
|
|
|
*/ |
135
|
|
|
function is_json_number($value) |
136
|
|
|
{ |
137
|
104 |
|
return is_float($value) || is_json_integer($value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Push a segment onto the given JSON Pointer. |
142
|
|
|
* |
143
|
|
|
* @param string $pointer |
144
|
|
|
* @param string[] $segments |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
* |
148
|
|
|
*/ |
149
|
|
|
function pointer_push($pointer, ...$segments) |
150
|
|
|
{ |
151
|
|
|
$segments = array_map(function ($segment) { |
152
|
178 |
|
$segment = str_replace('~', '~0', $segment); |
153
|
178 |
|
return str_replace('/', '~1', $segment); |
154
|
178 |
|
}, $segments); |
155
|
|
|
|
156
|
178 |
|
return ($pointer !== '/' ? $pointer : '') . '/' . implode('/', $segments); |
157
|
|
|
} |
158
|
|
|
|