-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathObjectItemTrait.php
190 lines (166 loc) · 4.6 KB
/
ObjectItemTrait.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
<?php
namespace Swaggest\JsonSchema\Structure;
use Swaggest\JsonSchema\MagicMapTrait;
/**
* Trait ObjectItemTrait
* @package Swaggest\JsonSchema\Structure
* @see ObjectItemContract
*/
trait ObjectItemTrait
{
use MagicMapTrait;
/** @var ObjectItemContract[] */
protected $__nestedObjects;
protected $__documentPath;
/** @var null|string[] */
protected $__fromRef;
protected $__hasResolvedValue = false;
protected $__resolvedValue;
public function setResolvedValue($value)
{
$this->__hasResolvedValue = true;
$this->__resolvedValue = $value;
return $this;
}
public function getResolvedValue()
{
return $this->__resolvedValue;
}
public function hasResolvedValue()
{
return $this->__hasResolvedValue;
}
public function getNestedObject($className)
{
if (isset($this->__nestedObjects[$className])) {
return $this->__nestedObjects[$className];
}
return null;
}
public function setNestedProperty($propertyName, $value, Egg $nestedEgg)
{
$nestedName = $nestedEgg->name;
/** @var null $nested */
$nested = &$this->__nestedObjects[$nestedName];
if (null === $nested) {
$nested = $nestedEgg->classSchema->makeObjectItem();
$this->__nestedObjects[$nestedName] = $nested;
if ($nestedName !== $nestedEgg->classSchema->getObjectItemClass()) {
$this->$nestedName = $nested;
}
}
$nested->$propertyName = $value;
$this->__arrayOfData[$propertyName] = &$nested->$propertyName;
}
protected $__additionalPropertyNames;
public function addAdditionalPropertyName($name)
{
$this->__additionalPropertyNames[$name] = true;
}
/**
* @return string[]
*/
public function getAdditionalPropertyNames()
{
if (null === $this->__additionalPropertyNames) {
return [];
}
return array_keys($this->__additionalPropertyNames);
}
/**
* @var string[][]
*/
protected $__patternPropertyNames;
public function addPatternPropertyName($pattern, $name)
{
$this->__patternPropertyNames[$pattern][$name] = true;
}
/**
* @param string $pattern
* @return null|string[]
*/
public function getPatternPropertyNames($pattern)
{
if (isset($this->__patternPropertyNames[$pattern])) {
return array_keys($this->__patternPropertyNames[$pattern]);
}
return null;
}
#[\ReturnTypeWillChange]
public function jsonSerialize(): mixed
{
if ($this->__nestedObjects) {
$result = $this->__arrayOfData;
foreach ($this->__nestedObjects as $object) {
foreach ($object->toArray() as $key => $value) {
$result[$key] = $value;
}
}
return (object)$result;
} else {
return (object)$this->__arrayOfData;
}
}
/**
* @return string
*/
public function getDocumentPath()
{
return $this->__documentPath;
}
public function setDocumentPath($path)
{
$this->__documentPath = $path;
return $this;
}
/**
* @return string|null
* @deprecated use ObjectItemContract::getFromRefs
* @see ObjectItemContract::getFromRefs
* @todo remove
* @see ObjectItemContract::getFromRef
*/
public function getFromRef()
{
return null === $this->__fromRef ? null : $this->__fromRef[0];
}
/**
* @return string[]|null
* @see ObjectItemContract::getFromRef
*/
public function getFromRefs()
{
return $this->__fromRef;
}
/**
* @param false|string $ref
* @return $this
* @see ObjectItemContract::setFromRef
*/
public function setFromRef($ref)
{
if (null === $this->__fromRef) {
$this->__fromRef = array($ref);
} else {
if (false !== $this->__fromRef[0]) {
$this->__fromRef[] = $ref;
}
}
return $this;
}
private $__refPath;
protected function getFromRefPath()
{
if ($this->__refPath === null) {
$this->__refPath = '';
if ($this->__fromRef) {
foreach ($this->__fromRef as $ref) {
if ($ref) {
$this->__refPath = '->$ref[' . strtr($ref, array('~' => '~1', ':' => '~2')) . ']' . $this->__refPath;
}
}
}
}
return $this->__refPath;
}
}