Completed
Pull Request — master (#10)
by Jan
02:39
created

Types::getPrimitiveTypeOf()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 17
cts 17
cp 1
rs 6.1403
cc 8
eloc 18
nc 8
nop 1
crap 8
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
     * Returns the type of an instance according to JSON Schema Core 3.5.
30
     *
31
     * @param mixed $instance
32
     *
33
     * @return string
34
     *
35
     * @throws UnsupportedTypeException
36
     */
37 356
    public static function getPrimitiveTypeOf($instance)
38
    {
39 356
        $phpType = gettype($instance);
40
41
        switch ($phpType) {
42 356
            case 'integer':
43 119
                return self::TYPE_INTEGER;
44 304
            case 'string':
45 97
                return self::TYPE_STRING;
46 242
            case 'boolean':
47 25
                return self::TYPE_BOOLEAN;
48 225
            case 'double':
49 34
                return self::TYPE_NUMBER;
50 191
            case 'object':
51 126
                return self::TYPE_OBJECT;
52 78
            case 'array':
53 62
                return self::TYPE_ARRAY;
54 18
            case 'NULL':
55 17
                return self::TYPE_NULL;
56
        }
57
58 1
        throw new UnsupportedTypeException($phpType);
59
    }
60
61
    /**
62
     * Returns whether an instance matches a given type.
63
     *
64
     * @param mixed  $instance
65
     * @param string $type
66
     *
67
     * @return bool
68
     */
69 260
    public static function isA($instance, $type)
70
    {
71 260
        $phpType = gettype($instance);
72
73
        switch ($phpType) {
74 260
            case 'integer':
75 120
                return $type === self::TYPE_INTEGER || $type === self::TYPE_NUMBER;
76 169
            case 'string':
77 49
                return $type === self::TYPE_STRING;
78 124
            case 'boolean':
79 17
                return $type === self::TYPE_BOOLEAN;
80 107
            case 'double':
81 55
                return $type === self::TYPE_NUMBER;
82 53
            case 'object':
83 26
                return $type === self::TYPE_OBJECT;
84 28
            case 'array':
85 16
                return $type === self::TYPE_ARRAY;
86 12
            case 'NULL':
87 12
                return $type === self::TYPE_NULL;
88
        }
89
90
        throw new UnsupportedTypeException($phpType);
91
    }
92
93
    /**
94
     * Returns whether a type is part of the list of primitive types
95
     * defined by the specification.
96
     *
97
     * @param string $type
98
     *
99
     * @return bool
100
     */
101 204
    public static function isPrimitive($type)
102
    {
103
        return $type === self::TYPE_ARRAY
104 204
            || $type === self::TYPE_BOOLEAN
105 197
            || $type === self::TYPE_INTEGER
106 185
            || $type === self::TYPE_NUMBER
107 96
            || $type === self::TYPE_NULL
108 79
            || $type === self::TYPE_OBJECT
109 204
            || $type === self::TYPE_STRING;
110
    }
111
}
112