Completed
Pull Request — master (#1757)
by Maciej
10:01
created

XmlDriver::loadMetadataForClass()   F

Complexity

Conditions 53
Paths > 20000

Size

Total Lines 159
Code Lines 101

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 74
CRAP Score 106.6473

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 159
ccs 74
cts 101
cp 0.7327
rs 2
c 1
b 0
f 0
cc 53
eloc 101
nc 14155777
nop 2
crap 106.6473

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Mapping\Driver;
6
7
use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
8
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
9
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
10
use function array_keys;
11
use function constant;
12
use function count;
13
use function current;
14
use function explode;
15
use function in_array;
16
use function is_numeric;
17
use function iterator_to_array;
18
use function next;
19
use function preg_match;
20
use function simplexml_load_file;
21
use function strtoupper;
22
use function trim;
23
24
/**
25
 * XmlDriver is a metadata driver that enables mapping through XML files.
26
 *
27
 */
28
class XmlDriver extends FileDriver
29
{
30
    public const DEFAULT_FILE_EXTENSION = '.dcm.xml';
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 13
    public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION)
36
    {
37 13
        parent::__construct($locator, $fileExtension);
38 13
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 7
    public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Mapping\ClassMetadata $class)
44
    {
45
        /* @var $class ClassMetadata */
46
        /* @var $xmlRoot \SimpleXMLElement */
47 7
        $xmlRoot = $this->getElement($className);
48 7
        if (! $xmlRoot) {
49
            return;
50
        }
51
52 7
        if ($xmlRoot->getName() === 'document') {
53 7
            if (isset($xmlRoot['repository-class'])) {
54 7
                $class->setCustomRepositoryClass((string) $xmlRoot['repository-class']);
55
            }
56 2
        } elseif ($xmlRoot->getName() === 'mapped-superclass') {
57 1
            $class->setCustomRepositoryClass(
58 1
                isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null
59
            );
60 1
            $class->isMappedSuperclass = true;
61 1
        } elseif ($xmlRoot->getName() === 'embedded-document') {
62 1
            $class->isEmbeddedDocument = true;
63 1
        } elseif ($xmlRoot->getName() === 'query-result-document') {
64 1
            $class->isQueryResultDocument = true;
65
        }
66 7
        if (isset($xmlRoot['db'])) {
67 4
            $class->setDatabase((string) $xmlRoot['db']);
68
        }
69 7
        if (isset($xmlRoot['collection'])) {
70 6
            if (isset($xmlRoot['capped-collection'])) {
71
                $config = ['name' => (string) $xmlRoot['collection']];
72
                $config['capped'] = (bool) $xmlRoot['capped-collection'];
73
                if (isset($xmlRoot['capped-collection-max'])) {
74
                    $config['max'] = (int) $xmlRoot['capped-collection-max'];
75
                }
76
                if (isset($xmlRoot['capped-collection-size'])) {
77
                    $config['size'] = (int) $xmlRoot['capped-collection-size'];
78
                }
79
                $class->setCollection($config);
80
            } else {
81 6
                $class->setCollection((string) $xmlRoot['collection']);
82
            }
83
        }
84 7
        if (isset($xmlRoot['writeConcern'])) {
85
            $class->setWriteConcern((string) $xmlRoot['writeConcern']);
86
        }
87 7
        if (isset($xmlRoot['inheritance-type'])) {
88
            $inheritanceType = (string) $xmlRoot['inheritance-type'];
89
            $class->setInheritanceType(constant(ClassMetadata::class . '::INHERITANCE_TYPE_' . $inheritanceType));
90
        }
91 7
        if (isset($xmlRoot['change-tracking-policy'])) {
92 1
            $class->setChangeTrackingPolicy(constant(ClassMetadata::class . '::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy'])));
93
        }
94 7
        if (isset($xmlRoot->{'discriminator-field'})) {
95
            $discrField = $xmlRoot->{'discriminator-field'};
96
            /* XSD only allows for "name", which is consistent with association
97
             * configurations, but fall back to "fieldName" for BC.
98
             */
99
            $class->setDiscriminatorField(
100
                (string) ($discrField['name'] ?? $discrField['fieldName'])
101
            );
102
        }
103 7
        if (isset($xmlRoot->{'discriminator-map'})) {
104
            $map = [];
105
            foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} as $discrMapElement) {
106
                $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class'];
107
            }
