1 | <?php |
||
12 | class JsonDefinitionHash implements DefinitionElementInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var string Name of this hash |
||
16 | */ |
||
17 | private $name; |
||
18 | /** |
||
19 | * @var JsonDefinition |
||
20 | */ |
||
21 | private $parent; |
||
22 | /** |
||
23 | * @var DefinitionElementInterface[] Array of fields.. |
||
24 | */ |
||
25 | private $fields = []; |
||
26 | /** |
||
27 | * @var Schema\Field Field definition |
||
28 | */ |
||
29 | private $definition; |
||
30 | |||
31 | /** |
||
32 | * Constructor |
||
33 | * |
||
34 | * @param string $name Name of this hash |
||
35 | * @param JsonDefinition $parent Parent definiton |
||
36 | * @param DefinitionElementInterface[] $fields Fields of the hash |
||
37 | * @param Schema\Field $definition Field definition |
||
|
|||
38 | 42 | */ |
|
39 | public function __construct($name, JsonDefinition $parent, array $fields, Schema\Field $definition = null) |
||
46 | |||
47 | /** |
||
48 | * Returns the hash name |
||
49 | * |
||
50 | * @return string Name |
||
51 | 22 | */ |
|
52 | public function getName() |
||
56 | |||
57 | /** |
||
58 | * Returns the definition as array.. |
||
59 | * |
||
60 | * @return array the definition |
||
61 | 6 | */ |
|
62 | public function getDefAsArray() |
||
93 | |||
94 | /** |
||
95 | * {@inheritDoc} |
||
96 | * |
||
97 | 8 | * @return string type |
|
98 | */ |
||
99 | 8 | public function getType() |
|
103 | |||
104 | /** |
||
105 | * Whether this hash is anonymous, so has no own field definition (properly only defined |
||
106 | * by definitions such as "object.field", not an own definition) |
||
107 | 8 | * |
|
108 | * @return bool true if yes, false otherwise |
||
109 | 8 | */ |
|
110 | public function isAnonymous() |
||
111 | { |
||
112 | return ($this->definition === null); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * in an 'anonymous' hash situation, we will check if any children are required |
||
117 | 8 | * |
|
118 | * @return bool if required or not |
||
119 | 8 | */ |
|
120 | public function getRequired() |
||
121 | { |
||
122 | $isRequired = false; |
||
123 | |||
124 | // see if on the first level of fields we have a required=true in the definition |
||
125 | foreach ($this->fields as $field) { |
||
126 | if ($field instanceof JsonDefinitionField && |
||
127 | $field->getDef() instanceof Field && |
||
128 | $field->getDef()->getRequired() === true |
||
129 | ) { |
||
130 | 10 | $isRequired = true; |
|
131 | } |
||
132 | 10 | } |
|
133 | 10 | ||
134 | 10 | return $isRequired; |
|
135 | 10 | } |
|
136 | 10 | ||
137 | 10 | /** |
|
138 | * {@inheritDoc} |
||
139 | 10 | * |
|
140 | 10 | * @return string type |
|
141 | 10 | */ |
|
142 | 5 | public function getTypeDoctrine() |
|
146 | 4 | ||
147 | 4 | /** |
|
148 | 2 | * Returns the field type in a serializer-understandable way.. |
|
149 | 5 | * |
|
150 | * @return string Type |
||
151 | 10 | */ |
|
152 | public function getTypeSerializer() |
||
156 | |||
157 | /** |
||
158 | * Returns the field definition of this hash from "local perspective", |
||
159 | * meaning that we only include fields inside this hash BUT with all |
||
160 | 10 | * the stuff from the json file. this is needed to generate a Document/Model |
|
161 | * from this hash (generate a json file again) |
||
162 | 10 | * |
|
163 | 10 | * @return JsonDefinition the definition of this hash in a standalone array ready to be json_encoded() |
|
164 | 4 | */ |
|
165 | 8 | public function getJsonDefinition() |
|
166 | 2 | { |
|
167 | 4 | $definition = (new Schema\Definition()) |
|
168 | 4 | ->setId($this->getClassName()) |
|
169 | 4 | ->setDescription($this->definition === null ? null : $this->definition->getDescription()) |
|
170 | 4 | ->setTitle($this->definition === null ? null : $this->definition->getTitle()) |
|
171 | 4 | ->setIsSubDocument(true) |
|
172 | 4 | ->setTarget(new Schema\Target()); |
|
173 | 2 | ||
174 | foreach ($this->fields as $field) { |
||
175 | foreach ($this->processFieldDefinitionsRecursive($field) as $definitions) { |
||
176 | $definition->getTarget()->addField($definitions); |
||
177 | } |
||
178 | } |
||
179 | foreach ($this->parent->getRelations() as $relation) { |
||
180 | $relation = $this->processParentRelation($relation); |
||
181 | if ($relation !== null) { |
||
182 | $definition->getTarget()->addRelation($relation); |
||
183 | } |
||
184 | } |
||
185 | 10 | ||
186 | return new JsonDefinition($definition); |
||
187 | 10 | } |
|
188 | 10 | ||
189 | 10 | /** |
|
190 | * Method getFieldDefinitionsRecursive |
||
191 | * |
||
192 | * @param DefinitionElementInterface $field |
||
193 | * @return Schema\Field[] |
||
194 | */ |
||
195 | private function processFieldDefinitionsRecursive(DefinitionElementInterface $field) |
||
196 | { |
||
197 | if ($field instanceof JsonDefinitionField) { |
||
198 | 4 | return [$this->cloneFieldDefinition($field->getDef())]; |
|
199 | } elseif ($field instanceof JsonDefinitionArray) { |
||
200 | 4 | return $this->processFieldDefinitionsRecursive($field->getElement()); |
|
201 | 4 | } elseif ($field instanceof JsonDefinitionHash) { |
|
202 | 2 | return array_reduce( |
|
203 | $field->fields, |
||
204 | function (array $subfields, DefinitionElementInterface $subfield) { |
||
205 | 4 | return array_merge($subfields, $this->processFieldDefinitionsRecursive($subfield)); |
|
206 | 4 | }, |
|
207 | 4 | $field->definition === null ? [] : [$this->cloneFieldDefinition($field->definition)] |
|
208 | ); |
||
209 | } |
||
210 | |||
211 | throw new \InvalidArgumentException(sprintf('Unknown field type "%s"', get_class($field))); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Clone field definition |
||
216 | * |
||
217 | * @param Schema\Field $field Field |
||
218 | * @return Schema\Field |
||
219 | 20 | */ |
|
220 | private function cloneFieldDefinition(Schema\Field $field) |
||
221 | 20 | { |
|
222 | 20 | $clone = clone $field; |
|
223 | 10 | $clone->setName(preg_replace('/^'.preg_quote($this->name, '/').'\.(\d+\.)*/', '', $clone->getName())); |
|
224 | 5 | return $clone; |
|
225 | } |
||
226 | 20 | ||
227 | /** |
||
228 | * Process parent relation |
||
229 | * |
||
230 | * @param Schema\Relation $relation Parent relation |
||
231 | * @return Schema\Relation|null |
||
232 | */ |
||
233 | private function processParentRelation(Schema\Relation $relation) |
||
244 | |||
245 | /** |
||
246 | * Returns the class name of this hash, possibly |
||
247 | * taking the parent element into the name. this |
||
248 | * string here results in the name of the generated Document. |
||
249 | * |
||
250 | * @param boolean $fq if true, we'll return the class name full qualified |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | private function getClassName($fq = false) |
||
263 | } |
||
264 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.