Completed
Push — master ( 26c529...f44c95 )
by Matt
02:23
created

functions.php ➔ is_relative_ref()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
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 44
function json_decode($json, $assoc = false, $depth = 512, $options = 0)
16
{
17 44
    $data = \json_decode($json, $assoc, $depth, $options);
18
19
    if (json_last_error() !== JSON_ERROR_NONE) {
20
        throw new \InvalidArgumentException(sprintf('Invalid JSON: %s', json_last_error_msg()));
21 44
    }
22
23
    return $data;
24
}
25
26
/**
27
 * @param $string
28
 * @return int
29
 */
30 76
function strlen($string)
31 76
{
32
    if (extension_loaded('intl')) {
33
        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 116
function as_string($value)
50 60
{
51
    if (is_string($value)) {
52
        return $value;
53 94
    }
54 78
55
    if (is_int($value)) {
56
        return (string)$value;
57 52
    }
58 16
59
    if (is_bool($value)) {
60
        return $value ? '<TRUE>' : '<FALSE>';
61 44
    }
62 12
63
    if (is_object($value)) {
64
        return get_class($value);
65 44
    }
66 16
67
    if (is_array($value)) {
68
        return '<ARRAY>';
69 36
    }
70
71
    if (is_resource($value)) {
72
        return '<RESOURCE>';
73 36
    }
74 12
75
    if (is_null($value)) {
76
        return '<NULL>';
77 28
    }
78
79
    return '<UNKNOWN>';
80
}
81
82
/**
83
 * Get the properties matching $pattern from the $data.
84
 *
85
 * @param string       $pattern
86
 * @param array|object $data
87
 * @return array
88
 */
89
function properties_matching_pattern($pattern, $data)
90 12
{
91 12
    // If an object is supplied, extract an array of the property names.
92 12
    if (is_object($data)) {
93
        $data = array_keys(get_object_vars($data));
94 12
    }
95
96
    return preg_grep(delimit_pattern($pattern), $data);
97
}
98
99
/**
100
 * Delimit a regular expression pattern.
101
 *
102
 * The regular expression syntax used for JSON schema is ECMA 262, from Javascript,
103
 * and does not use delimiters.  Since the PCRE functions do, this function will
104
 * delimit a pattern and escape the delimiter if found in the pattern.
105
 *
106
 * @see http://json-schema.org/latest/json-schema-validation.html#anchor6
107
 * @see http://php.net/manual/en/regexp.reference.delimiters.php
108
 *
109
 * @param string $pattern
110
 *
111
 * @return string
112
 */
113 16
function delimit_pattern($pattern)
114
{
115
    return '/' . str_replace('/', '\\/', $pattern) . '/';
116
}
117
118
/**
119
 * Escape a JSON Pointer.
120
 *
121
 * @param  string $pointer
122
 * @return string
123
 */
124 108
function escape_pointer($pointer)
125 108
{
126
    $pointer = str_replace('~', '~0', $pointer);
127
    return str_replace('/', '~1', $pointer);
128
}
129
130
/**
131
 * Determines if the value is an integer or an integer that was cast to a string
132
 * because it is larger than PHP_INT_MAX.
133
 *
134
 * @param  mixed  $value
135
 * @return boolean
136
 */
137 74
function is_json_integer($value)
138 6
{
139 6
    if (is_string($value) && strlen($value) && $value[0] === '-') {
140
        $value = substr($value, 1);
141 74
    }
142
143
    return is_int($value) || (is_string($value) && ctype_digit($value) && compare($value, PHP_INT_MAX) === 1);
144
}
145
146
/**
147
 * @param string|double|int $leftOperand
148
 * @param string|double|int $rightOperand
149
 *
150
 * @return int Returns 0 if the two operands are equal, 1 if the left_operand is larger than the right_operand,
151
 * -1 otherwise.
152
 */
153 40
function compare($leftOperand, $rightOperand)
154
{
155
    return Comparator::compare($leftOperand, $rightOperand);
156
}
157
158
/**
159
 * Removes the fragment from a reference.
160
 *
161
 * @param  string $ref
162
 * @return string
163
 */
164 40
function strip_fragment($ref)
165
{
166 40
    $fragment = parse_url($ref, PHP_URL_FRAGMENT);
167
168
    return $fragment ? str_replace($fragment, '', $ref) : $ref;
169
}
170
171
/**
172
 * Determine if a reference is relative.
173
 * A reference is relative if it does not being with a prefix.
174
 *
175
 * @param string $ref
176
 *
177
 * @return bool
178
 */
179
function is_relative_ref($ref)
180
{
181
    return !preg_match('#^.+\:\/\/.*#', $ref);
182
}
183
184
/**
185
 * Resolve the given id against the parent scope and return the resolved URI.
186
 *
187
 * @param string $id          The id to resolve.  This should be a valid relative or absolute URI.
188
 * @param string $parentScope The parent scope to resolve against.  Should be a valid URI or empty.z
189
 *
190
 * @return string
191
 */
192
function resolve_uri($id, $parentScope)
193
{
194
    // If the id is absolute, it doesn't need to be resolved.
195
    if (!is_relative_ref($id)) {
196
        return $id;
197
    }
198
199
    // If there is no parent scope, there is nothing to resolve against.
200
    if ($parentScope === '') {
201
        return $id;
202
    }
203
204
    $uri   = Uri\parse($parentScope);
205
    $parts = Uri\parse($id);
206
207
    if (!empty($parts['path'])) {
208
        // If the path ends in a slash, it shouldn't be replaced but instead appended to.
209
        if ('/' === substr($uri['path'], -1)) {
210
            $uri['path'] .= ltrim($parts['path'], '/');
211
        } else {
212
            $uri['path'] = '/' . ltrim($parts['path'], '/');
213
        }
214
    }
215
216
    if (!empty($parts['fragment'])) {
217
        $uri['fragment'] = $parts['fragment'];
218
    }
219
220
    return Uri\build($uri);
221
}
222