These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
14 | * |
||
15 | * This software consists of voluntary contributions made by many individuals |
||
16 | * and is licensed under the MIT license. For more information, see |
||
17 | * <http://www.doctrine-project.org>. |
||
18 | */ |
||
19 | |||
20 | namespace Doctrine\ORM\Mapping\Driver; |
||
21 | |||
22 | use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
||
23 | use Doctrine\ORM\Mapping\Builder\EntityListenerBuilder; |
||
24 | use Doctrine\Common\Persistence\Mapping\Driver\FileDriver; |
||
25 | use Doctrine\ORM\Mapping\ClassMetadata as Metadata; |
||
26 | use Doctrine\ORM\Mapping\MappingException; |
||
27 | use Symfony\Component\Yaml\Yaml; |
||
28 | |||
29 | /** |
||
30 | * The YamlDriver reads the mapping metadata from yaml schema files. |
||
31 | * |
||
32 | * @since 2.0 |
||
33 | * @author Benjamin Eberlei <[email protected]> |
||
34 | * @author Guilherme Blanco <[email protected]> |
||
35 | * @author Jonathan H. Wage <[email protected]> |
||
36 | * @author Roman Borschel <[email protected]> |
||
37 | */ |
||
38 | class YamlDriver extends FileDriver |
||
39 | { |
||
40 | const DEFAULT_FILE_EXTENSION = '.dcm.yml'; |
||
41 | |||
42 | /** |
||
43 | * {@inheritDoc} |
||
44 | */ |
||
45 | 71 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
46 | { |
||
47 | 71 | parent::__construct($locator, $fileExtension); |
|
48 | 71 | } |
|
49 | |||
50 | /** |
||
51 | * {@inheritDoc} |
||
52 | */ |
||
53 | 66 | public function loadMetadataForClass($className, ClassMetadata $metadata) |
|
54 | { |
||
55 | /* @var $metadata \Doctrine\ORM\Mapping\ClassMetadataInfo */ |
||
56 | 66 | $element = $this->getElement($className); |
|
57 | |||
58 | 64 | if ($element['type'] == 'entity') { |
|
59 | 62 | if (isset($element['repositoryClass'])) { |
|
60 | $metadata->setCustomRepositoryClass($element['repositoryClass']); |
||
61 | } |
||
62 | 62 | if (isset($element['readOnly']) && $element['readOnly'] == true) { |
|
63 | 62 | $metadata->markReadOnly(); |
|
64 | } |
||
65 | 16 | } else if ($element['type'] == 'mappedSuperclass') { |
|
66 | 14 | $metadata->setCustomRepositoryClass( |
|
67 | 14 | isset($element['repositoryClass']) ? $element['repositoryClass'] : null |
|
68 | ); |
||
69 | 14 | $metadata->isMappedSuperclass = true; |
|
70 | 2 | View Code Duplication | } else if ($element['type'] == 'embeddable') { |
71 | $metadata->isEmbeddedClass = true; |
||
72 | } else { |
||
73 | 2 | throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className); |
|
74 | } |
||
75 | |||
76 | // Evaluate root level properties |
||
77 | 62 | $primaryTable = []; |
|
78 | |||
79 | 62 | if (isset($element['table'])) { |
|
80 | 26 | $primaryTable['name'] = $element['table']; |
|
81 | } |
||
82 | |||
83 | 62 | if (isset($element['schema'])) { |
|
84 | 2 | $primaryTable['schema'] = $element['schema']; |
|
85 | } |
||
86 | |||
87 | // Evaluate second level cache |
||
88 | 62 | if (isset($element['cache'])) { |
|
89 | 2 | $metadata->enableCache($this->cacheToArray($element['cache'])); |
|
90 | } |
||
91 | |||
92 | 62 | $metadata->setPrimaryTable($primaryTable); |
|
93 | |||
94 | // Evaluate named queries |
||
95 | 62 | if (isset($element['namedQueries'])) { |
|
96 | 8 | foreach ($element['namedQueries'] as $name => $queryMapping) { |
|
97 | 8 | if (is_string($queryMapping)) { |
|
98 | 8 | $queryMapping = ['query' => $queryMapping]; |
|
99 | } |
||
100 | |||
101 | 8 | if ( ! isset($queryMapping['name'])) { |
|
102 | 8 | $queryMapping['name'] = $name; |
|
103 | } |
||
104 | |||
105 | 8 | $metadata->addNamedQuery($queryMapping); |
|
106 | } |
||
107 | } |
||
108 | |||
109 | // Evaluate named native queries |
||
110 | 62 | if (isset($element['namedNativeQueries'])) { |
|
111 | 6 | foreach ($element['namedNativeQueries'] as $name => $mappingElement) { |
|
112 | 6 | if (!isset($mappingElement['name'])) { |
|
113 | 6 | $mappingElement['name'] = $name; |
|
114 | } |
||
115 | 6 | $metadata->addNamedNativeQuery( |
|
116 | [ |
||
117 | 6 | 'name' => $mappingElement['name'], |
|
118 | 6 | 'query' => isset($mappingElement['query']) ? $mappingElement['query'] : null, |
|
119 | 6 | 'resultClass' => isset($mappingElement['resultClass']) ? $mappingElement['resultClass'] : null, |
|
120 | 6 | 'resultSetMapping' => isset($mappingElement['resultSetMapping']) ? $mappingElement['resultSetMapping'] : null, |
|
121 | ] |
||
122 | ); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | // Evaluate sql result set mappings |
||
127 | 62 | if (isset($element['sqlResultSetMappings'])) { |
|
128 | 6 | foreach ($element['sqlResultSetMappings'] as $name => $resultSetMapping) { |
|
129 | 6 | if (!isset($resultSetMapping['name'])) { |
|
130 | 6 | $resultSetMapping['name'] = $name; |
|
131 | } |
||
132 | |||
133 | 6 | $entities = []; |
|
134 | 6 | $columns = []; |
|
135 | 6 | if (isset($resultSetMapping['entityResult'])) { |
|
136 | 6 | foreach ($resultSetMapping['entityResult'] as $entityResultElement) { |
|
137 | $entityResult = [ |
||
138 | 6 | 'fields' => [], |
|
139 | 6 | 'entityClass' => isset($entityResultElement['entityClass']) ? $entityResultElement['entityClass'] : null, |
|
140 | 6 | 'discriminatorColumn' => isset($entityResultElement['discriminatorColumn']) ? $entityResultElement['discriminatorColumn'] : null, |
|
141 | ]; |
||
142 | |||
143 | 6 | if (isset($entityResultElement['fieldResult'])) { |
|
144 | 6 | View Code Duplication | foreach ($entityResultElement['fieldResult'] as $fieldResultElement) { |
145 | 6 | $entityResult['fields'][] = [ |
|
146 | 6 | 'name' => isset($fieldResultElement['name']) ? $fieldResultElement['name'] : null, |
|
147 | 6 | 'column' => isset($fieldResultElement['column']) ? $fieldResultElement['column'] : null, |
|
148 | ]; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | 6 | $entities[] = $entityResult; |
|
153 | } |
||
154 | } |
||
155 | |||
156 | |||
157 | 6 | if (isset($resultSetMapping['columnResult'])) { |
|
158 | 6 | foreach ($resultSetMapping['columnResult'] as $columnResultAnnot) { |
|
159 | 6 | $columns[] = [ |
|
160 | 6 | 'name' => isset($columnResultAnnot['name']) ? $columnResultAnnot['name'] : null, |
|
161 | ]; |
||
162 | } |
||
163 | } |
||
164 | |||
165 | 6 | $metadata->addSqlResultSetMapping( |
|
166 | [ |
||
167 | 6 | 'name' => $resultSetMapping['name'], |
|
168 | 6 | 'entities' => $entities, |
|
169 | 6 | 'columns' => $columns |
|
170 | ] |
||
171 | ); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | 62 | if (isset($element['inheritanceType'])) { |
|
176 | 18 | $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType']))); |
|
177 | |||
178 | 18 | if ($metadata->inheritanceType != Metadata::INHERITANCE_TYPE_NONE) { |
|
179 | // Evaluate discriminatorColumn |
||
180 | 18 | View Code Duplication | if (isset($element['discriminatorColumn'])) { |
181 | 12 | $discrColumn = $element['discriminatorColumn']; |
|
182 | 12 | $metadata->setDiscriminatorColumn( |
|
183 | [ |
||
184 | 12 | 'name' => isset($discrColumn['name']) ? (string) $discrColumn['name'] : null, |
|
185 | 12 | 'type' => isset($discrColumn['type']) ? (string) $discrColumn['type'] : 'string', |
|
186 | 12 | 'length' => isset($discrColumn['length']) ? (string) $discrColumn['length'] : 255, |
|
187 | 12 | 'columnDefinition' => isset($discrColumn['columnDefinition']) ? (string) $discrColumn['columnDefinition'] : null |
|
188 | ] |
||
189 | ); |
||
190 | } else { |
||
191 | 12 | $metadata->setDiscriminatorColumn(['name' => 'dtype', 'type' => 'string', 'length' => 255]); |
|
192 | } |
||
193 | |||
194 | // Evaluate discriminatorMap |
||
195 | 18 | if (isset($element['discriminatorMap'])) { |
|
196 | 18 | $metadata->setDiscriminatorMap($element['discriminatorMap']); |
|
197 | } |
||
198 | } |
||
199 | } |
||
200 | |||
201 | |||
202 | // Evaluate changeTrackingPolicy |
||
203 | 62 | View Code Duplication | if (isset($element['changeTrackingPolicy'])) { |
204 | $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_' |
||
205 | . strtoupper($element['changeTrackingPolicy']))); |
||
206 | } |
||
207 | |||
208 | // Evaluate indexes |
||
209 | 62 | if (isset($element['indexes'])) { |
|
210 | 10 | foreach ($element['indexes'] as $name => $indexYml) { |
|
211 | 10 | if ( ! isset($indexYml['name'])) { |
|
212 | 10 | $indexYml['name'] = $name; |
|
213 | } |
||
214 | |||
215 | 10 | View Code Duplication | if (is_string($indexYml['columns'])) { |
216 | 10 | $index = ['columns' => array_map('trim', explode(',', $indexYml['columns']))]; |
|
217 | } else { |
||
218 | $index = ['columns' => $indexYml['columns']]; |
||
219 | } |
||
220 | |||
221 | 10 | if (isset($indexYml['flags'])) { |
|
222 | 2 | if (is_string($indexYml['flags'])) { |
|
223 | 2 | $index['flags'] = array_map('trim', explode(',', $indexYml['flags'])); |
|
224 | } else { |
||
225 | $index['flags'] = $indexYml['flags']; |
||
226 | } |
||
227 | } |
||
228 | |||
229 | 10 | if (isset($indexYml['options'])) { |
|
230 | 2 | $index['options'] = $indexYml['options']; |
|
231 | } |
||
232 | |||
233 | 10 | $metadata->table['indexes'][$indexYml['name']] = $index; |
|
234 | } |
||
235 | } |
||
236 | |||
237 | // Evaluate uniqueConstraints |
||
238 | 62 | if (isset($element['uniqueConstraints'])) { |
|
239 | 9 | foreach ($element['uniqueConstraints'] as $name => $uniqueYml) { |
|
240 | 9 | if ( ! isset($uniqueYml['name'])) { |
|
241 | 9 | $uniqueYml['name'] = $name; |
|
242 | } |
||
243 | |||
244 | 9 | View Code Duplication | if (is_string($uniqueYml['columns'])) { |
245 | 8 | $unique = ['columns' => array_map('trim', explode(',', $uniqueYml['columns']))]; |
|
246 | } else { |
||
247 | 1 | $unique = ['columns' => $uniqueYml['columns']]; |
|
248 | } |
||
249 | |||
250 | 9 | if (isset($uniqueYml['options'])) { |
|
251 | 6 | $unique['options'] = $uniqueYml['options']; |
|
252 | } |
||
253 | |||
254 | 9 | $metadata->table['uniqueConstraints'][$uniqueYml['name']] = $unique; |
|
255 | } |
||
256 | } |
||
257 | |||
258 | 62 | if (isset($element['options'])) { |
|
259 | 8 | $metadata->table['options'] = $element['options']; |
|
260 | } |
||
261 | |||
262 | 62 | $associationIds = []; |
|
263 | 62 | if (isset($element['id'])) { |
|
264 | // Evaluate identifier settings |
||
265 | 58 | foreach ($element['id'] as $name => $idElement) { |
|
266 | 58 | if (isset($idElement['associationKey']) && $idElement['associationKey'] == true) { |
|
267 | $associationIds[$name] = true; |
||
268 | continue; |
||
269 | } |
||
270 | |||
271 | $mapping = [ |
||
272 | 58 | 'id' => true, |
|
273 | 58 | 'fieldName' => $name |
|
274 | ]; |
||
275 | |||
276 | 58 | if (isset($idElement['type'])) { |
|
277 | 38 | $mapping['type'] = $idElement['type']; |
|
278 | } |
||
279 | |||
280 | 58 | if (isset($idElement['column'])) { |
|
281 | 8 | $mapping['columnName'] = $idElement['column']; |
|
282 | } |
||
283 | |||
284 | 58 | if (isset($idElement['length'])) { |
|
285 | 6 | $mapping['length'] = $idElement['length']; |
|
286 | } |
||
287 | |||
288 | 58 | if (isset($idElement['columnDefinition'])) { |
|
289 | 2 | $mapping['columnDefinition'] = $idElement['columnDefinition']; |
|
290 | } |
||
291 | |||
292 | 58 | if (isset($idElement['options'])) { |
|
293 | 6 | $mapping['options'] = $idElement['options']; |
|
294 | } |
||
295 | |||
296 | 58 | $metadata->mapField($mapping); |
|
297 | |||
298 | 58 | View Code Duplication | if (isset($idElement['generator'])) { |
299 | 55 | $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' |
|
300 | 55 | . strtoupper($idElement['generator']['strategy']))); |
|
301 | } |
||
302 | // Check for SequenceGenerator/TableGenerator definition |
||
303 | 58 | if (isset($idElement['sequenceGenerator'])) { |
|
304 | 6 | $metadata->setSequenceGeneratorDefinition($idElement['sequenceGenerator']); |
|
305 | 52 | } else if (isset($idElement['customIdGenerator'])) { |
|
306 | 4 | $customGenerator = $idElement['customIdGenerator']; |
|
307 | 4 | $metadata->setCustomGeneratorDefinition( |
|
308 | [ |
||
309 | 4 | 'class' => (string) $customGenerator['class'] |
|
310 | ] |
||
311 | ); |
||
312 | 48 | } else if (isset($idElement['tableGenerator'])) { |
|
313 | 58 | throw MappingException::tableIdGeneratorNotImplemented($className); |
|
314 | } |
||
315 | } |
||
316 | } |
||
317 | |||
318 | // Evaluate fields |
||
319 | 62 | if (isset($element['fields'])) { |
|
320 | 43 | foreach ($element['fields'] as $name => $fieldMapping) { |
|
321 | |||
322 | 43 | $mapping = $this->columnToArray($name, $fieldMapping); |
|
323 | |||
324 | 43 | if (isset($fieldMapping['id'])) { |
|
325 | $mapping['id'] = true; |
||
326 | View Code Duplication | if (isset($fieldMapping['generator']['strategy'])) { |
|
327 | $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' |
||
328 | . strtoupper($fieldMapping['generator']['strategy']))); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | 43 | if (isset($mapping['version'])) { |
|
333 | 6 | $metadata->setVersionMapping($mapping); |
|
334 | 6 | unset($mapping['version']); |
|
335 | } |
||
336 | |||
337 | 43 | $metadata->mapField($mapping); |
|
338 | } |
||
339 | } |
||
340 | |||
341 | 62 | if (isset($element['embedded'])) { |
|
342 | foreach ($element['embedded'] as $name => $embeddedMapping) { |
||
343 | $mapping = [ |
||
344 | 'fieldName' => $name, |
||
345 | 'class' => $embeddedMapping['class'], |
||
346 | 'columnPrefix' => isset($embeddedMapping['columnPrefix']) ? $embeddedMapping['columnPrefix'] : null, |
||
347 | ]; |
||
348 | $metadata->mapEmbedded($mapping); |
||
349 | } |
||
350 | } |
||
351 | |||
352 | // Evaluate oneToOne relationships |
||
353 | 62 | if (isset($element['oneToOne'])) { |
|
354 | 15 | foreach ($element['oneToOne'] as $name => $oneToOneElement) { |
|
355 | $mapping = [ |
||
356 | 15 | 'fieldName' => $name, |
|
357 | 15 | 'targetEntity' => $oneToOneElement['targetEntity'] |
|
358 | ]; |
||
359 | |||
360 | 15 | if (isset($associationIds[$mapping['fieldName']])) { |
|
361 | $mapping['id'] = true; |
||
362 | } |
||
363 | |||
364 | 15 | View Code Duplication | if (isset($oneToOneElement['fetch'])) { |
365 | 3 | $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToOneElement['fetch']); |
|
366 | } |
||
367 | |||
368 | 15 | if (isset($oneToOneElement['mappedBy'])) { |
|
369 | 3 | $mapping['mappedBy'] = $oneToOneElement['mappedBy']; |
|
370 | } else { |
||
371 | 14 | if (isset($oneToOneElement['inversedBy'])) { |
|
372 | 14 | $mapping['inversedBy'] = $oneToOneElement['inversedBy']; |
|
373 | } |
||
374 | |||
375 | 14 | $joinColumns = []; |
|
376 | |||
377 | 14 | View Code Duplication | if (isset($oneToOneElement['joinColumn'])) { |
378 | 13 | $joinColumns[] = $this->joinColumnToArray($oneToOneElement['joinColumn']); |
|
379 | 1 | } else if (isset($oneToOneElement['joinColumns'])) { |
|
380 | 1 | foreach ($oneToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) { |
|
381 | 1 | if ( ! isset($joinColumnElement['name'])) { |
|
382 | 1 | $joinColumnElement['name'] = $joinColumnName; |
|
383 | } |
||
384 | |||
385 | 1 | $joinColumns[] = $this->joinColumnToArray($joinColumnElement); |
|
386 | } |
||
387 | } |
||
388 | |||
389 | 14 | $mapping['joinColumns'] = $joinColumns; |
|
390 | } |
||
391 | |||
392 | 15 | if (isset($oneToOneElement['cascade'])) { |
|
393 | 11 | $mapping['cascade'] = $oneToOneElement['cascade']; |
|
394 | } |
||
395 | |||
396 | 15 | if (isset($oneToOneElement['orphanRemoval'])) { |
|
397 | 5 | $mapping['orphanRemoval'] = (bool) $oneToOneElement['orphanRemoval']; |
|
398 | } |
||
399 | |||
400 | // Evaluate second level cache |
||
401 | 15 | View Code Duplication | if (isset($oneToOneElement['cache'])) { |
402 | $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToOneElement['cache'])); |
||
403 | } |
||
404 | |||
405 | 15 | $metadata->mapOneToOne($mapping); |
|
406 | } |
||
407 | } |
||
408 | |||
409 | // Evaluate oneToMany relationships |
||
410 | 62 | if (isset($element['oneToMany'])) { |
|
411 | 10 | foreach ($element['oneToMany'] as $name => $oneToManyElement) { |
|
412 | $mapping = [ |
||
413 | 10 | 'fieldName' => $name, |
|
414 | 10 | 'targetEntity' => $oneToManyElement['targetEntity'], |
|
415 | 10 | 'mappedBy' => $oneToManyElement['mappedBy'] |
|
416 | ]; |
||
417 | |||
418 | 10 | View Code Duplication | if (isset($oneToManyElement['fetch'])) { |
419 | 2 | $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToManyElement['fetch']); |
|
420 | } |
||
421 | |||
422 | 10 | if (isset($oneToManyElement['cascade'])) { |
|
423 | 8 | $mapping['cascade'] = $oneToManyElement['cascade']; |
|
424 | } |
||
425 | |||
426 | 10 | if (isset($oneToManyElement['orphanRemoval'])) { |
|
427 | 8 | $mapping['orphanRemoval'] = (bool) $oneToManyElement['orphanRemoval']; |
|
428 | } |
||
429 | |||
430 | 10 | if (isset($oneToManyElement['orderBy'])) { |
|
431 | 10 | $mapping['orderBy'] = $oneToManyElement['orderBy']; |
|
432 | } |
||
433 | |||
434 | 10 | if (isset($oneToManyElement['indexBy'])) { |
|
435 | $mapping['indexBy'] = $oneToManyElement['indexBy']; |
||
436 | } |
||
437 | |||
438 | |||
439 | // Evaluate second level cache |
||
440 | 10 | View Code Duplication | if (isset($oneToManyElement['cache'])) { |
441 | 2 | $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($oneToManyElement['cache'])); |
|
442 | } |
||
443 | |||
444 | 10 | $metadata->mapOneToMany($mapping); |
|
445 | } |
||
446 | } |
||
447 | |||
448 | // Evaluate manyToOne relationships |
||
449 | 62 | if (isset($element['manyToOne'])) { |
|
450 | 10 | foreach ($element['manyToOne'] as $name => $manyToOneElement) { |
|
451 | $mapping = [ |
||
452 | 10 | 'fieldName' => $name, |
|
453 | 10 | 'targetEntity' => $manyToOneElement['targetEntity'] |
|
454 | ]; |
||
455 | |||
456 | 10 | if (isset($associationIds[$mapping['fieldName']])) { |
|
457 | $mapping['id'] = true; |
||
458 | } |
||
459 | |||
460 | 10 | View Code Duplication | if (isset($manyToOneElement['fetch'])) { |
461 | 1 | $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToOneElement['fetch']); |
|
462 | } |
||
463 | |||
464 | 10 | if (isset($manyToOneElement['inversedBy'])) { |
|
465 | 2 | $mapping['inversedBy'] = $manyToOneElement['inversedBy']; |
|
466 | } |
||
467 | |||
468 | 10 | $joinColumns = []; |
|
469 | |||
470 | 10 | View Code Duplication | if (isset($manyToOneElement['joinColumn'])) { |
471 | 4 | $joinColumns[] = $this->joinColumnToArray($manyToOneElement['joinColumn']); |
|
472 | 6 | } else if (isset($manyToOneElement['joinColumns'])) { |
|
473 | 3 | foreach ($manyToOneElement['joinColumns'] as $joinColumnName => $joinColumnElement) { |
|
474 | 3 | if ( ! isset($joinColumnElement['name'])) { |
|
475 | 3 | $joinColumnElement['name'] = $joinColumnName; |
|
476 | } |
||
477 | |||
478 | 3 | $joinColumns[] = $this->joinColumnToArray($joinColumnElement); |
|
479 | } |
||
480 | } |
||
481 | |||
482 | 10 | $mapping['joinColumns'] = $joinColumns; |
|
483 | |||
484 | 10 | if (isset($manyToOneElement['cascade'])) { |
|
485 | 5 | $mapping['cascade'] = $manyToOneElement['cascade']; |
|
486 | } |
||
487 | |||
488 | // Evaluate second level cache |
||
489 | 10 | View Code Duplication | if (isset($manyToOneElement['cache'])) { |
490 | 2 | $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToOneElement['cache'])); |
|
491 | } |
||
492 | |||
493 | 10 | $metadata->mapManyToOne($mapping); |
|
494 | } |
||
495 | } |
||
496 | |||
497 | // Evaluate manyToMany relationships |
||
498 | 62 | if (isset($element['manyToMany'])) { |
|
499 | 21 | foreach ($element['manyToMany'] as $name => $manyToManyElement) { |
|
500 | $mapping = [ |
||
501 | 21 | 'fieldName' => $name, |
|
502 | 21 | 'targetEntity' => $manyToManyElement['targetEntity'] |
|
503 | ]; |
||
504 | |||
505 | 21 | View Code Duplication | if (isset($manyToManyElement['fetch'])) { |
506 | 2 | $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToManyElement['fetch']); |
|
507 | } |
||
508 | |||
509 | 21 | if (isset($manyToManyElement['mappedBy'])) { |
|
510 | 2 | $mapping['mappedBy'] = $manyToManyElement['mappedBy']; |
|
511 | 19 | } else if (isset($manyToManyElement['joinTable'])) { |
|
512 | |||
513 | 15 | $joinTableElement = $manyToManyElement['joinTable']; |
|
514 | $joinTable = [ |
||
515 | 15 | 'name' => $joinTableElement['name'] |
|
516 | ]; |
||
517 | |||
518 | 15 | if (isset($joinTableElement['schema'])) { |
|
519 | $joinTable['schema'] = $joinTableElement['schema']; |
||
520 | } |
||
521 | |||
522 | 15 | View Code Duplication | if (isset($joinTableElement['joinColumns'])) { |
523 | 15 | foreach ($joinTableElement['joinColumns'] as $joinColumnName => $joinColumnElement) { |
|
524 | 15 | if ( ! isset($joinColumnElement['name'])) { |
|
525 | 14 | $joinColumnElement['name'] = $joinColumnName; |
|
526 | } |
||
527 | 15 | $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement); |
|
528 | } |
||
529 | } |
||
530 | |||
531 | 15 | View Code Duplication | if (isset($joinTableElement['inverseJoinColumns'])) { |
532 | 15 | foreach ($joinTableElement['inverseJoinColumns'] as $joinColumnName => $joinColumnElement) { |
|
533 | 15 | if ( ! isset($joinColumnElement['name'])) { |
|
534 | 14 | $joinColumnElement['name'] = $joinColumnName; |
|
535 | } |
||
536 | 15 | $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement); |
|
537 | } |
||
538 | } |
||
539 | |||
540 | 15 | $mapping['joinTable'] = $joinTable; |
|
541 | } |
||
542 | |||
543 | 21 | if (isset($manyToManyElement['inversedBy'])) { |
|
544 | 2 | $mapping['inversedBy'] = $manyToManyElement['inversedBy']; |
|
545 | } |
||
546 | |||
547 | 21 | if (isset($manyToManyElement['cascade'])) { |
|
548 | 14 | $mapping['cascade'] = $manyToManyElement['cascade']; |
|
549 | } |
||
550 | |||
551 | 21 | if (isset($manyToManyElement['orderBy'])) { |
|
552 | $mapping['orderBy'] = $manyToManyElement['orderBy']; |
||
553 | } |
||
554 | |||
555 | 21 | if (isset($manyToManyElement['indexBy'])) { |
|
556 | $mapping['indexBy'] = $manyToManyElement['indexBy']; |
||
557 | } |
||
558 | |||
559 | 21 | if (isset($manyToManyElement['orphanRemoval'])) { |
|
560 | $mapping['orphanRemoval'] = (bool) $manyToManyElement['orphanRemoval']; |
||
561 | } |
||
562 | |||
563 | // Evaluate second level cache |
||
564 | 21 | View Code Duplication | if (isset($manyToManyElement['cache'])) { |
565 | $mapping['cache'] = $metadata->getAssociationCacheDefaults($mapping['fieldName'], $this->cacheToArray($manyToManyElement['cache'])); |
||
566 | } |
||
567 | |||
568 | 21 | $metadata->mapManyToMany($mapping); |
|
569 | } |
||
570 | } |
||
571 | |||
572 | // Evaluate associationOverride |
||
573 | 62 | if (isset($element['associationOverride']) && is_array($element['associationOverride'])) { |
|
574 | |||
575 | 8 | foreach ($element['associationOverride'] as $fieldName => $associationOverrideElement) { |
|
576 | 8 | $override = []; |
|
577 | |||
578 | // Check for joinColumn |
||
579 | 8 | if (isset($associationOverrideElement['joinColumn'])) { |
|
580 | 4 | $joinColumns = []; |
|
581 | 4 | foreach ($associationOverrideElement['joinColumn'] as $name => $joinColumnElement) { |
|
582 | 4 | if ( ! isset($joinColumnElement['name'])) { |
|
583 | $joinColumnElement['name'] = $name; |
||
584 | } |
||
585 | 4 | $joinColumns[] = $this->joinColumnToArray($joinColumnElement); |
|
586 | } |
||
587 | 4 | $override['joinColumns'] = $joinColumns; |
|
588 | } |
||
589 | |||
590 | // Check for joinTable |
||
591 | 8 | if (isset($associationOverrideElement['joinTable'])) { |
|
592 | |||
593 | 4 | $joinTableElement = $associationOverrideElement['joinTable']; |
|
594 | $joinTable = [ |
||
595 | 4 | 'name' => $joinTableElement['name'] |
|
596 | ]; |
||
597 | |||
598 | 4 | if (isset($joinTableElement['schema'])) { |
|
599 | $joinTable['schema'] = $joinTableElement['schema']; |
||
600 | } |
||
601 | |||
602 | 4 | foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) { |
|
603 | 4 | if ( ! isset($joinColumnElement['name'])) { |
|
604 | 4 | $joinColumnElement['name'] = $name; |
|
605 | } |
||
606 | |||
607 | 4 | $joinTable['joinColumns'][] = $this->joinColumnToArray($joinColumnElement); |
|
608 | } |
||
609 | |||
610 | 4 | foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) { |
|
611 | 4 | if ( ! isset($joinColumnElement['name'])) { |
|
612 | 4 | $joinColumnElement['name'] = $name; |
|
613 | } |
||
614 | |||
615 | 4 | $joinTable['inverseJoinColumns'][] = $this->joinColumnToArray($joinColumnElement); |
|
616 | } |
||
617 | |||
618 | 4 | $override['joinTable'] = $joinTable; |
|
619 | } |
||
620 | |||
621 | // Check for inversedBy |
||
622 | 8 | if (isset($associationOverrideElement['inversedBy'])) { |
|
623 | 2 | $override['inversedBy'] = (string) $associationOverrideElement['inversedBy']; |
|
624 | } |
||
625 | |||
626 | // Check for `fetch` |
||
627 | 8 | View Code Duplication | if (isset($associationOverrideElement['fetch'])) { |
628 | 2 | $override['fetch'] = constant(Metadata::class . '::FETCH_' . $associationOverrideElement['fetch']); |
|
629 | } |
||
630 | |||
631 | 8 | $metadata->setAssociationOverride($fieldName, $override); |
|
632 | } |
||
633 | } |
||
634 | |||
635 | // Evaluate associationOverride |
||
636 | 62 | if (isset($element['attributeOverride']) && is_array($element['attributeOverride'])) { |
|
637 | |||
638 | 4 | foreach ($element['attributeOverride'] as $fieldName => $attributeOverrideElement) { |
|
639 | 4 | $mapping = $this->columnToArray($fieldName, $attributeOverrideElement); |
|
640 | 4 | $metadata->setAttributeOverride($fieldName, $mapping); |
|
641 | } |
||
642 | } |
||
643 | |||
644 | // Evaluate lifeCycleCallbacks |
||
645 | 62 | if (isset($element['lifecycleCallbacks'])) { |
|
646 | 9 | foreach ($element['lifecycleCallbacks'] as $type => $methods) { |
|
647 | 8 | foreach ($methods as $method) { |
|
648 | 8 | $metadata->addLifecycleCallback($method, constant('Doctrine\ORM\Events::' . $type)); |
|
649 | } |
||
650 | } |
||
651 | } |
||
652 | |||
653 | // Evaluate entityListeners |
||
654 | 62 | if (isset($element['entityListeners'])) { |
|
655 | 8 | foreach ($element['entityListeners'] as $className => $entityListener) { |
|
656 | // Evaluate the listener using naming convention. |
||
657 | 8 | if (empty($entityListener)) { |
|
658 | 4 | EntityListenerBuilder::bindEntityListener($metadata, $className); |
|
659 | |||
660 | 4 | continue; |
|
661 | } |
||
662 | |||
663 | 4 | foreach ($entityListener as $eventName => $callbackElement) { |
|
664 | 4 | foreach ($callbackElement as $methodName) { |
|
665 | 4 | $metadata->addEntityListener($eventName, $className, $methodName); |
|
666 | } |
||
667 | } |
||
668 | } |
||
669 | } |
||
670 | 62 | } |
|
671 | |||
672 | /** |
||
673 | * Constructs a joinColumn mapping array based on the information |
||
674 | * found in the given join column element. |
||
675 | * |
||
676 | * @param array $joinColumnElement The array join column element. |
||
677 | * |
||
678 | * @return array The mapping array. |
||
679 | */ |
||
680 | 21 | private function joinColumnToArray($joinColumnElement) |
|
681 | { |
||
682 | 21 | $joinColumn = []; |
|
683 | 21 | if (isset($joinColumnElement['referencedColumnName'])) { |
|
684 | 21 | $joinColumn['referencedColumnName'] = (string) $joinColumnElement['referencedColumnName']; |
|
685 | } |
||
686 | |||
687 | 21 | if (isset($joinColumnElement['name'])) { |
|
688 | 17 | $joinColumn['name'] = (string) $joinColumnElement['name']; |
|
689 | } |
||
690 | |||
691 | 21 | if (isset($joinColumnElement['fieldName'])) { |
|
692 | $joinColumn['fieldName'] = (string) $joinColumnElement['fieldName']; |
||
693 | } |
||
694 | |||
695 | 21 | if (isset($joinColumnElement['unique'])) { |
|
696 | 8 | $joinColumn['unique'] = (bool) $joinColumnElement['unique']; |
|
697 | } |
||
698 | |||
699 | 21 | if (isset($joinColumnElement['nullable'])) { |
|
700 | 10 | $joinColumn['nullable'] = (bool) $joinColumnElement['nullable']; |
|
701 | } |
||
702 | |||
703 | 21 | if (isset($joinColumnElement['onDelete'])) { |
|
704 | 8 | $joinColumn['onDelete'] = $joinColumnElement['onDelete']; |
|
705 | } |
||
706 | |||
707 | 21 | if (isset($joinColumnElement['columnDefinition'])) { |
|
708 | 8 | $joinColumn['columnDefinition'] = $joinColumnElement['columnDefinition']; |
|
709 | } |
||
710 | |||
711 | 21 | return $joinColumn; |
|
712 | } |
||
713 | |||
714 | /** |
||
715 | * Parses the given column as array. |
||
716 | * |
||
717 | * @param string $fieldName |
||
718 | * @param array $column |
||
719 | * |
||
720 | * @return array |
||
721 | */ |
||
722 | 43 | private function columnToArray($fieldName, $column) |
|
723 | { |
||
724 | $mapping = [ |
||
725 | 43 | 'fieldName' => $fieldName |
|
726 | ]; |
||
727 | |||
728 | 43 | if (isset($column['type'])) { |
|
729 | 37 | $params = explode('(', $column['type']); |
|
730 | |||
731 | 37 | $column['type'] = $params[0]; |
|
732 | 37 | $mapping['type'] = $column['type']; |
|
733 | |||
734 | 37 | if (isset($params[1])) { |
|
735 | 2 | $column['length'] = (integer) substr($params[1], 0, strlen($params[1]) - 1); |
|
736 | } |
||
737 | } |
||
738 | |||
739 | 43 | if (isset($column['column'])) { |
|
740 | 15 | $mapping['columnName'] = $column['column']; |
|
741 | } |
||
742 | |||
743 | 43 | if (isset($column['length'])) { |
|
744 | 25 | $mapping['length'] = $column['length']; |
|
745 | } |
||
746 | |||
747 | 43 | if (isset($column['precision'])) { |
|
748 | $mapping['precision'] = $column['precision']; |
||
749 | } |
||
750 | |||
751 | 43 | if (isset($column['scale'])) { |
|
752 | $mapping['scale'] = $column['scale']; |
||
753 | } |
||
754 | |||
755 | 43 | if (isset($column['unique'])) { |
|
756 | 17 | $mapping['unique'] = (bool) $column['unique']; |
|
757 | } |
||
758 | |||
759 | 43 | if (isset($column['options'])) { |
|
760 | 8 | $mapping['options'] = $column['options']; |
|
761 | } |
||
762 | |||
763 | 43 | if (isset($column['nullable'])) { |
|
764 | 14 | $mapping['nullable'] = $column['nullable']; |
|
765 | } |
||
766 | |||
767 | 43 | if (isset($column['version']) && $column['version']) { |
|
768 | 6 | $mapping['version'] = $column['version']; |
|
769 | } |
||
770 | |||
771 | 43 | if (isset($column['columnDefinition'])) { |
|
772 | 10 | $mapping['columnDefinition'] = $column['columnDefinition']; |
|
773 | } |
||
774 | |||
775 | 43 | return $mapping; |
|
776 | } |
||
777 | |||
778 | /** |
||
779 | * Parse / Normalize the cache configuration |
||
780 | * |
||
781 | * @param array $cacheMapping |
||
782 | * |
||
783 | * @return array |
||
784 | */ |
||
785 | 2 | View Code Duplication | private function cacheToArray($cacheMapping) |
786 | { |
||
787 | 2 | $region = isset($cacheMapping['region']) ? (string) $cacheMapping['region'] : null; |
|
788 | 2 | $usage = isset($cacheMapping['usage']) ? strtoupper($cacheMapping['usage']) : null; |
|
789 | |||
790 | 2 | if ($usage && ! defined('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage)) { |
|
791 | throw new \InvalidArgumentException(sprintf('Invalid cache usage "%s"', $usage)); |
||
792 | } |
||
793 | |||
794 | 2 | if ($usage) { |
|
795 | 2 | $usage = constant('Doctrine\ORM\Mapping\ClassMetadata::CACHE_USAGE_' . $usage); |
|
796 | } |
||
797 | |||
798 | return [ |
||
799 | 2 | 'usage' => $usage, |
|
800 | 2 | 'region' => $region, |
|
801 | ]; |
||
802 | } |
||
803 | |||
804 | /** |
||
805 | * {@inheritDoc} |
||
806 | */ |
||
807 | 66 | protected function loadMappingFile($file) |
|
808 | { |
||
809 | 66 | if (defined(Yaml::class . '::PARSE_KEYS_AS_STRINGS')) { |
|
810 | 66 | return Yaml::parse(file_get_contents($file), Yaml::PARSE_KEYS_AS_STRINGS); |
|
0 ignored issues
–
show
|
|||
811 | } |
||
812 | |||
813 | return Yaml::parse(file_get_contents($file)); |
||
814 | } |
||
815 | } |
||
816 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.