Completed
Pull Request — master (#1834)
by Andreas
16:41
created

XmlDriver::addFieldMapping()   B

Complexity

Conditions 11
Paths 67

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11.6363

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 19
cts 23
cp 0.8261
rs 7.3166
c 0
b 0
f 0
cc 11
nc 67
nop 2
crap 11.6363

How to fix   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 ClassMetadata $class */
46
        /** @var \SimpleXMLElement $xmlRoot */
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['write-concern'])) {
85
            $class->setWriteConcern((string) $xmlRoot['write-concern']);
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
            $class->setDiscriminatorField((string) $discrField['name']);
97
        }
98 7
        if (isset($xmlRoot->{'discriminator-map'})) {
99
            $map = [];
100
            foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} as $discrMapElement) {
101
                $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class'];
102
            }
103
            $class->setDiscriminatorMap($map);
104
        }
105 7
        if (isset($xmlRoot->{'default-discriminator-value'})) {
106
            $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']);
107
        }
108 7
        if (isset($xmlRoot->{'indexes'})) {
109 2
            foreach ($xmlRoot->{'indexes'}->{'index'} as $index) {
110 2
                $this->addIndex($class, $index);
111
            }
112
        }
113 7
        if (isset($xmlRoot->{'shard-key'})) {
114
            $this->setShardKey($class, $xmlRoot->{'shard-key'}[0]);
115
        }
116 7
        if (isset($xmlRoot['read-only']) && (string) $xmlRoot['read-only'] === 'true') {
117
            $class->markReadOnly();
118
        }
119 7
        if (isset($xmlRoot->{'read-preference'})) {
120
            $class->setReadPreference(...$this->transformReadPreference($xmlRoot->{'read-preference'}));
121
        }
122
123 7
        if (isset($xmlRoot->id)) {
124 7
            $field = $xmlRoot->id;
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
125
            $mapping = [
126 7
                'id' => true,
127
                'fieldName' => 'id',
128
            ];
129
130 7
            $attributes = $field->attributes();
131 7
            foreach ($attributes as $key => $value) {
132 2
                $mapping[$key] = (string) $value;
133
            }
134
135 7
            if (isset($mapping['strategy'])) {
136 2
                $mapping['options'] = [];
137 2
                if (isset($field->{'generator-option'})) {
138 1
                    foreach ($field->{'generator-option'} as $generatorOptions) {
139 1
                        $attributesGenerator = iterator_to_array($generatorOptions->attributes());
140 1
                        if (! isset($attributesGenerator['name']) || ! isset($attributesGenerator['value'])) {
141
                            continue;
142
                        }
143
144 1
                        $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value'];
145
                    }
146
                }
147
            }
148
149 7
            $this->addFieldMapping($class, $mapping);
150
        }
151
152 7
        if (isset($xmlRoot->field)) {
153 2
            foreach ($xmlRoot->field as $field) {
154 2
                $mapping = [];
155 2
                $attributes = $field->attributes();
156 2
                foreach ($attributes as $key => $value) {
157 2
                    $mapping[$key] = (string) $value;
158 2
                    $booleanAttributes = ['id', 'reference', 'embed', 'unique', 'sparse'];
159 2
                    if (! in_array($key, $booleanAttributes)) {
160 2
                        continue;
161
                    }
162
163 1
                    $mapping[$key] = ($mapping[$key] === 'true');
164
                }
165
166 2
                if (isset($attributes['not-saved'])) {
167
                    $mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true');
168
                }
169
170 2
                if (isset($attributes['field-name'])) {
171
                    $mapping['fieldName'] = (string) $attributes['field-name'];
172
                }
173
174 2
                if (isset($attributes['also-load'])) {
175
                    $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
176 2
                } elseif (isset($attributes['version'])) {
177
                    $mapping['version'] = ((string) $attributes['version'] === 'true');
178 2
                } elseif (isset($attributes['lock'])) {
179
                    $mapping['lock'] = ((string) $attributes['lock'] === 'true');
180
                }
181
182 2
                $this->addFieldMapping($class, $mapping);
183
            }
184
        }
185 7
        if (isset($xmlRoot->{'embed-one'})) {
186 1
            foreach ($xmlRoot->{'embed-one'} as $embed) {
187 1
                $this->addEmbedMapping($class, $embed, 'one');
188
            }
189
        }
190 7
        if (isset($xmlRoot->{'embed-many'})) {
191 1
            foreach ($xmlRoot->{'embed-many'} as $embed) {
192 1
                $this->addEmbedMapping($class, $embed, 'many');
193
            }
194
        }
