|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema\Constraint; |
|
4
|
|
|
|
|
5
|
|
|
use Swaggest\JsonSchema\Exception; |
|
6
|
|
|
use Swaggest\JsonSchema\Schema; |
|
7
|
|
|
use Swaggest\JsonSchema\SchemaContract; |
|
8
|
|
|
use Swaggest\JsonSchema\Structure\Egg; |
|
9
|
|
|
use Swaggest\JsonSchema\Structure\Nested; |
|
10
|
|
|
use Swaggest\JsonSchema\Structure\ObjectItem; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @method SchemaContract __get($key) |
|
14
|
|
|
*/ |
|
15
|
|
|
class Properties extends ObjectItem implements Constraint |
|
16
|
|
|
{ |
|
17
|
|
|
private $__isReadOnly = false; |
|
18
|
|
|
|
|
19
|
|
|
/** @var Schema[] */ |
|
20
|
|
|
protected $__arrayOfData = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** @var Schema */ |
|
23
|
|
|
protected $__schema; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Schema[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $__mappedProperties; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $__dataKeyMaps = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Data to property mapping, example ["$ref" => "ref"] |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
public $__dataToProperty = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Property to data mapping, example ["ref" => "$ref"] |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
public $__propertyToData = array(); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns a map of properties by default data name |
|
49
|
|
|
* @return Schema[] |
|
50
|
|
|
*/ |
|
51
|
3254 |
|
public function &toArray() |
|
52
|
|
|
{ |
|
53
|
3254 |
|
if (!isset($this->__propertyToData[Schema::DEFAULT_MAPPING])) { |
|
54
|
23 |
|
return $this->__arrayOfData; |
|
55
|
|
|
} |
|
56
|
3236 |
|
if (null === $this->__mappedProperties) { |
|
57
|
4 |
|
$properties = array(); |
|
58
|
4 |
|
foreach ($this->__arrayOfData as $propertyName => $property) { |
|
59
|
4 |
|
if (isset($this->__propertyToData[Schema::DEFAULT_MAPPING][$propertyName])) { |
|
60
|
4 |
|
$propertyName = $this->__propertyToData[Schema::DEFAULT_MAPPING][$propertyName]; |
|
61
|
|
|
} |
|
62
|
4 |
|
$properties[$propertyName] = $property; |
|
63
|
|
|
} |
|
64
|
4 |
|
$this->__mappedProperties = $properties; |
|
65
|
|
|
} |
|
66
|
3236 |
|
return $this->__mappedProperties; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $mapping |
|
71
|
|
|
* @return string[] a map of propertyName to dataName |
|
72
|
|
|
*/ |
|
73
|
97 |
|
public function getDataKeyMap($mapping = Schema::DEFAULT_MAPPING) |
|
74
|
|
|
{ |
|
75
|
97 |
|
if (!isset($this->__dataKeyMaps[$mapping])) { |
|
76
|
9 |
|
$map = array(); |
|
77
|
9 |
|
foreach ($this->__arrayOfData as $propertyName => $property) { |
|
78
|
9 |
|
if (isset($this->__propertyToData[$mapping][$propertyName])) { |
|
79
|
3 |
|
$map[$propertyName] = $this->__propertyToData[$mapping][$propertyName]; |
|
80
|
|
|
} else { |
|
81
|
9 |
|
$map[$propertyName] = $propertyName; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
9 |
|
$this->__dataKeyMaps[$mapping] = $map; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
97 |
|
return $this->__dataKeyMaps[$mapping]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
10 |
|
public function lock() |
|
91
|
|
|
{ |
|
92
|
10 |
|
$this->__isReadOnly = true; |
|
93
|
10 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
public function addPropertyMapping($dataName, $propertyName, $mapping = Schema::DEFAULT_MAPPING) |
|
97
|
|
|
{ |
|
98
|
4 |
|
$this->__dataToProperty[$mapping][$dataName] = $propertyName; |
|
99
|
4 |
|
$this->__propertyToData[$mapping][$propertyName] = $dataName; |
|
100
|
4 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $name |
|
105
|
|
|
* @param mixed $column |
|
106
|
|
|
* @return $this|static |
|
107
|
|
|
* @throws Exception |
|
108
|
|
|
*/ |
|
109
|
20 |
|
public function __set($name, $column) |
|
110
|
|
|
{ |
|
111
|
20 |
|
if ($this->__isReadOnly) { |
|
112
|
|
|
throw new Exception('Trying to modify read-only Properties'); |
|
113
|
|
|
} |
|
114
|
20 |
|
if ($column instanceof Nested) { |
|
115
|
4 |
|
$this->addNested($column->schema, $name); |
|
116
|
4 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
18 |
|
parent::__set($name, $column); |
|
119
|
18 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
3 |
|
public static function create() |
|
123
|
|
|
{ |
|
124
|
3 |
|
return new static; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** @var Egg[][] */ |
|
128
|
|
|
public $nestedProperties = array(); |
|
129
|
|
|
|
|
130
|
|
|
/** @var string[] */ |
|
131
|
|
|
public $nestedPropertyNames = array(); |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param SchemaContract $nested |
|
135
|
|
|
* @param string $name |
|
136
|
|
|
* @return $this |
|
137
|
|
|
* @throws Exception |
|
138
|
|
|
*/ |
|
139
|
4 |
|
protected function addNested(SchemaContract $nested, $name) |
|
140
|
|
|
{ |
|
141
|
4 |
|
if ($this->__isReadOnly) { |
|
142
|
|
|
throw new Exception('Trying to modify read-only Properties'); |
|
143
|
|
|
} |
|
144
|
4 |
|
if (null === $nested->getProperties()) { |
|
145
|
|
|
throw new Exception('Schema with properties required', Exception::PROPERTIES_REQUIRED); |
|
146
|
|
|
} |
|
147
|
4 |
|
$this->nestedPropertyNames[$name] = $name; |
|
148
|
4 |
|
foreach ($nested->getProperties()->toArray() as $propertyName => $property) { |
|
149
|
4 |
|
$this->nestedProperties[$propertyName][] = new Egg($nested, $name, $property); |
|
150
|
|
|
} |
|
151
|
4 |
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return Egg[][] |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getNestedProperties() |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->nestedProperties; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
10 |
|
public function isEmpty() |
|
163
|
|
|
{ |
|
164
|
10 |
|
return (count($this->__arrayOfData) + count($this->nestedProperties)) === 0; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
4 |
|
public function jsonSerialize() |
|
168
|
|
|
{ |
|
169
|
4 |
|
$result = $this->toArray(); |
|
170
|
|
|
|
|
171
|
4 |
|
if ($this->__nestedObjects) { |
|
|
|
|
|
|
172
|
|
|
foreach ($this->__nestedObjects as $object) { |
|
173
|
|
|
foreach ($object->toArray() as $key => $value) { |
|
174
|
|
|
$result[$key] = $value; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
4 |
|
return (object)$result; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.