Completed
Pull Request — master (#21)
by Matt
04:51
created

functions.php ➔ is_integer()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 7.7656

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 5
c 1
b 1
f 0
nc 13
nop 1
dl 0
loc 9
ccs 3
cts 4
cp 0.75
crap 7.7656
rs 8.2222
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 28
    $data = \json_decode($json, $assoc, $depth, $options);
16
17 28
    if (json_last_error() !== JSON_ERROR_NONE) {
18
        throw new \InvalidArgumentException(sprintf('Invalid JSON: %s', json_last_error_msg()));
19
    }
20
21 28
    return $data;
22
}
23
24
/**
25
 * @param $string
26
 * @return int
27
 */
28
function strlen($string)
29
{
30 10
    if (extension_loaded('intl')) {
31 10
        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 as_string($value)
48
{
49 62
    if (is_string($value)) {
50 32
        return $value;
51
    }
52
53 48
    if (is_int($value)) {
54 40
        return (string)$value;
55
    }
56
57 26
    if (is_bool($value)) {
58 8
        return $value ? '<TRUE>' : '<FALSE>';
59
    }
60
61 22
    if (is_object($value)) {
62 6
        return get_class($value);
63
    }
64
65 22
    if (is_array($value)) {
66 8
        return '<ARRAY>';
67
    }
68
69 18
    if (is_resource($value)) {
70
        return '<RESOURCE>';
71
    }
72
73 18
    if (is_null($value)) {
74 6
        return '<NULL>';
75
    }
76
77 14
    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 properties_matching_pattern($pattern, $data)
88
{
89
    // If an object is supplied, extract an array of the property names.
90 6
    if (is_object($data)) {
91 6
        $data = array_keys(get_object_vars($data));
92 6
    }
93
94 6
    return preg_grep(delimit_pattern($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 delimit_pattern($pattern)
112
{
113 8
    return '/' . str_replace('/', '\\/', $pattern) . '/';
114
}
115
116
/**
117
 * Escape a JSON Pointer.
118
 *
119
 * @param  string $pointer
120
 * @return string
121
 */
122
function escape_pointer($pointer)
123
{
124 62
    $pointer = str_replace('~', '~0', $pointer);
125 62
    return str_replace('/', '~1', $pointer);
126
}
127
128
/**
129
 * Compare two numbers.  If the number is larger than PHP_INT_MAX and
130
 * the bcmatch extension is installed, this function will use bccomp.
131
 *
132
 * @param  string|int $leftOperand
133
 * @param  string $operator         one of : '>', '>=', '=', '<', '<='.
134
 * @param  string|int $rightOperand
135
 * @return bool
136
 */
137
function compare($leftOperand, $operator, $rightOperand)
138
{
139 62
    if (!function_exists('bccomp')) {
140
        switch ($operator) {
141
            case '>':
142
                return $leftOperand > $rightOperand;
143
            case '>=':
144
                return $leftOperand >= $rightOperand;
145
            case '=':
146
                return $leftOperand >= $rightOperand;
147
            case '<':
148
                return $leftOperand < $rightOperand;
149
            case '<=':
150
                return $leftOperand <= $rightOperand;
151
            default:
152
                throw new \InvalidArgumentException(
153
                    sprintf('Unknown operator %s', $operator)
154
                );
155
        }
156
    }
157
158 62
    $result = bccomp($leftOperand, $rightOperand, 5);
159
    switch ($operator) {
160 62
        case '>':
161 12
            return $result === 1;
162 52
        case '>=':
163 22
            return $result === 0 || $result === 1;
164 32
        case '=':
165 8
            return $result === 0;
166 24
        case '<':
167 10
            return $result === -1;
168 16
        case '<=':
169 14
            return $result === 0 || $result === -1;
170 2
        default:
171 2
            throw new \InvalidArgumentException(
172 2
                sprintf('Unknown operator %s', $operator)
173 2
            );
174 2
    }
175
}
176
177
/**
178
 * Determines if the value is an integer or an integer that was cast to a string
179
 * because it is larger than PHP_INT_MAX.
180
 *
181
 * @param  mixed  $value
182
 * @return boolean
183
 */
184
function is_integer($value)
185
{
186 32
    if (!function_exists('bccomp')) {
187
        return is_int($value);
188
    }
189
190 32
    $isNumericString = is_string($value) && (ctype_digit($value) || $value[0] === '-' && ctype_digit(substr($value, 1)));
191 32
    return is_int($value) || ($isNumericString && bccomp($value, PHP_INT_MAX, 0) === 1);
192
}
193