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