1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RoundingWell\Schematic; |
4
|
|
|
|
5
|
|
|
abstract class Schema |
6
|
|
|
{ |
7
|
|
|
const SCHEMA_TYPES = [ |
8
|
|
|
'array', |
9
|
|
|
'boolean', |
10
|
|
|
'integer', |
11
|
|
|
'null', |
12
|
|
|
'number', |
13
|
|
|
'object', |
14
|
|
|
'string' |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $path |
19
|
|
|
* @return static |
20
|
|
|
*/ |
21
|
|
|
public static function fromFile($path) |
22
|
|
|
{ |
23
|
|
|
return self::make(json_decode(file_get_contents($path))); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param object $json |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public static function make($json) |
31
|
|
|
{ |
32
|
|
|
if (!isset($json->type)) { |
33
|
|
|
throw new \InvalidArgumentException('Missing schema type.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!in_array(strtolower($json->type), self::SCHEMA_TYPES)) { |
37
|
|
|
throw new \InvalidArgumentException(sprintf( |
38
|
|
|
'No schema type available for %s.', |
39
|
|
|
$json->type |
40
|
|
|
)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$schema = 'RoundingWell\\Schematic\\Schema\\' . ucfirst($json->type) . 'Schema'; |
44
|
|
|
|
45
|
|
|
return new $schema($json); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var object |
50
|
|
|
*/ |
51
|
|
|
protected $schema; |
52
|
|
|
|
53
|
|
|
public function __construct($schema) |
54
|
|
|
{ |
55
|
|
|
$this->schema = $schema; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function type() |
59
|
|
|
{ |
60
|
|
|
return $this->schema->type; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
abstract public function phpType(); |
64
|
|
|
|
65
|
|
|
public function isArray() |
66
|
|
|
{ |
67
|
|
|
return $this->type() === 'array'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function isBoolean() |
71
|
|
|
{ |
72
|
|
|
return $this->type() === 'boolean'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function isInteger() |
76
|
|
|
{ |
77
|
|
|
return $this->type() === 'integer'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function isNull() |
81
|
|
|
{ |
82
|
|
|
return $this->type() === 'null'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function isNumber() |
86
|
|
|
{ |
87
|
|
|
return $this->type() === 'number'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function isObject() |
91
|
|
|
{ |
92
|
|
|
return $this->type() === 'object'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function isString() |
96
|
|
|
{ |
97
|
|
|
return $this->type() === 'string'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function title() |
101
|
|
|
{ |
102
|
|
|
return $this->schema->title ? $this->schema->title : ''; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getDescription() |
106
|
|
|
{ |
107
|
|
|
return $this->schema->description ? $this->schema->description : ''; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function isEnum() |
111
|
|
|
{ |
112
|
|
|
return isset($this->schema->enum); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getEnum() |
116
|
|
|
{ |
117
|
|
|
return isset($this->schema->enum) ? $this->schema->enum : []; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|