XmlDriver::addMetadataFor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Infrastructure\Web\Doctrine\ODM\MongoDB\Metadata\Driver;
12
13
use Cubiche\Core\Metadata\ClassMetadataInterface;
14
use Cubiche\Core\Metadata\PropertyMetadata;
15
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Exception\MappingException;
16
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver as BaseXmlDriver;
17
18
/**
19
 * XmlDriver class.
20
 *
21
 * @author Ivannis Suárez Jerez <[email protected]>
22
 */
23
class XmlDriver extends BaseXmlDriver
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function addMetadataFor(\SimpleXMLElement $xmlRoot, ClassMetadataInterface $classMetadata)
29
    {
30
        $this->addMetadataForType('email', $xmlRoot, $classMetadata);
31
        $this->addMetadataForType('hostname', $xmlRoot, $classMetadata);
32
        $this->addMetadataForType('ip', $xmlRoot, $classMetadata);
33
        $this->addMetadataForType('path', $xmlRoot, $classMetadata);
34
        $this->addMetadataForType('port', $xmlRoot, $classMetadata);
35
        $this->addMetadataForType('url', $xmlRoot, $classMetadata);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function addMetadataForType($type, \SimpleXMLElement $xmlRoot, ClassMetadataInterface $classMetadata)
42
    {
43
        foreach ($xmlRoot->xpath('//cubiche:'.$type) as $item) {
44
            // get the field tag
45
            $field = $item->xpath('..')[0];
46
            $fieldMapping = $this->getMappingAttributes($field);
47
            $fieldName = $fieldMapping['name'];
48
49
            if ($field->getName() == 'field') {
50
                if (isset($fieldMapping['id']) && $fieldMapping['id'] !== false) {
51
                    throw MappingException::inField(
52
                        'The cubiche:'.$type.' configuration is only for field tags that is not an id',
53
                        $classMetadata->className(),
54
                        $fieldName
55
                    );
56
                }
57
58
                if (!isset($fieldMapping['type']) ||
59
                    (isset($fieldMapping['type']) && $fieldMapping['type'] !== 'CubicheType')
60
                ) {
61
                    throw MappingException::inField(
62
                        'The cubiche:'.$type.' parent should have a "type" value equal to CubicheType',
63
                        $classMetadata->className(),
64
                        $fieldName
65
                    );
66
                }
67
68
                $propertyMetadata = new PropertyMetadata($classMetadata->className(), $fieldName);
69
                $propertyMetadata->addMetadata('namespace', $type);
70
71
                $classMetadata->addPropertyMetadata($propertyMetadata);
72
            } else {
73
                throw MappingException::inField(
74
                    'The cubiche:'.$type.' configuration is only for id fields',
75
                    $classMetadata->className(),
76
                    $fieldName
77
                );
78
            }
79
        }
80
    }
81
}
82