1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\JsonGuard; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @param string $json |
7
|
|
|
* @param bool $assoc |
8
|
|
|
* @param int $depth |
9
|
|
|
* @param int $options |
10
|
|
|
* @return mixed |
11
|
|
|
* @throws \InvalidArgumentException |
12
|
|
|
*/ |
13
|
|
|
function json_decode($json, $assoc = false, $depth = 512, $options = 0) |
14
|
|
|
{ |
15
|
24 |
|
$data = \json_decode($json, $assoc, $depth, $options); |
16
|
|
|
|
17
|
24 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
18
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid JSON: %s', json_last_error_msg())); |
19
|
|
|
} |
20
|
|
|
|
21
|
24 |
|
return $data; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $string |
26
|
|
|
* @return int |
27
|
|
|
*/ |
28
|
|
|
function strlen($string) |
29
|
|
|
{ |
30
|
8 |
|
if (extension_loaded('intl')) { |
31
|
8 |
|
return grapheme_strlen($string); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (extension_loaded('mbstring')) { |
35
|
|
|
return mb_strlen($string, mb_detect_encoding($string)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return strlen($string); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the string representation of a value. |
43
|
|
|
* |
44
|
|
|
* @param mixed $value |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
function asString($value) |
48
|
|
|
{ |
49
|
60 |
|
if (is_string($value)) { |
50
|
30 |
|
return $value; |
51
|
|
|
} |
52
|
|
|
|
53
|
50 |
|
if (is_int($value)) { |
54
|
40 |
|
return (string)$value; |
55
|
|
|
} |
56
|
|
|
|
57
|
28 |
|
if (is_bool($value)) { |
58
|
8 |
|
return $value ? '<TRUE>' : '<FALSE>'; |
59
|
|
|
} |
60
|
|
|
|
61
|
24 |
|
if (is_object($value)) { |
62
|
6 |
|
return get_class($value); |
63
|
|
|
} |
64
|
|
|
|
65
|
24 |
|
if (is_array($value)) { |
66
|
8 |
|
return '<ARRAY>'; |
67
|
|
|
} |
68
|
|
|
|
69
|
20 |
|
if (is_resource($value)) { |
70
|
|
|
return '<RESOURCE>'; |
71
|
|
|
} |
72
|
|
|
|
73
|
20 |
|
if (is_null($value)) { |
74
|
6 |
|
return '<NULL>'; |
75
|
|
|
} |
76
|
|
|
|
77
|
16 |
|
return '<UNKNOWN>'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the properties matching $pattern from the $data. |
82
|
|
|
* |
83
|
|
|
* @param string $pattern |
84
|
|
|
* @param array|object $data |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
function propertiesMatchingPattern($pattern, $data) |
88
|
|
|
{ |
89
|
|
|
// If an object is supplied, extract an array of the property names. |
90
|
4 |
|
if (is_object($data)) { |
91
|
4 |
|
$data = array_keys(get_object_vars($data)); |
92
|
4 |
|
} |
93
|
|
|
|
94
|
4 |
|
return preg_grep(delimitPattern($pattern), $data); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Delimit a regular expression pattern. |
99
|
|
|
* |
100
|
|
|
* The regular expression syntax used for JSON schema is ECMA 262, from Javascript, |
101
|
|
|
* and does not use delimiters. Since the PCRE functions do, this function will |
102
|
|
|
* delimit a pattern and escape the delimiter if found in the pattern. |
103
|
|
|
* |
104
|
|
|
* @see http://json-schema.org/latest/json-schema-validation.html#anchor6 |
105
|
|
|
* @see http://php.net/manual/en/regexp.reference.delimiters.php |
106
|
|
|
* |
107
|
|
|
* @param string $pattern |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
function delimitPattern($pattern) |
112
|
|
|
{ |
113
|
6 |
|
return '/' . str_replace('/', '\\/', $pattern) . '/'; |
114
|
|
|
} |
115
|
|
|
|