1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\PhpApi\Descriptions package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\PhpApi\Descriptions\Description\Schema; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Element; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Visitor\VisiteeMixin; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Represents standard JSON Schema but with support for complex types |
16
|
|
|
* |
17
|
|
|
* @author John Kleijn <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
abstract class Schema implements Element |
20
|
|
|
{ |
21
|
|
|
use VisiteeMixin; |
22
|
|
|
|
23
|
|
|
const TYPE_ANY = 'any'; |
24
|
|
|
const TYPE_ARRAY = 'array'; |
25
|
|
|
const TYPE_BOOL = 'boolean'; |
26
|
|
|
const TYPE_INT = 'integer'; |
27
|
|
|
const TYPE_NUMBER = 'number'; |
28
|
|
|
const TYPE_NULL = 'null'; |
29
|
|
|
const TYPE_OBJECT = 'object'; |
30
|
|
|
const TYPE_STRING = 'string'; |
31
|
|
|
|
32
|
|
|
const FORMAT_DATE = 'date'; |
33
|
|
|
const FORMAT_DATE_TIME = 'date-time'; |
34
|
|
|
|
35
|
|
|
const FORMAT_INT32 = 'int32'; |
36
|
|
|
const FORMAT_INT64 = 'int64'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \stdClass |
40
|
|
|
*/ |
41
|
|
|
protected $definition; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $type; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var mixed |
50
|
|
|
*/ |
51
|
|
|
protected $default; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $title = ''; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $description = ''; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
protected $readOnly = false; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
protected $writeOnly = false; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected $examples = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Schema constructor. |
80
|
|
|
* |
81
|
|
|
* @param \stdClass $definition |
82
|
|
|
*/ |
83
|
|
|
public function __construct(\stdClass $definition) |
84
|
|
|
{ |
85
|
|
|
foreach (array_keys(get_object_vars($this)) as $propertyName) { |
86
|
|
|
if (isset($definition->$propertyName)) { |
87
|
|
|
$this->$propertyName = $definition->$propertyName; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
$this->definition = $definition; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function getDefault() |
97
|
|
|
{ |
98
|
|
|
return $this->default; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getTitle(): string |
105
|
|
|
{ |
106
|
|
|
return $this->title; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getDescription(): string |
113
|
|
|
{ |
114
|
|
|
return $this->description; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function isReadOnly(): bool |
121
|
|
|
{ |
122
|
|
|
return $this->readOnly; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function isWriteOnly(): bool |
129
|
|
|
{ |
130
|
|
|
return $this->writeOnly; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getExamples(): array |
137
|
|
|
{ |
138
|
|
|
return $this->examples; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $type |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public function isPrimitive(string $type): bool |
147
|
|
|
{ |
148
|
|
|
return $this->type === $type; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function getType(): string |
155
|
|
|
{ |
156
|
|
|
return $this->type; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return \stdClass |
161
|
|
|
*/ |
162
|
|
|
public function getDefinition() |
163
|
|
|
{ |
164
|
|
|
return $this->definition; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $type |
169
|
|
|
* |
170
|
|
|
* @return mixed |
171
|
|
|
*/ |
172
|
|
|
public function isType($type) |
173
|
|
|
{ |
174
|
|
|
return $this->isPrimitive($type); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|