Completed
Pull Request — master (#46)
by Daniel
03:15
created

XmlDriver::extractOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
namespace Psi\Component\ContentType\Metadata\Driver;
4
5
use Metadata\Driver\AdvancedDriverInterface;
6
use Psi\Component\ContentType\Metadata\ClassMetadata;
7
use Psi\Component\ContentType\Metadata\PropertyMetadata;
8
use Metadata\Driver\AbstractFileDriver;
9
10
class XmlDriver extends AbstractFileDriver
11
{
12
    const XML_NAMESPACE = 'http://github.com/psiphp/content-type';
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function loadMetadataFromFile(\ReflectionClass $class, $path)
17
    {
18
        $classMetadata = new ClassMetadata($class->getName());
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
19
20
        $dom = new \DOMDocument('1.0');
21
        $dom->load($path);
22
        $xpath = new \DOMXpath($dom);
23
        $xpath->registerNamespace('psict', self::XML_NAMESPACE);
24
25
        foreach ($xpath->query('//psict:class') as $classEl) {
26
            $classAttr = $classEl->getAttribute('name');
27
28
            if ($classAttr !== $class->getName()) {
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
29
                throw new \InvalidArgumentException(sprintf(
30
                    'Expected class name to be "%s" but it is mapped as "%s"',
31
                    $class->getName(), $classAttr
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
32
                ));
33
            }
34
35
            foreach ($xpath->query('./psict:field', $classEl) as $fieldEl) {
36
                $options = $this->extractOptions($xpath, $fieldEl);
37
                $propertyMetadata = new PropertyMetadata(
38
                    $class->getName(),
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
39
                    $fieldEl->getAttribute('name'),
40
                    $fieldEl->getAttribute('type'),
41
                    $fieldEl->getAttribute('role'),
42
                    $fieldEl->getAttribute('group'),
43
                    $options
44
                );
45
46
                $classMetadata->addPropertyMetadata($propertyMetadata);
47
            }
48
        }
49
50
        return $classMetadata;
51
    }
52
53
    private function extractOptions(\DOMXPath $xpath, \DOMElement $fieldEl)
54
    {
55
        $options = [];
56
57
        foreach ($xpath->query('./psict:option', $fieldEl) as $optionEl) {
58
            if ($optionEl->getAttribute('type') === 'collection') {
59
                $options[$optionEl->getAttribute('name')] = $this->extractOptions($xpath, $optionEl);
60
                continue;
61
            }
62
63
            $options[$optionEl->getAttribute('name')] = $optionEl->nodeValue;
64
        }
65
66
        return $options;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getExtension()
73
    {
74
        return 'xml';
75
    }
76
77
}
78
79