195 7
        if (isset($xmlRoot->{'reference-many'})) {
196 3
            foreach ($xmlRoot->{'reference-many'} as $reference) {
197 3
                $this->addReferenceMapping($class, $reference, 'many');
198
            }
199
        }
200 7
        if (isset($xmlRoot->{'reference-one'})) {
201 2
            foreach ($xmlRoot->{'reference-one'} as $reference) {
202 2
                $this->addReferenceMapping($class, $reference, 'one');
203
            }
204
        }
205 7
        if (isset($xmlRoot->{'lifecycle-callbacks'})) {
206 1
            foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
207 1
                $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type']));
208
            }
209
        }
210 7
        if (! isset($xmlRoot->{'also-load-methods'})) {
211 7
            return;
212
        }
213
214 1
        foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) {
215 1
            $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']);
216
        }
217 1
    }
218
219 8
    private function addFieldMapping(ClassMetadata $class, $mapping)
220
    {
221 8
        if (isset($mapping['name'])) {
222 5
            $name = $mapping['name'];
223 7
        } elseif (isset($mapping['fieldName'])) {
224 7
            $name = $mapping['fieldName'];
225
        } else {
226
            throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping');
227
        }
228
229 8
        $class->mapField($mapping);
230
231
        // Index this field if either "index", "unique", or "sparse" are set
232 8
        if (! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) {
233 8
            return;
234
        }
235
236 1
        $keys = [$name => $mapping['order'] ?? 'asc'];
237 1
        $options = [];
238
239 1
        if (isset($mapping['background'])) {
240
            $options['background'] = (bool) $mapping['background'];
241
        }
242 1
        if (isset($mapping['drop-dups'])) {
243
            $options['dropDups'] = (bool) $mapping['drop-dups'];
244
        }
245 1
        if (isset($mapping['index-name'])) {
246
            $options['name'] = (string) $mapping['index-name'];
247
        }
248 1
        if (isset($mapping['sparse'])) {
249 1
            $options['sparse'] = (bool) $mapping['sparse'];
250
        }
251 1
        if (isset($mapping['unique'])) {
252 1
            $options['unique'] = (bool) $mapping['unique'];
253
        }
254
255 1
        $class->addIndex($keys, $options);
256 1
    }
257
258 1
    private function addEmbedMapping(ClassMetadata $class, $embed, $type)
259
    {
260 1
        $attributes = $embed->attributes();
261 1
        $defaultStrategy = $type === 'one' ? ClassMetadata::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
262
        $mapping = [
263 1
            'type'            => $type,
264
            'embedded'        => true,
265 1
            'targetDocument'  => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
266 1
            'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
267 1
            'name'            => (string) $attributes['field'],
268 1
            'strategy'        => (string) ($attributes['strategy'] ?? $defaultStrategy),
269
        ];
270 1
        if (isset($attributes['field-name'])) {
271
            $mapping['fieldName'] = (string) $attributes['field-name'];
272
        }
273 1
        if (isset($embed->{'discriminator-field'})) {
274
            $attr = $embed->{'discriminator-field'};
275
            $mapping['discriminatorField'] = (string) $attr['name'];
276
        }
277 1
        if (isset($embed->{'discriminator-map'})) {
278
            foreach ($embed->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
279
                $attr = $discriminatorMapping->attributes();
280
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
281
            }
282
        }
283 1
        if (isset($embed->{'default-discriminator-value'})) {
284
            $mapping['defaultDiscriminatorValue'] = (string) $embed->{'default-discriminator-value'}['value'];
285
        }
286 1
        if (isset($attributes['not-saved'])) {
287
            $mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true');
288
        }
289 1
        if (isset($attributes['also-load'])) {
290
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
291
        }
292 1
        $this->addFieldMapping($class, $mapping);
293 1
    }
294
295 4
    private function addReferenceMapping(ClassMetadata $class, $reference, $type)