108
            $class->setDiscriminatorMap($map);
109
        }
110 7
        if (isset($xmlRoot->{'default-discriminator-value'})) {
111
            $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']);
112
        }
113 7
        if (isset($xmlRoot->{'indexes'})) {
114 2
            foreach ($xmlRoot->{'indexes'}->{'index'} as $index) {
115 2
                $this->addIndex($class, $index);
116
            }
117
        }
118 7
        if (isset($xmlRoot->{'shard-key'})) {
119
            $this->setShardKey($class, $xmlRoot->{'shard-key'}[0]);
120
        }
121 7
        if (isset($xmlRoot['read-only']) && (string) $xmlRoot['read-only'] === 'true') {
122
            $class->markReadOnly();
123
        }
124 7
        if (isset($xmlRoot->{'read-preference'})) {
125
            $class->setReadPreference(...$this->transformReadPreference($xmlRoot->{'read-preference'}));
126
        }
127 7
        if (isset($xmlRoot->field)) {
128 7
            foreach ($xmlRoot->field as $field) {
129 7
                $mapping = [];
130 7
                $attributes = $field->attributes();
131 7
                foreach ($attributes as $key => $value) {
132 7
                    $mapping[$key] = (string) $value;
133 7
                    $booleanAttributes = ['id', 'reference', 'embed', 'unique', 'sparse'];
134 7
                    if (! in_array($key, $booleanAttributes)) {
135 7
                        continue;
136
                    }
137
138 7
                    $mapping[$key] = ($mapping[$key] === 'true');
139
                }
140 7
                if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) {
141 2
                    $mapping['options'] = [];
142 2
                    if (isset($field->{'id-generator-option'})) {
143 1
                        foreach ($field->{'id-generator-option'} as $generatorOptions) {
144 1
                            $attributesGenerator = iterator_to_array($generatorOptions->attributes());
145 1
                            if (! isset($attributesGenerator['name']) || ! isset($attributesGenerator['value'])) {
146
                                continue;
147
                            }
148
149 1
                            $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value'];
150
                        }
151
                    }
152
                }
153
154 7
                if (isset($attributes['not-saved'])) {
155
                    $mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true');
156
                }
157
158 7
                if (isset($attributes['also-load'])) {
159
                    $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
160 7
                } elseif (isset($attributes['version'])) {
161
                    $mapping['version'] = ((string) $attributes['version'] === 'true');
162 7
                } elseif (isset($attributes['lock'])) {
163
                    $mapping['lock'] = ((string) $attributes['lock'] === 'true');
164
                }
165
166 7
                $this->addFieldMapping($class, $mapping);
167
            }
168
        }
169 7
        if (isset($xmlRoot->{'embed-one'})) {
170 1
            foreach ($xmlRoot->{'embed-one'} as $embed) {
171 1
                $this->addEmbedMapping($class, $embed, 'one');
172
            }
173
        }
174 7
        if (isset($xmlRoot->{'embed-many'})) {
175 1
            foreach ($xmlRoot->{'embed-many'} as $embed) {
176 1
                $this->addEmbedMapping($class, $embed, 'many');
177
            }
178
        }
179 7
        if (isset($xmlRoot->{'reference-many'})) {
180 3
            foreach ($xmlRoot->{'reference-many'} as $reference) {
181 3
                $this->addReferenceMapping($class, $reference, 'many');
182
            }
183
        }
184 7
        if (isset($xmlRoot->{'reference-one'})) {
185 2
            foreach ($xmlRoot->{'reference-one'} as $reference) {
186 2
                $this->addReferenceMapping($class, $reference, 'one');
187
            }
188
        }
189 7
        if (isset($xmlRoot->{'lifecycle-callbacks'})) {
190 1
            foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
191 1
                $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type']));
192
            }
193
        }
194 7
        if (! isset($xmlRoot->{'also-load-methods'})) {
195 7
            return;
196
        }
197
198 1
        foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) {
199 1
            $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']);
200
        }
201 1
    }
202
203 8
    private function addFieldMapping(ClassMetadata $class, $mapping)
