1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 20.08.16 at 12:39 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\containerxml; |
7
|
|
|
|
8
|
|
|
use samsonframework\container\AbstractMetadataCollector; |
9
|
|
|
use samsonframework\containercollection\CollectionClassResolver; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* XML class metadata collector. |
13
|
|
|
* Class resolves and collects class metadata from XML string. |
14
|
|
|
* |
15
|
|
|
* @author Vitaly Egorov <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class XmlMetadataCollector extends AbstractMetadataCollector |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
2 |
|
public function collect($xmlConfig, array $classesMetadata = []) : array |
23
|
|
|
{ |
24
|
|
|
// Iterate config and resolve single instance |
25
|
2 |
|
foreach ($this->xml2array(new \SimpleXMLElement($xmlConfig)) as $key => $classesArrayData) { |
26
|
2 |
|
if ($key === CollectionClassResolver::KEY) { |
27
|
|
|
// If we have only one instance we need to add array |
28
|
|
|
/** @var array $classesArrayData */ |
29
|
2 |
|
foreach (!array_key_exists(0, $classesArrayData) ? [$classesArrayData] : $classesArrayData as $classArrayData) { |
30
|
2 |
|
$classMetadata = $this->resolver->resolve($classArrayData); |
|
|
|
|
31
|
|
|
// TODO: This is only work aroud right now |
32
|
2 |
|
$classesMetadata[$classMetadata->className] = $this->resolver->resolve($classArrayData, $classesMetadata[$classMetadata->className] ?? null); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
return $classesMetadata; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* function xml2array |
42
|
|
|
* |
43
|
|
|
* This function is part of the PHP manual. |
44
|
|
|
* |
45
|
|
|
* The PHP manual text and comments are covered by the Creative Commons |
46
|
|
|
* Attribution 3.0 License, copyright (c) the PHP Documentation Group |
47
|
|
|
* |
48
|
|
|
* @param \SimpleXMLElement|array $xmlObject |
49
|
|
|
* @param array $out |
50
|
|
|
* |
51
|
|
|
* @return array XML converted array |
52
|
|
|
* |
53
|
|
|
* @author k dot antczak at livedata dot pl |
54
|
|
|
* @date 2011-04-22 06:08 UTC |
55
|
|
|
* @link http://www.php.net/manual/en/ref.simplexml.php#103617 |
56
|
|
|
* @license http://www.php.net/license/index.php#doc-lic |
57
|
|
|
* @license http://creativecommons.org/licenses/by/3.0/ |
58
|
|
|
* @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0> |
59
|
|
|
*/ |
60
|
2 |
|
protected function xml2array($xmlObject, array $out = []) : array |
61
|
|
|
{ |
62
|
2 |
|
foreach ((array)$xmlObject as $index => $node) { |
63
|
2 |
|
$out[$index] = (is_object($node) || is_array($node)) ? $this->xml2array($node) : $node; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
return $out; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: