1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\JsonGuard; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @param string $string |
7
|
|
|
* @param string $charset |
8
|
|
|
* |
9
|
|
|
* @return int |
10
|
|
|
*/ |
11
|
|
|
function strlen($string, $charset = 'UTF-8') |
12
|
|
|
{ |
13
|
12 |
|
if (function_exists('iconv_strlen')) { |
14
|
12 |
|
return iconv_strlen($string, $charset); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
if (function_exists('mb_strlen')) { |
18
|
|
|
return mb_strlen($string, $charset); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if (function_exists('utf8_decode') && $charset === 'UTF-8') { |
22
|
|
|
$string = utf8_decode($string); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return \strlen($string); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns the string representation of a value. |
30
|
|
|
* |
31
|
|
|
* @param mixed $value |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
function as_string($value) |
35
|
|
|
{ |
36
|
100 |
|
if (is_resource($value)) { |
37
|
2 |
|
return '<RESOURCE>'; |
38
|
|
|
} |
39
|
|
|
|
40
|
98 |
|
return (string) json_encode($value); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the properties matching $pattern from the $data. |
45
|
|
|
* |
46
|
|
|
* @param string $pattern |
47
|
|
|
* @param array|object $data |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
function properties_matching_pattern($pattern, $data) |
51
|
|
|
{ |
52
|
|
|
// If an object is supplied, extract an array of the property names. |
53
|
6 |
|
if (is_object($data)) { |
54
|
6 |
|
$data = array_keys(get_object_vars($data)); |
55
|
6 |
|
} |
56
|
|
|
|
57
|
6 |
|
return preg_grep(delimit_pattern($pattern), $data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Delimit a regular expression pattern. |
62
|
|
|
* |
63
|
|
|
* The regular expression syntax used for JSON schema is ECMA 262, from Javascript, |
64
|
|
|
* and does not use delimiters. Since the PCRE functions do, this function will |
65
|
|
|
* delimit a pattern and escape the delimiter if found in the pattern. |
66
|
|
|
* |
67
|
|
|
* @see http://json-schema.org/latest/json-schema-validation.html#anchor6 |
68
|
|
|
* @see http://php.net/manual/en/regexp.reference.delimiters.php |
69
|
|
|
* |
70
|
|
|
* @param string $pattern |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
function delimit_pattern($pattern) |
75
|
|
|
{ |
76
|
8 |
|
return '/' . str_replace('/', '\\/', $pattern) . '/'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Determines if the value is an integer or an integer that was cast to a string |
81
|
|
|
* because it is larger than PHP_INT_MAX. |
82
|
|
|
* |
83
|
|
|
* @param mixed $value |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
|
|
function is_json_integer($value) |
87
|
|
|
{ |
88
|
122 |
|
if (is_string($value) && \strlen($value) && $value[0] === '-') { |
89
|
6 |
|
$value = substr($value, 1); |
90
|
6 |
|
} |
91
|
|
|
|
92
|
122 |
|
return is_int($value) || (is_string($value) && ctype_digit($value) && bccomp($value, PHP_INT_MAX) === 1); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Determines if the value is a number. A number is a float, integer, or a number that was cast |
97
|
|
|
* to a string because it is larger than PHP_INT_MAX. |
98
|
|
|
* |
99
|
|
|
* @param mixed $value |
100
|
|
|
* |
101
|
|
|
* @return boolean |
102
|
|
|
*/ |
103
|
|
|
function is_json_number($value) |
104
|
|
|
{ |
105
|
102 |
|
return is_float($value) || is_json_integer($value); |
106
|
|
|
} |
107
|
|
|
|