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\Core\Metadata\Driver; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Metadata\ClassMetadata; |
14
|
|
|
use Cubiche\Core\Metadata\ClassMetadataInterface; |
15
|
|
|
use Cubiche\Core\Metadata\Exception\MappingException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* AbstractXmlDriver class. |
19
|
|
|
* |
20
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractXmlDriver extends AbstractFileDriver |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
protected function loadMetadataFromFile($className, $file) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/* @var $xmlRoot \SimpleXMLElement */ |
30
|
|
|
$xmlRoot = $this->getElement($className, $file); |
31
|
|
|
$xmlRoot->registerXPathNamespace('cubiche', 'cubiche'); |
32
|
|
|
|
33
|
|
|
try { |
34
|
|
|
$classMetadata = new ClassMetadata($className); |
35
|
|
|
} catch (\ReflectionException $e) { |
36
|
|
|
throw MappingException::classNotFound($className); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->addMetadataFor($xmlRoot, $classMetadata); |
40
|
|
|
|
41
|
|
|
return $classMetadata; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \SimpleXMLElement $item |
46
|
|
|
* @param array $default |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
protected function getMappingAttributes(\SimpleXMLElement $item, array $default = array()) |
51
|
|
|
{ |
52
|
|
|
$mapping = $default; |
53
|
|
|
foreach ($item->attributes() as $key => $value) { |
54
|
|
|
$mapping[$key] = (string) $value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $mapping; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
protected function getExtension() |
64
|
|
|
{ |
65
|
|
|
return '.xml'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \SimpleXMLElement $xmlRoot |
70
|
|
|
* @param ClassMetadataInterface $classMetadata |
71
|
|
|
*/ |
72
|
|
|
abstract protected function addMetadataFor(\SimpleXMLElement $xmlRoot, ClassMetadataInterface $classMetadata); |
73
|
|
|
} |
74
|
|
|
|
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.