1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema\Structure; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonSchema\MagicMapTrait; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait ObjectItemTrait |
9
|
|
|
* @package Swaggest\JsonSchema\Structure |
10
|
|
|
* @see ObjectItemContract |
11
|
|
|
*/ |
12
|
|
|
trait ObjectItemTrait |
13
|
|
|
{ |
14
|
|
|
use MagicMapTrait; |
15
|
|
|
|
16
|
|
|
/** @var ObjectItemContract[] */ |
17
|
|
|
protected $__nestedObjects; |
18
|
|
|
protected $__documentPath; |
19
|
|
|
/** @var null|string[] */ |
20
|
|
|
protected $__fromRef; |
21
|
2 |
|
|
22
|
2 |
|
protected $__hasResolvedValue = false; |
23
|
2 |
|
protected $__resolvedValue; |
24
|
|
|
|
25
|
|
|
public function setResolvedValue($value) |
26
|
|
|
{ |
27
|
|
|
$this->__hasResolvedValue = true; |
28
|
5 |
|
$this->__resolvedValue = $value; |
29
|
|
|
return $this; |
30
|
5 |
|
} |
31
|
5 |
|
|
32
|
5 |
|
public function getResolvedValue() |
33
|
5 |
|
{ |
34
|
5 |
|
return $this->__resolvedValue; |
35
|
5 |
|
} |
36
|
3 |
|
|
37
|
|
|
public function hasResolvedValue() |
38
|
|
|
{ |
39
|
5 |
|
return $this->__hasResolvedValue; |
40
|
5 |
|
} |
41
|
5 |
|
|
42
|
|
|
public function getNestedObject($className) |
43
|
|
|
{ |
44
|
1012 |
|
if (isset($this->__nestedObjects[$className])) { |
45
|
|
|
return $this->__nestedObjects[$className]; |
46
|
1012 |
|
} |
47
|
1012 |
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function setNestedProperty($propertyName, $value, Egg $nestedEgg) |
51
|
|
|
{ |
52
|
|
|
$nestedName = $nestedEgg->name; |
53
|
|
|
/** @var null $nested */ |
54
|
|
|
$nested = &$this->__nestedObjects[$nestedName]; |
55
|
|
|
if (null === $nested) { |
|
|
|
|
56
|
|
|
$nested = $nestedEgg->classSchema->makeObjectItem(); |
57
|
|
|
$this->__nestedObjects[$nestedName] = $nested; |
58
|
|
|
if ($nestedName !== $nestedEgg->classSchema->getObjectItemClass()) { |
59
|
|
|
$this->$nestedName = $nested; |
60
|
|
|
} |
61
|
|
|
} |
62
|
99 |
|
$nested->$propertyName = $value; |
63
|
|
|
$this->__arrayOfData[$propertyName] = &$nested->$propertyName; |
64
|
99 |
|
} |
65
|
99 |
|
|
66
|
|
|
protected $__additionalPropertyNames; |
67
|
|
|
|
68
|
|
|
public function addAdditionalPropertyName($name) |
69
|
|
|
{ |
70
|
|
|
$this->__additionalPropertyNames[$name] = true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string[] |
75
|
|
|
*/ |
76
|
|
|
public function getAdditionalPropertyNames() |
77
|
|
|
{ |
78
|
|
|
if (null === $this->__additionalPropertyNames) { |
79
|
|
|
return []; |
80
|
785 |
|
} |
81
|
|
|
return array_keys($this->__additionalPropertyNames); |
82
|
785 |
|
} |
83
|
1 |
|
|
84
|
1 |
|
/** |
85
|
1 |
|
* @var string[][] |
86
|
1 |
|
*/ |
87
|
|
|
protected $__patternPropertyNames; |
88
|
|
|
|
89
|
1 |
|
public function addPatternPropertyName($pattern, $name) |
90
|
|
|
{ |
91
|
784 |
|
$this->__patternPropertyNames[$pattern][$name] = true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $pattern |
96
|
|
|
* @return null|string[] |
97
|
|
|
*/ |
98
|
|
|
public function getPatternPropertyNames($pattern) |
99
|
|
|
{ |
100
|
|
|
if (isset($this->__patternPropertyNames[$pattern])) { |
101
|
|
|
return array_keys($this->__patternPropertyNames[$pattern]); |
102
|
|
|
} |
103
|
3250 |
|
return null; |
104
|
|
|
} |
105
|
3250 |
|
|
106
|
3250 |
|
/** |
107
|
|
|
* @return string[] |
108
|
|
|
*/ |
109
|
|
|
public function getAllPatternPropertyNames() |
110
|
|
|
{ |
111
|
|
|
$result = array(); |
112
|
|
|
if (isset($this->__patternPropertyNames)) { |
113
|
|
|
foreach ($this->__patternPropertyNames as $patternProperties) { |
114
|
|
|
if (is_array($patternProperties)) { |
115
|
|
|
$result = array_merge($result, array_keys($patternProperties)); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
return $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function jsonSerialize() |
123
|
|
|
{ |
124
|
|
|
if ($this->__nestedObjects) { |
|
|
|
|
125
|
448 |
|
$result = $this->__arrayOfData; |
126
|
|
|
foreach ($this->__nestedObjects as $object) { |
127
|
448 |
|
foreach ($object->toArray() as $key => $value) { |
128
|
|
|
$result[$key] = $value; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
return (object)$result; |
132
|
|
|
} else { |
133
|
|
|
return (object)$this->__arrayOfData; |
134
|
|
|
} |
135
|
422 |
|
} |
136
|
|
|
|
137
|
422 |
|
/** |
138
|
422 |
|
* @return string |
139
|
|
|
*/ |
140
|
48 |
|
public function getDocumentPath() |
141
|
48 |
|
{ |
142
|
|
|
return $this->__documentPath; |
143
|
|
|
} |
144
|
422 |
|
|
145
|
|
|
public function setDocumentPath($path) |
146
|
|
|
{ |
147
|
|
|
$this->__documentPath = $path; |
148
|
3316 |
|
return $this; |
149
|
3316 |
|
} |
150
|
3225 |
|
|
151
|
3225 |
|
/** |
152
|
391 |
|
* @return string|null |
153
|
391 |
|
* @deprecated use ObjectItemContract::getFromRefs |
154
|
389 |
|
* @see ObjectItemContract::getFromRefs |
155
|
|
|
* @todo remove |
156
|
|
|
* @see ObjectItemContract::getFromRef |
157
|
|
|
*/ |
158
|
|
|
public function getFromRef() |
159
|
3316 |
|
{ |
160
|
|
|
return null === $this->__fromRef ? null : $this->__fromRef[0]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return string[]|null |
165
|
|
|
* @see ObjectItemContract::getFromRef |
166
|
|
|
*/ |
167
|
|
|
public function getFromRefs() |
168
|
|
|
{ |
169
|
|
|
return $this->__fromRef; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param false|string $ref |
174
|
|
|
* @return $this |
175
|
|
|
* @see ObjectItemContract::setFromRef |
176
|
|
|
*/ |
177
|
|
|
public function setFromRef($ref) |
178
|
|
|
{ |
179
|
|
|
if (null === $this->__fromRef) { |
180
|
|
|
$this->__fromRef = array($ref); |
|
|
|
|
181
|
|
|
} else { |
182
|
|
|
if (false !== $this->__fromRef[0]) { |
|
|
|
|
183
|
|
|
$this->__fromRef[] = $ref; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
private $__refPath; |
190
|
|
|
|
191
|
|
|
protected function getFromRefPath() |
192
|
|
|
{ |
193
|
|
|
if ($this->__refPath === null) { |
194
|
|
|
$this->__refPath = ''; |
195
|
|
|
if ($this->__fromRef) { |
196
|
|
|
foreach ($this->__fromRef as $ref) { |
197
|
|
|
if ($ref) { |
198
|
|
|
$this->__refPath = '->$ref[' . strtr($ref, array('~' => '~1', ':' => '~2')) . ']' . $this->__refPath; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
return $this->__refPath; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|