Completed
Pull Request — master (#6)
by
unknown
01:28
created

Schema::type()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 10
    public static function make($json): Schema
20
    {
21 10
        if ( ! isset($json->type)) {
22 1
            throw new \InvalidArgumentException('Missing schema type.');
23
        }
24
25 9
        if ($json->type === 'array') {
26 3
            return new Schema\ArraySchema($json);
27
        }
28
29 9
        if ($json->type === 'boolean') {
30 2
            return new Schema\BooleanSchema($json);
31
        }
32
33 8
        if ($json->type === 'integer') {
34 3
            return new Schema\IntegerSchema($json);
35
        }
36
37 7
        if ($json->type === 'null') {
38 2
            return new Schema\NullSchema($json);
39
        }
40
41 6
        if ($json->type === 'number') {
42 2
            return new Schema\NumberSchema($json);
43
        }
44
45 5
        if ($json->type === 'object') {
46 3
            return new Schema\ObjectSchema($json);
47
        }
48
49 5
        if ($json->type === 'string') {
50 5
            return new Schema\StringSchema($json);
51
        }
52
53
        // @codeCoverageIgnoreStart
54
        throw new \InvalidArgumentException(sprintf(
55
            "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...
56
            $json->type
57
        ));
58
        // @codeCoverageIgnoreEnd
59
    }
60
61
    /**
62
     * @var object
63
     */
64
    protected $schema;
65
66 9
    public function __construct($schema)
67
    {
68 9
        $this->schema = $schema;
69 9
    }
70
71 9
    public function type(): string
72
    {
73 9
        return $this->schema->type;
74
    }
75
76
    abstract public function phpType(): string;
77
78 3
    public function isArray(): bool
79
    {
80 3
        return $this->type() === 'array';
81
    }
82
83 1
    public function isBoolean(): bool
84
    {
85 1
        return $this->type() === 'boolean';
86
    }
87
88 2
    public function isInteger(): bool
89
    {
90 2
        return $this->type() === 'integer';
91
    }
92
93 2
    public function isNull(): bool
94
    {
95 2
        return $this->type() === 'null';
96
    }
97
98 1
    public function isNumber(): bool
99
    {
100 1
        return $this->type() === 'number';
101
    }
102
103 3
    public function isObject(): bool
104
    {
105 3
        return $this->type() === 'object';
106
    }
107
108 3
    public function isString(): bool
109
    {
110 3
        return $this->type() === 'string';
111
    }
112
113 1
    public function title(): string
114
    {
115 1
        return $this->schema->title ?? '';
116
    }
117
}
118