Completed
Push — master ( fdc4c7...a2204f )
by Stéphane
7s
created

Types::getPrimitiveTypeOf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal;
11
12
use JVal\Exception\UnsupportedTypeException;
13
14
/**
15
 * Wraps the list of primitive types defined in the JSON Schema Core
16
 * specification and provides utility methods to deal with them.
17
 */
18
class Types
19
{
20
    const TYPE_ARRAY = 'array';
21
    const TYPE_BOOLEAN = 'boolean';
22
    const TYPE_INTEGER = 'integer';
23
    const TYPE_NUMBER = 'number';
24
    const TYPE_NULL = 'null';
25
    const TYPE_OBJECT = 'object';
26
    const TYPE_STRING = 'string';
27
28
    /**
29
     * @var array
30
     */
31
    private static $phpToJson = [
32
        'array' => self::TYPE_ARRAY,
33
        'boolean' => self::TYPE_BOOLEAN,
34
        'double' => self::TYPE_NUMBER,
35
        'integer' => self::TYPE_INTEGER,
36
        'NULL' => self::TYPE_NULL,
37
        'object' => self::TYPE_OBJECT,
38
        'string' => self::TYPE_STRING,
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    private static $jsonToPhp = [
45
        self::TYPE_ARRAY => 'array',
46
        self::TYPE_BOOLEAN => 'boolean',
47
        self::TYPE_INTEGER => 'integer',
48
        self::TYPE_NUMBER => 'double',
49
        self::TYPE_NULL => 'NULL',
50
        self::TYPE_OBJECT => 'object',
51
        self::TYPE_STRING => 'string',
52
    ];
53
54
    /**
55
     * Maps PHP native types to set of compatible JSON types.
56
     *
57
     * @var array
58
     */
59
    private static $typeCompatibility = [
60
        'array' => ['array' => true],
61
        'boolean' => ['boolean' => true],
62
        'double' => ['number' => true],
63
        'integer' => ['integer' => true, 'number' => true],
64
        'NULL' => ['null' => true],
65
        'object' => ['object' => true],
66
        'resource' => [],
67
        'string' => ['string' => true],
68
        'unknown type' => [],
69
    ];
70
71
    /**
72
     * Returns the type of an instance according to JSON Schema Core 3.5.
73
     *
74
     * @param mixed $instance
75
     *
76
     * @return string
77
     *
78
     * @throws UnsupportedTypeException
79
     */
80 356
    public static function getPrimitiveTypeOf($instance)
81
    {
82 356
        $phpType = gettype($instance);
83
84 356
        if (isset(self::$phpToJson[$phpType])) {
85 355
            return self::$phpToJson[$phpType];
86
        }
87
88 1
        throw new UnsupportedTypeException($phpType);
89
    }
90
91
    /**
92
     * Returns whether an instance matches a given JSON type.
93
     *
94
     * @param mixed  $instance
95
     * @param string $type
96
     *
97
     * @return bool
98
     */
99 245
    public static function isA($instance, $type)
100
    {
101 245
        return isset(self::$typeCompatibility[gettype($instance)][$type]);
102
    }
103
104
    /**
105
     * Returns whether an instance matches at least one of given JSON types.
106
     *
107
     * @param mixed    $instance
108
     * @param string[] $types
109
     *
110
     * @return bool
111
     */
112 15
    public static function isOneOf($instance, array $types)
113
    {
114 15
        $possible = self::$typeCompatibility[gettype($instance)];
115
116 15
        foreach ($types as $type) {
117 15
            if (isset($possible[$type])) {
118 8
                return true;
119
            }
120 11
        }
121
122 7
        return false;
123
    }
124
125
    /**
126
     * Returns whether a type is part of the list of primitive types
127
     * defined by the specification.
128
     *
129
     * @param string $type
130
     *
131
     * @return bool
132
     */
133 204
    public static function isPrimitive($type)
134
    {
135 204
        return isset(self::$jsonToPhp[$type]);
136
    }
137
}
138