204
    {
205 8
        if (isset($mapping['name'])) {
206 8
            $name = $mapping['name'];
207
        } elseif (isset($mapping['fieldName'])) {
208
            $name = $mapping['fieldName'];
209
        } else {
210
            throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping');
211
        }
212
213 8
        $class->mapField($mapping);
214
215
        // Index this field if either "index", "unique", or "sparse" are set
216 8
        if (! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) {
217 8
            return;
218
        }
219
220 1
        $keys = [$name => $mapping['order'] ?? 'asc'];
221 1
        $options = [];
222
223 1
        if (isset($mapping['background'])) {
224
            $options['background'] = (bool) $mapping['background'];
225
        }
226 1
        if (isset($mapping['drop-dups'])) {
227
            $options['dropDups'] = (bool) $mapping['drop-dups'];
228
        }
229 1
        if (isset($mapping['index-name'])) {
230
            $options['name'] = (string) $mapping['index-name'];
231
        }
232 1
        if (isset($mapping['sparse'])) {
233 1
            $options['sparse'] = (bool) $mapping['sparse'];
234
        }
235 1
        if (isset($mapping['unique'])) {
236 1
            $options['unique'] = (bool) $mapping['unique'];
237
        }
238
239 1
        $class->addIndex($keys, $options);
240 1
    }
241
242 1
    private function addEmbedMapping(ClassMetadata $class, $embed, $type)
243
    {
244 1
        $attributes = $embed->attributes();
245 1
        $defaultStrategy = $type === 'one' ? ClassMetadata::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
246
        $mapping = [
247 1
            'type'            => $type,
248
            'embedded'        => true,
249 1
            'targetDocument'  => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
250 1
            'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
251 1
            'name'            => (string) $attributes['field'],
252 1
            'strategy'        => (string) ($attributes['strategy'] ?? $defaultStrategy),
253
        ];
254 1
        if (isset($attributes['fieldName'])) {
255
            $mapping['fieldName'] = (string) $attributes['fieldName'];
256
        }
257 1
        if (isset($embed->{'discriminator-field'})) {
258
            $attr = $embed->{'discriminator-field'};
259
            $mapping['discriminatorField'] = (string) $attr['name'];
260
        }
261 1
        if (isset($embed->{'discriminator-map'})) {
262
            foreach ($embed->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
263
                $attr = $discriminatorMapping->attributes();
264
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
265
            }
266
        }
267 1
        if (isset($embed->{'default-discriminator-value'})) {
268
            $mapping['defaultDiscriminatorValue'] = (string) $embed->{'default-discriminator-value'}['value'];
269
        }
270 1
        if (isset($attributes['not-saved'])) {
271
            $mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true');
272
        }
273 1
        if (isset($attributes['also-load'])) {
274
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
275
        }
276 1
        $this->addFieldMapping($class, $mapping);
277 1
    }
278
279 4
    private function addReferenceMapping(ClassMetadata $class, $reference, $type)
