Completed
Pull Request — master (#108)
by Matt
11:28
created

functions.php ➔ strlen()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 2
dl 0
loc 16
ccs 4
cts 4
cp 1
crap 5
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard;
4
5
/**
6
 * @param string $string
7
 * @param string $charset
8
 *
9
 * @return int
10
 */
11
function strlen($string, $charset = 'UTF-8')
12
{
13
    if (function_exists('iconv_strlen')) {
14
        return iconv_strlen($string, $charset);
15
    }
16
17 52
    if (function_exists('mb_strlen')) {
18
        return mb_strlen($string, $charset);
19 52
    }
20 2
21
    if (function_exists('utf8_decode') && $charset === 'UTF-8') {
22
        $string = utf8_decode($string);
23 50
    }
24
25
    return \strlen($string);
26
}
27
28
/**
29
 * Returns the string representation of a value.
30
 *
31
 * @param mixed $value
32 100
 * @return string
33 100
 */
34
function as_string($value)
35
{
36
    if (is_resource($value)) {
37
        return '<RESOURCE>';
38
    }
39
40
    return (string) json_encode($value);
41
}
42
43
/**
44
 * Get the properties matching $pattern from the $data.
45
 *
46
 * @param string       $pattern
47
 * @param array|object $data
48
 * @return array
49
 */
50
function properties_matching_pattern($pattern, $data)
51 154
{
52 2
    // If an object is supplied, extract an array of the property names.
53
    if (is_object($data)) {
54
        $data = array_keys(get_object_vars($data));
55 152
    }
56
57
    return preg_grep(delimit_pattern($pattern), $data);
58
}
59
60
/**
61
 * Delimit a regular expression pattern.
62
 *
63
 * The regular expression syntax used for JSON schema is ECMA 262, from Javascript,
64
 * and does not use delimiters.  Since the PCRE functions do, this function will
65
 * delimit a pattern and escape the delimiter if found in the pattern.
66
 *
67
 * @see http://json-schema.org/latest/json-schema-validation.html#anchor6
68 12
 * @see http://php.net/manual/en/regexp.reference.delimiters.php
69 12
 *
70 12
 * @param string $pattern
71
 *
72 12
 * @return string
73
 */
74
function delimit_pattern($pattern)
75
{
76
    return '/' . str_replace('/', '\\/', $pattern) . '/';
77
}
78
79
/**
80
 * Determines if the value is an integer or an integer that was cast to a string
81
 * because it is larger than PHP_INT_MAX.
82
 *
83
 * @param  mixed $value
84
 * @return boolean
85
 */
86
function is_json_integer($value)
87
{
88
    if (is_string($value) && \strlen($value) && $value[0] === '-') {
89
        $value = substr($value, 1);
90
    }
91 16
92
    return is_int($value) || (is_string($value) && ctype_digit($value) && bccomp($value, PHP_INT_MAX) === 1);
93
}
94
95
/**
96
 * Determines if the value is a number.  A number is a float, integer, or a number that was cast
97
 * to a string because it is larger than PHP_INT_MAX.
98
 *
99
 * @param mixed $value
100
 *
101
 * @return boolean
102 116
 */
103 116
function is_json_number($value)
104
{
105
    return is_float($value) || is_json_integer($value);
106
}
107