Completed
Push — master ( a0be20...c3e525 )
by Matt
02:18
created

functions.php ➔ json_decode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 4
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard;
4
5
use Sabre\Uri;
6
7
/**
8
 * @param string $json
9
 * @param bool   $assoc
10
 * @param int    $depth
11
 * @param int    $options
12
 * @return mixed
13
 * @throws \InvalidArgumentException
14
 */
15
function json_decode($json, $assoc = false, $depth = 512, $options = 0)
16
{
17 50
    $data = \json_decode($json, $assoc, $depth, $options);
18
19 50
    if (json_last_error() !== JSON_ERROR_NONE) {
20 2
        throw new \InvalidArgumentException(sprintf('Invalid JSON: %s', json_last_error_msg()));
21
    }
22
23 48
    return $data;
24
}
25
26
/**
27
 * @param $string
28
 * @return int
29
 */
30
function strlen($string)
31
{
32 100
    if (extension_loaded('intl')) {
33 100
        return grapheme_strlen($string);
34
    }
35
36
    if (extension_loaded('mbstring')) {
37
        return mb_strlen($string, mb_detect_encoding($string));
38
    }
39
40
    return \strlen($string);
41
}
42
43
/**
44
 * Returns the string representation of a value.
45
 *
46
 * @param mixed $value
47
 * @return string
48
 */
49
function as_string($value)
50
{
51 142
    if (is_resource($value)) {
52 2
        return '<RESOURCE>';
53
    }
54
55 140
    return (string) json_encode($value);
56
}
57
58
/**
59
 * Get the properties matching $pattern from the $data.
60
 *
61
 * @param string       $pattern
62
 * @param array|object $data
63
 * @return array
64
 */
65
function properties_matching_pattern($pattern, $data)
66
{
67
    // If an object is supplied, extract an array of the property names.
68 12
    if (is_object($data)) {
69 12
        $data = array_keys(get_object_vars($data));
70 12
    }
71
72 12
    return preg_grep(delimit_pattern($pattern), $data);
73
}
74
75
/**
76
 * Delimit a regular expression pattern.
77
 *
78
 * The regular expression syntax used for JSON schema is ECMA 262, from Javascript,
79
 * and does not use delimiters.  Since the PCRE functions do, this function will
80
 * delimit a pattern and escape the delimiter if found in the pattern.
81
 *
82
 * @see http://json-schema.org/latest/json-schema-validation.html#anchor6
83
 * @see http://php.net/manual/en/regexp.reference.delimiters.php
84
 *
85
 * @param string $pattern
86
 *
87
 * @return string
88
 */
89
function delimit_pattern($pattern)
90
{
91 16
    return '/' . str_replace('/', '\\/', $pattern) . '/';
92
}
93
94
/**
95
 * Escape a JSON Pointer.
96
 *
97
 * @param  string $pointer
98
 * @return string
99
 */
100
function escape_pointer($pointer)
101
{
102 112
    $pointer = str_replace('~', '~0', $pointer);
103 112
    return str_replace('/', '~1', $pointer);
104
}
105
106
/**
107
 * Determines if the value is an integer or an integer that was cast to a string
108
 * because it is larger than PHP_INT_MAX.
109
 *
110
 * @param  mixed $value
111
 * @return boolean
112
 */
113
function is_json_integer($value)
114
{
115 154
    if (is_string($value) && strlen($value) && $value[0] === '-') {
116 6
        $value = substr($value, 1);
117 6
    }
118
119 154
    return is_int($value) || (is_string($value) && ctype_digit($value) && compare($value, PHP_INT_MAX) === 1);
120
}
121
122
/**
123
 * Determines if the value is a number.  A number is a float, integer, or a number that was cast
124
 * to a string because it is larger than PHP_INT_MAX.
125
 *
126
 * @param mixed $value
127
 *
128
 * @return boolean
129
 */
130
function is_json_number($value)
131
{
132 124
    return is_float($value) || is_json_integer($value);
133
}
134
135
/**
136
 * @param string|double|int $leftOperand
137
 * @param string|double|int $rightOperand
138
 *
139
 * @return int Returns 0 if the two operands are equal, 1 if the left_operand is larger than the right_operand,
140
 * -1 otherwise.
141
 */
142
function compare($leftOperand, $rightOperand)
143
{
144 42
    return Comparator::compare($leftOperand, $rightOperand);
145
}
146
147
/**
148
 * Removes the fragment from a reference.
149
 *
150
 * @param  string $ref
151
 * @return string
152
 */
153
function strip_fragment($ref)
154
{
155 44
    $fragment = parse_url($ref, PHP_URL_FRAGMENT);
156
157 44
    return $fragment ? str_replace($fragment, '', $ref) : $ref;
158
}
159
160
/**
161
 * Determine if a reference is relative.
162
 * A reference is relative if it does not being with a prefix.
163
 *
164
 * @param string $ref
165
 *
166
 * @return bool
167
 */
168
function is_relative_ref($ref)
169
{
170 24
    return !preg_match('#^.+\:\/\/.*#', $ref);
171
}
172
173
/**
174
 * Resolve the given id against the parent scope and return the resolved URI.
175
 *
176
 * @param string $id          The id to resolve.  This should be a valid relative or absolute URI.
177
 * @param string $parentScope The parent scope to resolve against.  Should be a valid URI or empty.
178
 *
179
 * @return string
180
 */
181
function resolve_uri($id, $parentScope)
182
{
183
    // If there is no parent scope, there is nothing to resolve against.
184 36
    if ($parentScope === '') {
185 14
        return $id;
186
    }
187
188 30
    return Uri\resolve($parentScope, $id);
189
}
190