Completed
Push — master ( 0a0b74...dcdfcd )
by Woody
03:12
created

Schema::title()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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