1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Metadata\Tests\Fixtures\Driver; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Metadata\ClassMetadataInterface; |
15
|
|
|
use Cubiche\Core\Metadata\Driver\AbstractXmlDriver; |
16
|
|
|
use Cubiche\Core\Metadata\PropertyMetadata; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* XmlDriver class. |
20
|
|
|
* |
21
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class XmlDriver extends AbstractXmlDriver |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
protected function loadMappingFile($file) |
29
|
|
|
{ |
30
|
|
|
$result = array(); |
31
|
|
|
$xmlElement = simplexml_load_file($file); |
32
|
|
|
foreach (array('aggregate-root', 'entity') as $type) { |
33
|
|
|
if (isset($xmlElement->$type)) { |
34
|
|
|
foreach ($xmlElement->$type as $documentElement) { |
35
|
|
|
$documentName = (string) $documentElement['name']; |
36
|
|
|
$result[$documentName] = $documentElement; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $result; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
protected function addMetadataFor(\SimpleXMLElement $xmlRoot, ClassMetadataInterface $classMetadata) |
48
|
|
|
{ |
49
|
|
|
if (isset($xmlRoot->field)) { |
50
|
|
|
foreach ($xmlRoot->field as $field) { |
51
|
|
|
$mapping = $this->getMappingAttributes($field); |
52
|
|
|
$booleanAttributes = array('id', 'unique'); |
53
|
|
View Code Duplication |
foreach ($mapping as $key => $value) { |
|
|
|
|
54
|
|
|
if (in_array($key, $booleanAttributes)) { |
55
|
|
|
$mapping[$key] = ('true' === $mapping[$key]); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->addFieldMapping($classMetadata, $mapping); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ClassMetadataInterface $classMetadata |
66
|
|
|
* @param array $mapping |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
private function addFieldMapping(ClassMetadataInterface $classMetadata, $mapping) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if (isset($mapping['name'])) { |
71
|
|
|
$name = $mapping['name']; |
72
|
|
|
if (!isset($mapping['fieldName'])) { |
73
|
|
|
$mapping['fieldName'] = $name; |
74
|
|
|
} |
75
|
|
|
} elseif (isset($mapping['fieldName'])) { |
76
|
|
|
$name = $mapping['fieldName']; |
77
|
|
|
if (!isset($mapping['name'])) { |
78
|
|
|
$mapping['name'] = $name; |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
|
|
throw new \InvalidArgumentException('Cannot infer a name from the mapping'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$isIdentifier = false; |
85
|
|
|
if (isset($mapping['id']) && $mapping['id'] === true) { |
86
|
|
|
$isIdentifier = true; |
87
|
|
|
$mapping['name'] = '_id'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$propertyMetadata = new PropertyMetadata( |
91
|
|
|
$classMetadata->className(), |
92
|
|
|
$mapping['fieldName'] |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$propertyMetadata->addMetadata('identifier', $isIdentifier); |
96
|
|
|
$propertyMetadata->addMetadata('name', $mapping['name']); |
97
|
|
|
|
98
|
|
|
if (isset($mapping['type'])) { |
99
|
|
|
$propertyMetadata->addMetadata('type', $mapping['type']); |
100
|
|
|
if (isset($mapping['of'])) { |
101
|
|
|
$propertyMetadata->addMetadata('of', $mapping['of']); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$classMetadata->addPropertyMetadata($propertyMetadata); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.