280
    {
281 4
        $cascade = array_keys((array) $reference->cascade);
282 4
        if (count($cascade) === 1) {
283 1
            $cascade = current($cascade) ?: next($cascade);
284
        }
285 4
        $attributes = $reference->attributes();
286 4
        $defaultStrategy = $type === 'one' ? ClassMetadata::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
287
        $mapping = [
288 4
            'cascade'          => $cascade,
289 4
            'orphanRemoval'    => isset($attributes['orphan-removal']) ? ((string) $attributes['orphan-removal'] === 'true') : false,
290 4
            'type'             => $type,
291
            'reference'        => true,
292 4
            'storeAs'          => (string) ($attributes['store-as'] ?? ClassMetadata::REFERENCE_STORE_AS_DB_REF),
293 4
            'targetDocument'   => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
294 4
            'collectionClass'  => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
295 4
            'name'             => (string) $attributes['field'],
296 4
            'strategy'         => (string) ($attributes['strategy'] ?? $defaultStrategy),
297 4
            'inversedBy'       => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null,
298 4
            'mappedBy'         => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null,
299 4
            'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null,
300 4
            'limit'            => isset($attributes['limit']) ? (int) $attributes['limit'] : null,
301 4
            'skip'             => isset($attributes['skip']) ? (int) $attributes['skip'] : null,
302
            'prime'            => [],
303
        ];
304
305 4
        if (isset($attributes['fieldName'])) {
306
            $mapping['fieldName'] = (string) $attributes['fieldName'];
307
        }
308 4
        if (isset($reference->{'discriminator-field'})) {
309
            $attr = $reference->{'discriminator-field'};
310
            $mapping['discriminatorField'] = (string) $attr['name'];
311
        }
312 4
        if (isset($reference->{'discriminator-map'})) {
313
            foreach ($reference->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
314
                $attr = $discriminatorMapping->attributes();
315
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
316
            }
317
        }
318 4
        if (isset($reference->{'default-discriminator-value'})) {
319
            $mapping['defaultDiscriminatorValue'] = (string) $reference->{'default-discriminator-value'}['value'];
320
        }
321 4
        if (isset($reference->{'sort'})) {
322
            foreach ($reference->{'sort'}->{'sort'} as $sort) {
323
                $attr = $sort->attributes();
324
                $mapping['sort'][(string) $attr['field']] = (string) ($attr['order'] ?? 'asc');
325
            }
326
        }
327 4
        if (isset($reference->{'criteria'})) {
328
            foreach ($reference->{'criteria'}->{'criteria'} as $criteria) {
329
                $attr = $criteria->attributes();
330
                $mapping['criteria'][(string) $attr['field']] = (string) $attr['value'];
331
            }
332
        }
333 4
        if (isset($attributes['not-saved'])) {
334
            $mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true');
335
        }
336 4
        if (isset($attributes['also-load'])) {
337
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
338
        }
339 4
        if (isset($reference->{'prime'})) {
340 1
            foreach ($reference->{'prime'}->{'field'} as $field) {
341 1
                $attr = $field->attributes();
342 1
                $mapping['prime'][] = (string) $attr['name'];
343
            }
344
        }
345
346 4
        $this->addFieldMapping($class, $mapping);
347 4
    }
348
349 2
    private function addIndex(ClassMetadata $class, \SimpleXmlElement $xmlIndex)
350
    {
351 2
        $attributes = $xmlIndex->attributes();
352
353 2
        $keys = [];
354
355 2
        foreach ($xmlIndex->{'key'} as $key) {
356 2
            $keys[(string) $key['name']] = (string) ($key['order'] ?? 'asc');
357
        }
358
359 2
        $options = [];
360
361 2
        if (isset($attributes['background'])) {
362
            $options['background'] = ((string) $attributes['background'] === 'true');
363
        }
364 2
        if (isset($attributes['drop-dups'])) {
365
            $options['dropDups'] = ((string) $attributes['drop-dups'] === 'true');
366
        }
367 2
        if (isset($attributes['name'])) {
368
            $options['name'] = (string) $attributes['name'];
369
        }
370 2
        if (isset($attributes['sparse'])) {
371
            $options['sparse'] = ((string) $attributes['sparse'] === 'true');
372
        }
373 2
        if (isset($attributes['unique'])) {
374
            $options['unique'] = ((string) $attributes['unique'] === 'true');
375
        }
376
377 2
        if (isset($xmlIndex->{'option'})) {
378
            foreach ($xmlIndex->{'option'} as $option) {
379
                $value = (string) $option['value'];
380
                if ($value === 'true') {
381
                    $value = true;
382
                } elseif ($value === 'false') {
383
                    $value = false;
384
                } elseif (is_numeric($value)) {
385
                    $value = preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value;
386
                }
387
                $options[(string) $option['name']] = $value;
388
            }
389
        }
390
391 2
        if (isset($xmlIndex->{'partial-filter-expression'})) {
392 2
            $partialFilterExpressionMapping = $xmlIndex->{'partial-filter-expression'};
393
394 2
            if (isset($partialFilterExpressionMapping->and)) {
395 2
                foreach ($partialFilterExpressionMapping->and as $and) {
396 2
                    if (! isset($and->field)) {
397 1
                        continue;
398
                    }
399
400 2
                    $partialFilterExpression = $this->getPartialFilterExpression($and->field);
401 2
                    if (! $partialFilterExpression) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partialFilterExpression of type array 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 empty(..) or ! empty(...) instead.

Loading history...
402
                        continue;
403
                    }
404
405 2
                    $options['partialFilterExpression']['$and'][] = $partialFilterExpression;
406
                }
407 1
            } elseif (isset($partialFilterExpressionMapping->field)) {
408 1
                $partialFilterExpression = $this->getPartialFilterExpression($partialFilterExpressionMapping->field);
409
410 1
                if ($partialFilterExpression) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $partialFilterExpression of type array 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 empty(..) or ! empty(...) instead.

Loading history...
411 1
                    $options['partialFilterExpression'] = $partialFilterExpression;
412
                }
413
            }
