Completed
Push — master ( 8e214b...00d25f )
by Woody
12s
created

Schema::make()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 20
cts 20
cp 1
rs 4.909
c 0
b 0
f 0
cc 9
eloc 20
nc 9
nop 1
crap 9
1
<?php
2
3
namespace RoundingWell\Schematic;
4
5
abstract class Schema
6
{
7
    /**
8
     * @param string $path
9
     * @return static
10
     */
11 2
    public static function fromFile($path): Schema
12
    {
13 2
        return self::make(json_decode(file_get_contents($path)));
14
    }
15
16
    /**
17
     * @param object $json
18
     */
19 11
    public static function make($json): Schema
20
    {
21 11
        if ( ! isset($json->type)) {
22 1
            throw new \InvalidArgumentException('Missing schema type.');
23
        }
24
25 10
        if ($json->type === 'array') {
26 3
            return new Schema\ArraySchema($json);
27
        }
28
29 10
        if ($json->type === 'boolean') {
30 2
            return new Schema\BooleanSchema($json);
31
        }
32
33 9
        if ($json->type === 'integer') {
34 3
            return new Schema\IntegerSchema($json);
35
        }
36
37 8
        if ($json->type === 'null') {
38 2
            return new Schema\NullSchema($json);
39
        }
40
41 7
        if ($json->type === 'number') {
42 2
            return new Schema\NumberSchema($json);
43
        }
44
45 6
        if ($json->type === 'object') {
46 3
            return new Schema\ObjectSchema($json);
47
        }
48
49 6
        if ($json->type === 'string') {
50 5
            return new Schema\StringSchema($json);
51
        }
52
53 1
        throw new \InvalidArgumentException(sprintf(
54 1
            "No schema type available for %s.",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal No schema type available for %s. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
55 1
            $json->type
56
        ));
57
    }
58
59
    /**
60
     * @var object
61
     */
62
    protected $schema;
63
64 9
    public function __construct($schema)
65
    {
66 9
        $this->schema = $schema;
67 9
    }
68
69 9
    public function type(): string
70
    {
71 9
        return $this->schema->type;
72
    }
73
74
    abstract public function phpType(): string;
75
76 3
    public function isArray(): bool
77
    {
78 3
        return $this->type() === 'array';
79
    }
80
81 1
    public function isBoolean(): bool
82
    {
83 1
        return $this->type() === 'boolean';
84
    }
85
86 2
    public function isInteger(): bool
87
    {
88 2
        return $this->type() === 'integer';
89
    }
90
91 2
    public function isNull(): bool
92
    {
93 2
        return $this->type() === 'null';
94
    }
95
96 1
    public function isNumber(): bool
97
    {
98 1
        return $this->type() === 'number';
99
    }
100
101 3
    public function isObject(): bool
102
    {
103 3
        return $this->type() === 'object';
104
    }
105
106 3
    public function isString(): bool
107
    {
108 3
        return $this->type() === 'string';
109
    }
110
111 1
    public function title(): string
112
    {
113 1
        return $this->schema->title ?? '';
114
    }
115
}
116