|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psi\Component\ContentType\Metadata\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Metadata\Driver\AbstractFileDriver; |
|
6
|
|
|
use Psi\Component\ContentType\Metadata\ClassMetadata; |
|
7
|
|
|
use Psi\Component\ContentType\Metadata\PropertyMetadata; |
|
8
|
|
|
|
|
9
|
|
|
class XmlDriver extends AbstractFileDriver |
|
10
|
|
|
{ |
|
11
|
|
|
const XML_NAMESPACE = 'http://github.com/psiphp/content-type'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* {@inheritdoc} |
|
15
|
|
|
*/ |
|
16
|
|
|
public function loadMetadataFromFile(\ReflectionClass $class, $path) |
|
17
|
|
|
{ |
|
18
|
|
|
$classMetadata = new ClassMetadata($class->getName()); |
|
|
|
|
|
|
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()) { |
|
|
|
|
|
|
29
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
30
|
|
|
'Expected class name to be "%s" but it is mapped as "%s"', |
|
31
|
|
|
$class->getName(), $classAttr |
|
|
|
|
|
|
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(), |
|
|
|
|
|
|
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
|
|
|
|