296
    {
297 4
        $cascade = array_keys((array) $reference->cascade);
298 4
        if (count($cascade) === 1) {
299 1
            $cascade = current($cascade) ?: next($cascade);
300
        }
301 4
        $attributes = $reference->attributes();
302 4
        $defaultStrategy = $type === 'one' ? ClassMetadata::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY;
303
        $mapping = [
304 4
            'cascade'          => $cascade,
305 4
            'orphanRemoval'    => isset($attributes['orphan-removal']) ? ((string) $attributes['orphan-removal'] === 'true') : false,
306 4
            'type'             => $type,
307
            'reference'        => true,
308 4
            'storeAs'          => (string) ($attributes['store-as'] ?? ClassMetadata::REFERENCE_STORE_AS_DB_REF),
309 4
            'targetDocument'   => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null,
310 4
            'collectionClass'  => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null,
311 4
            'name'             => (string) $attributes['field'],
312 4
            'strategy'         => (string) ($attributes['strategy'] ?? $defaultStrategy),
313 4
            'inversedBy'       => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null,
314 4
            'mappedBy'         => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null,
315 4
            'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null,
316 4
            'limit'            => isset($attributes['limit']) ? (int) $attributes['limit'] : null,
317 4
            'skip'             => isset($attributes['skip']) ? (int) $attributes['skip'] : null,
318
            'prime'            => [],
319
        ];
320
321 4
        if (isset($attributes['field-name'])) {
322
            $mapping['fieldName'] = (string) $attributes['field-name'];
323
        }
324 4
        if (isset($reference->{'discriminator-field'})) {
325
            $attr = $reference->{'discriminator-field'};
326
            $mapping['discriminatorField'] = (string) $attr['name'];
327
        }
328 4
        if (isset($reference->{'discriminator-map'})) {
329
            foreach ($reference->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) {
330
                $attr = $discriminatorMapping->attributes();
331
                $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class'];
332
            }
333
        }
334 4
        if (isset($reference->{'default-discriminator-value'})) {
335
            $mapping['defaultDiscriminatorValue'] = (string) $reference->{'default-discriminator-value'}['value'];
336
        }
337 4
        if (isset($reference->{'sort'})) {
338
            foreach ($reference->{'sort'}->{'sort'} as $sort) {
339
                $attr = $sort->attributes();
340
                $mapping['sort'][(string) $attr['field']] = (string) ($attr['order'] ?? 'asc');
341
            }
342
        }
343 4
        if (isset($reference->{'criteria'})) {
344
            foreach ($reference->{'criteria'}->{'criteria'} as $criteria) {
345
                $attr = $criteria->attributes();
346
                $mapping['criteria'][(string) $attr['field']] = (string) $attr['value'];
347
            }
348
        }
349 4
        if (isset($attributes['not-saved'])) {
350
            $mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true');
351
        }
352 4
        if (isset($attributes['also-load'])) {
353
            $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']);
354
        }
355 4
        if (isset($reference->{'prime'})) {
356 1
            foreach ($reference->{'prime'}->{'field'} as $field) {
357 1
                $attr = $field->attributes();
358 1
                $mapping['prime'][] = (string) $attr['name'];
359
            }
360
        }
361
362 4
        $this->addFieldMapping($class, $mapping);
363 4
    }
364
365 2
    private function addIndex(ClassMetadata $class, \SimpleXmlElement $xmlIndex)
366
    {
367 2
        $attributes = $xmlIndex->attributes();
368
369 2
        $keys = [];
370
371 2
        foreach ($xmlIndex->{'key'} as $key) {
372 2
            $keys[(string) $key['name']] = (string) ($key['order'] ?? 'asc');
373
        }
374
375 2
        $options = [];
376
377 2
        if (isset($attributes['background'])) {
378
            $options['background'] = ((string) $attributes['background'] === 'true');
379
        }
380 2
        if (isset($attributes['drop-dups'])) {
381
            $options['dropDups'] = ((string) $attributes['drop-dups'] === 'true');
382
        }
383 2
        if (isset($attributes['name'])) {
384
            $options['name'] = (string) $attributes['name'];
385
        }
386 2
        if (isset($attributes['sparse'])) {
387
            $options['sparse'] = ((string) $attributes['sparse'] === 'true');
388
        }
389 2
        if (isset($attributes['unique'])) {
390
            $options['unique'] = ((string) $attributes['unique'] === 'true');
391
        }
392
393 2
        if (isset($xmlIndex->{'option'})) {
394
            foreach ($xmlIndex->{'option'} as $option) {
395
                $value = (string) $option['value'];
396
                if ($value === 'true') {
397
                    $value = true;
398
                } elseif ($value === 'false') {
399
                    $value = false;
400
                } elseif (is_numeric($value)) {
401
                    $value = preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value;
402
                }
403
                $options[(string) $option['name']] = $value;
404
            }
405
        }
406
407 2
        if (isset($xmlIndex->{'partial-filter-expression'})) {
408 2
            $partialFilterExpressionMapping = $xmlIndex->{'partial-filter-expression'};
409
410 2
            if (isset($partialFilterExpressionMapping->and)) {
411 2
                foreach ($partialFilterExpressionMapping->and as $and) {
412 2
                    if (! isset($and->field)) {
413 1
                        continue;
414
                    }
415
416 2
                    $partialFilterExpression = $this->getPartialFilterExpression($and->field);
417 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...
418
                        continue;
419
                    }
420
421 2
                    $options['partialFilterExpression']['$and'][] = $partialFilterExpression;
422
                }
423 1
            } elseif (isset($partialFilterExpressionMapping->field)) {
424 1
                $partialFilterExpression = $this->getPartialFilterExpression($partialFilterExpressionMapping->field);
425
426 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...
427 1
                    $options['partialFilterExpression'] = $partialFilterExpression;
428
                }
429
            }
