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) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
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 | public function jsonSerialize() |
|
107 | { |
||
108 | if ($this->__nestedObjects) { |
||
0 ignored issues
–
show
The expression
$this->__nestedObjects of type Swaggest\JsonSchema\Structure\ObjectItemContract[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
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
Loading history...
|
|||
109 | $result = $this->__arrayOfData; |
||
110 | foreach ($this->__nestedObjects as $object) { |
||
111 | foreach ($object->toArray() as $key => $value) { |
||
112 | $result[$key] = $value; |
||
113 | } |
||
114 | } |
||
115 | return (object)$result; |
||
116 | } else { |
||
117 | return (object)$this->__arrayOfData; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getDocumentPath() |
||
125 | 448 | { |
|
126 | return $this->__documentPath; |
||
127 | 448 | } |
|
128 | |||
129 | public function setDocumentPath($path) |
||
130 | { |
||
131 | $this->__documentPath = $path; |
||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | 422 | /** |
|
136 | * @return string|null |
||
137 | 422 | * @deprecated use ObjectItemContract::getFromRefs |
|
138 | 422 | * @see ObjectItemContract::getFromRefs |
|
139 | * @todo remove |
||
140 | 48 | * @see ObjectItemContract::getFromRef |
|
141 | 48 | */ |
|
142 | public function getFromRef() |
||
143 | { |
||
144 | 422 | return null === $this->__fromRef ? null : $this->__fromRef[0]; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | 3316 | * @return string[]|null |
|
149 | 3316 | * @see ObjectItemContract::getFromRef |
|
150 | 3225 | */ |
|
151 | 3225 | public function getFromRefs() |
|
152 | 391 | { |
|
153 | 391 | return $this->__fromRef; |
|
154 | 389 | } |
|
155 | |||
156 | /** |
||
157 | * @param false|string $ref |
||
158 | * @return $this |
||
159 | 3316 | * @see ObjectItemContract::setFromRef |
|
160 | */ |
||
161 | public function setFromRef($ref) |
||
162 | { |
||
163 | if (null === $this->__fromRef) { |
||
164 | $this->__fromRef = array($ref); |
||
0 ignored issues
–
show
array($ref) is of type array<integer,false|string> , but the property $__fromRef was declared to be of type null|string[] . Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly. Either this assignment is in error or an instanceof check should be added for that assignment. class Alien {}
class Dalek extends Alien {}
class Plot
{
/** @var Dalek */
public $villain;
}
$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
$plot->villain = $alien;
}
Loading history...
|
|||
165 | } else { |
||
166 | if (false !== $this->__fromRef[0]) { |
||
0 ignored issues
–
show
|
|||
167 | $this->__fromRef[] = $ref; |
||
168 | } |
||
169 | } |
||
170 | return $this; |
||
171 | } |
||
172 | |||
173 | private $__refPath; |
||
174 | |||
175 | protected function getFromRefPath() |
||
176 | { |
||
177 | if ($this->__refPath === null) { |
||
178 | $this->__refPath = ''; |
||
179 | if ($this->__fromRef) { |
||
180 | foreach ($this->__fromRef as $ref) { |
||
181 | if ($ref) { |
||
182 | $this->__refPath = '->$ref[' . strtr($ref, array('~' => '~1', ':' => '~2')) . ']' . $this->__refPath; |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | return $this->__refPath; |
||
188 | } |
||
189 | } |
||
190 |