414
        }
415
416 2
        $class->addIndex($keys, $options);
417 2
    }
418
419 2
    private function getPartialFilterExpression(\SimpleXMLElement $fields)
420
    {
421 2
        $partialFilterExpression = [];
422 2
        foreach ($fields as $field) {
423 2
            $operator = (string) $field['operator'] ?: null;
424
425 2
            if (! isset($field['value'])) {
426 1
                if (! isset($field->field)) {
427
                    continue;
428
                }
429
430 1
                $nestedExpression = $this->getPartialFilterExpression($field->field);
431 1
                if (! $nestedExpression) {
432
                    continue;
433
                }
434
435 1
                $value = $nestedExpression;
436
            } else {
437 2
                $value = trim((string) $field['value']);
438
            }
439
440 2
            if ($value === 'true') {
441
                $value = true;
442 2
            } elseif ($value === 'false') {
443
                $value = false;
444 2
            } elseif (is_numeric($value)) {
445 1
                $value = preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value;
446
            }
447
448 2
            $partialFilterExpression[(string) $field['name']] = $operator ? ['$' . $operator => $value] : $value;
449
        }
450
451 2
        return $partialFilterExpression;
452
    }
453
454 1
    private function setShardKey(ClassMetadata $class, \SimpleXmlElement $xmlShardkey)
455
    {
456 1
        $attributes = $xmlShardkey->attributes();
457
458 1
        $keys = [];
459 1
        $options = [];
460 1
        foreach ($xmlShardkey->{'key'} as $key) {
461 1
            $keys[(string) $key['name']] = (string) ($key['order'] ?? 'asc');
462
        }
463
464 1
        if (isset($attributes['unique'])) {
465 1
            $options['unique'] = ((string) $attributes['unique'] === 'true');
466
        }
467
468 1
        if (isset($attributes['numInitialChunks'])) {
469 1
            $options['numInitialChunks'] = (int) $attributes['numInitialChunks'];
470
        }
471
472 1
        if (isset($xmlShardkey->{'option'})) {
473
            foreach ($xmlShardkey->{'option'} as $option) {
474
                $value = (string) $option['value'];
475
                if ($value === 'true') {
476
                    $value = true;
477
                } elseif ($value === 'false') {
478
                    $value = false;
479
                } elseif (is_numeric($value)) {
480
                    $value = preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value;
481
                }
482
                $options[(string) $option['name']] = $value;
483
            }
484
        }
485
486 1
        $class->setShardKey($keys, $options);
487 1
    }
488
489
    /**
490
     * Parses <read-preference> to a format suitable for the underlying driver.
491
     *
492
     * list($readPreference, $tags) = $this->transformReadPreference($xml->{read-preference});
493
     *
494
     * @param \SimpleXMLElement $xmlReadPreference
495
     * @return array
496
     */
497
    private function transformReadPreference($xmlReadPreference)
498
    {
499
        $tags = null;
500
        if (isset($xmlReadPreference->{'tag-set'})) {
501
            $tags = [];
502
            foreach ($xmlReadPreference->{'tag-set'} as $tagSet) {
503
                $set = [];
504
                foreach ($tagSet->tag as $tag) {
505
                    $set[(string) $tag['name']] = (string) $tag['value'];
506
                }
507
                $tags[] = $set;
508
            }
509
        }
510
        return [(string) $xmlReadPreference['mode'], $tags];
511
    }
512
513
    /**
514
     * {@inheritDoc}
515
     */
516 7
    protected function loadMappingFile($file)
517
    {
518 7
        $result = [];
519 7
        $xmlElement = simplexml_load_file($file);
520
521 7
        foreach (['document', 'embedded-document', 'mapped-superclass', 'query-result-document'] as $type) {
522 7
            if (! isset($xmlElement->$type)) {
523 7
                continue;
524
            }
525
526 7
            foreach ($xmlElement->$type as $documentElement) {
527 7
                $documentName = (string) $documentElement['name'];
528 7
                $result[$documentName] = $documentElement;
529
            }
530
        }
531
532 7
        return $result;
533
    }
534
}
535