Completed
Push — master ( be7319...157c74 )
by Matt
01:48
created

functions.php ➔ as_string()   A

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 8
Code Lines 4

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
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 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_resource($value)) {
52
        return '<RESOURCE>';
53 94
    }
54 78
55
    return (string) json_encode($value);
56
}
57 52
58 16
/**
59
 * Get the properties matching $pattern from the $data.
60
 *
61 44
 * @param string       $pattern
62 12
 * @param array|object $data
63
 * @return array
64
 */
65 44
function properties_matching_pattern($pattern, $data)
66 16
{
67
    // If an object is supplied, extract an array of the property names.
68
    if (is_object($data)) {
69 36
        $data = array_keys(get_object_vars($data));
70
    }
71
72
    return preg_grep(delimit_pattern($pattern), $data);
73 36
}
74 12
75
/**
76
 * Delimit a regular expression pattern.
77 28
 *
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 12
{
91 12
    return '/' . str_replace('/', '\\/', $pattern) . '/';
92 12
}
93
94 12
/**
95
 * Escape a JSON Pointer.
96
 *
97
 * @param  string $pointer
98
 * @return string
99
 */
100
function escape_pointer($pointer)
101
{
102
    $pointer = str_replace('~', '~0', $pointer);
103
    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 16
function is_json_integer($value)
114
{
115
    if (is_string($value) && strlen($value) && $value[0] === '-') {
116
        $value = substr($value, 1);
117
    }
118
119
    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 108
 * to a string because it is larger than PHP_INT_MAX.
125 108
 *
126
 * @param mixed $value
127
 *
128
 * @return boolean
129
 */
130
function is_json_number($value)
131
{
132
    return is_float($value) || is_json_integer($value);
133
}
134
135
/**
136
 * @param string|double|int $leftOperand
137 74
 * @param string|double|int $rightOperand
138 6
 *
139 6
 * @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 74
 */
142
function compare($leftOperand, $rightOperand)
143
{
144
    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 40
function strip_fragment($ref)
154
{
155
    $fragment = parse_url($ref, PHP_URL_FRAGMENT);
156
157
    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 40
 * @param string $ref
165
 *
166 40
 * @return bool
167
 */
168
function is_relative_ref($ref)
169
{
170
    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
    if ($parentScope === '') {
185
        return $id;
186
    }
187
188
    return Uri\resolve($parentScope, $id);
189
}
190