430
        }
431
432 2
        $class->addIndex($keys, $options);
433 2
    }
434
435 2
    private function getPartialFilterExpression(\SimpleXMLElement $fields)
436
    {
437 2
        $partialFilterExpression = [];
438 2
        foreach ($fields as $field) {
439 2
            $operator = (string) $field['operator'] ?: null;
440
441 2
            if (! isset($field['value'])) {
442 1
                if (! isset($field->field)) {
443
                    continue;
444
                }
445
446 1
                $nestedExpression = $this->getPartialFilterExpression($field->field);
447 1
                if (! $nestedExpression) {
448
                    continue;
449
                }
450
451 1
                $value = $nestedExpression;
452
            } else {
453 2
                $value = trim((string) $field['value']);
454
            }
455
456 2
            if ($value === 'true') {
457
                $value = true;
458 2
            } elseif ($value === 'false') {
459
                $value = false;
460 2
            } elseif (is_numeric($value)) {
461 1
                $value = preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value;
462
            }
463
464 2
            $partialFilterExpression[(string) $field['name']] = $operator ? ['$' . $operator => $value] : $value;
465
        }
466
467 2
        return $partialFilterExpression;
468
    }
469
470 1
    private function setShardKey(ClassMetadata $class, \SimpleXmlElement $xmlShardkey)
471
    {
472 1
        $attributes = $xmlShardkey->attributes();
473
474 1
        $keys = [];
475 1
        $options = [];
476 1
        foreach ($xmlShardkey->{'key'} as $key) {
477 1
            $keys[(string) $key['name']] = (string) ($key['order'] ?? 'asc');
478
        }
479
480 1
        if (isset($attributes['unique'])) {
481 1
            $options['unique'] = ((string) $attributes['unique'] === 'true');
482
        }
483
484 1
        if (isset($attributes['numInitialChunks'])) {
485 1
            $options['numInitialChunks'] = (int) $attributes['numInitialChunks'];
486
        }
487
488 1
        if (isset($xmlShardkey->{'option'})) {
489
            foreach ($xmlShardkey->{'option'} as $option) {
490
                $value = (string) $option['value'];
491
                if ($value === 'true') {
492
                    $value = true;
493
                } elseif ($value === 'false') {
494
                    $value = false;
495
                } elseif (is_numeric($value)) {
496
                    $value = preg_match('/^[-]?\d+$/', $value) ? (int) $value : (float) $value;
497
                }
498
                $options[(string) $option['name']] = $value;
499
            }
500
        }
501
502 1
        $class->setShardKey($keys, $options);
503 1
    }
504
505
    /**
506
     * Parses <read-preference> to a format suitable for the underlying driver.
507
     *
508
     * list($readPreference, $tags) = $this->transformReadPreference($xml->{read-preference});
509
     *
510
     * @param \SimpleXMLElement $xmlReadPreference
511
     * @return array
512
     */
513
    private function transformReadPreference($xmlReadPreference)
514
    {
515
        $tags = null;
516
        if (isset($xmlReadPreference->{'tag-set'})) {
517
            $tags = [];
518
            foreach ($xmlReadPreference->{'tag-set'} as $tagSet) {
519
                $set = [];
520
                foreach ($tagSet->tag as $tag) {
521
                    $set[(string) $tag['name']] = (string) $tag['value'];
522
                }
523
                $tags[] = $set;
524
            }
525
        }
526
        return [(string) $xmlReadPreference['mode'], $tags];
527
    }
528
529
    /**
530
     * {@inheritDoc}
531
     */
532 7
    protected function loadMappingFile($file)
533
    {
534 7
        $result = [];
535 7
        $xmlElement = simplexml_load_file($file);
536
537 7
        foreach (['document', 'embedded-document', 'mapped-superclass', 'query-result-document'] as $type) {
538 7
            if (! isset($xmlElement->$type)) {
539 7
                continue;
540
            }
541
542 7
            foreach ($xmlElement->$type as $documentElement) {
543 7
                $documentName = (string) $documentElement['name'];
544 7
                $result[$documentName] = $documentElement;
545
            }
546
        }
547
548 7
        return $result;
549
    }
550
}
551