1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema\Structure; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonSchema\Constraint\Properties; |
6
|
|
|
use Swaggest\JsonSchema\Context; |
7
|
|
|
use Swaggest\JsonSchema\Schema; |
8
|
|
|
use Swaggest\JsonSchema\Wrapper; |
9
|
|
|
use Swaggest\JsonSchema\NameMirror; |
10
|
|
|
|
11
|
|
|
trait ClassStructureTrait |
12
|
|
|
{ |
13
|
|
|
use ObjectItemTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Serialize additional and pattern properties |
17
|
|
|
* @var boolean |
18
|
3252 |
|
*/ |
19
|
|
|
protected $extendedPropertySerialization = false; |
20
|
3252 |
|
|
21
|
3252 |
|
/** |
22
|
3252 |
|
* @return Wrapper |
23
|
|
|
*/ |
24
|
3252 |
|
public static function schema() |
25
|
10 |
|
{ |
26
|
10 |
|
static $schemas = array(); |
27
|
10 |
|
$className = get_called_class(); |
28
|
10 |
|
$schemaWrapper = &$schemas[$className]; |
29
|
10 |
|
|
30
|
10 |
|
if (null === $schemaWrapper) { |
31
|
10 |
|
$schema = new Schema(); |
32
|
8 |
|
$properties = new Properties(); |
33
|
|
|
$schema->properties = $properties; |
34
|
10 |
|
$schema->objectItemClass = $className; |
35
|
1 |
|
$schemaWrapper = new Wrapper($schema); |
36
|
|
|
static::setUpProperties($properties, $schema); |
37
|
10 |
|
if (null === $schema->getFromRefs()) { |
38
|
|
|
$schema->setFromRef('#/definitions/' . $className); |
39
|
|
|
} |
40
|
3252 |
|
if ($properties->isEmpty()) { |
41
|
|
|
$schema->properties = null; |
42
|
|
|
} |
43
|
|
|
$properties->lock(); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
return $schemaWrapper; |
47
|
|
|
} |
48
|
2 |
|
|
49
|
|
|
/** |
50
|
|
|
* @return Properties|static|null |
51
|
|
|
*/ |
52
|
|
|
public static function properties() |
53
|
|
|
{ |
54
|
|
|
return static::schema()->getProperties(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
3235 |
|
* @param mixed $data |
59
|
|
|
* @param Context $options |
60
|
3235 |
|
* @return static|mixed |
61
|
|
|
* @throws \Swaggest\JsonSchema\Exception |
62
|
|
|
* @throws \Swaggest\JsonSchema\InvalidValue |
63
|
|
|
*/ |
64
|
|
|
public static function import($data, Context $options = null) |
65
|
|
|
{ |
66
|
|
|
return static::schema()->in($data, $options); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
18 |
|
* @param mixed $data |
71
|
|
|
* @param Context $options |
72
|
18 |
|
* @return mixed |
73
|
|
|
* @throws \Swaggest\JsonSchema\InvalidValue |
74
|
|
|
* @throws \Exception |
75
|
|
|
*/ |
76
|
|
|
public static function export($data, Context $options = null) |
77
|
|
|
{ |
78
|
|
|
return static::schema()->out($data, $options); |
79
|
2 |
|
} |
80
|
|
|
|
81
|
2 |
|
/** |
82
|
2 |
|
* @param ObjectItemContract $objectItem |
83
|
|
|
* @return static |
84
|
|
|
*/ |
85
|
|
|
public static function pick(ObjectItemContract $objectItem) |
86
|
|
|
{ |
87
|
|
|
$className = get_called_class(); |
88
|
6 |
|
return $objectItem->getNestedObject($className); |
89
|
|
|
} |
90
|
6 |
|
|
91
|
|
|
/** |
92
|
|
|
* @return static |
93
|
|
|
*/ |
94
|
|
|
public static function create() |
95
|
97 |
|
{ |
96
|
|
|
return new static; |
97
|
97 |
|
} |
98
|
97 |
|
|
99
|
97 |
|
protected $__validateOnSet = true; // todo skip validation during import |
100
|
97 |
|
|
101
|
97 |
|
/** |
102
|
57 |
|
* @return \stdClass |
103
|
|
|
*/ |
104
|
|
|
public function jsonSerialize() |
105
|
97 |
|
{ |
106
|
|
|
$result = new \stdClass(); |
107
|
4 |
|
$schema = static::schema(); |
108
|
4 |
|
$properties = $schema->getProperties(); |
109
|
2 |
|
if (null !== $properties) { |
110
|
2 |
|
foreach ($properties->getDataKeyMap() as $propertyName => $dataName) { |
111
|
|
|
$value = $this->$propertyName; |
112
|
|
|
// Value is exported if exists. |
113
|
|
|
if (null !== $value || array_key_exists($propertyName, $this->__arrayOfData)) { |
114
|
|
|
$result->$dataName = $value; |
115
|
97 |
|
continue; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Non-existent value is only exported if belongs to nullable property (having 'null' in type array). |
119
|
|
|
$property = $schema->getProperty($propertyName); |
120
|
|
|
if ($property instanceof Schema) { |
121
|
4 |
|
$types = $property->type; |
122
|
|
|
if ($types === Schema::NULL || (is_array($types) && in_array(Schema::NULL, $types))) { |
123
|
4 |
|
$result->$dataName = $value; |
124
|
4 |
|
} |
125
|
4 |
|
} |
126
|
|
|
} |
127
|
4 |
|
} |
128
|
|
|
foreach ($schema->getNestedPropertyNames() as $name) { |
129
|
|
|
/** @var ObjectItem $nested */ |
130
|
249 |
|
$nested = $this->$name; |
131
|
|
|
if (null !== $nested) { |
132
|
249 |
|
foreach ((array)$nested->jsonSerialize() as $key => $value) { |
133
|
5 |
|
$result->$key = $value; |
134
|
4 |
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
248 |
|
|
138
|
248 |
|
if ($this->extendedPropertySerialization) { |
139
|
|
|
foreach ($this->getAdditionalPropertyNames() as $name) { |
140
|
|
|
$result->$name = $this->{$name}; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
foreach ($this->getAllPatternPropertyNames() as $name) { |
144
|
|
|
$result->$name = $this->{$name}; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $result; |
149
|
|
|
} |
150
|
1 |
|
|
151
|
|
|
/** |
152
|
1 |
|
* @return static|NameMirror |
153
|
1 |
|
*/ |
154
|
|
|
public static function names() |
155
|
|
|
{ |
156
|
|
|
static $nameflector = null; |
157
|
|
|
if (null === $nameflector) { |
158
|
|
|
$nameflector = new NameMirror(); |
159
|
|
|
} |
160
|
|
|
return $nameflector; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function __set($name, $column) // todo nested schemas |
164
|
|
|
{ |
165
|
|
|
if ($this->__validateOnSet) { |
166
|
|
|
if ($property = static::schema()->getProperty($name)) { |
167
|
|
|
$property->out($column); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
$this->__arrayOfData[$name] = $column; |
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public static function className() |
175
|
|
|
{ |
176
|
|
|
return get_called_class(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @throws \Exception |
181
|
|
|
* @throws \Swaggest\JsonSchema\InvalidValue |
182
|
|
|
*/ |
183
|
|
|
public function validate() |
184
|
|
|
{ |
185
|
|
|
static::schema()->out($this); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Setter for $extendedPropertySerialization |
190
|
|
|
* @param boolean $extend |
191
|
|
|
* @return self |
192
|
|
|
*/ |
193
|
|
|
public function setExtendedPropertySerialization($extend = true) |
194
|
|
|
{ |
195
|
|
|
$this->extendedPropertySerialization = $extend; |
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|