Completed
Push — master ( 821e5b...8cc443 )
by Vitaly
24s
created

XmlResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 84
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A resolveConfig() 0 14 3
A resolve() 0 14 1
A xml2array() 0 8 4
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 14.08.16 at 15:55
5
 */
6
namespace samsonframework\container\resolver;
7
8
use samsonframework\container\collection\CollectionClassResolver;
9
use samsonframework\container\collection\CollectionPropertyResolver;
10
use samsonframework\container\metadata\ClassMetadata;
11
use samsonframework\container\metadata\PropertyMetadata;
12
13
/**
14
 * XML dependency injection container configuration.
15
 *
16
 * @author Vitaly Iegorov <[email protected]>
17
 * @author Ruslan Molodyko  <[email protected]>
18
 */
19
class XmlResolver implements ResolverInterface
20
{
21
    /** @var CollectionClassResolver */
22
    protected $classResolver;
23
24
    /** @var CollectionClassResolver */
25
    protected $propertyResolver;
26
27
    /**
28
     * AnnotationResolver constructor.
29
     *
30
     * @param CollectionClassResolver $classResolver
31
     * @param CollectionPropertyResolver $propertyResolver
32
     */
33 1
    public function __construct(CollectionClassResolver $classResolver, CollectionPropertyResolver $propertyResolver)
34
    {
35 1
        $this->classResolver = $classResolver;
36 1
        $this->propertyResolver = $propertyResolver;
0 ignored issues
show
Documentation Bug introduced by
It seems like $propertyResolver of type object<samsonframework\c...ectionPropertyResolver> is incompatible with the declared type object<samsonframework\c...ollectionClassResolver> of property $propertyResolver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        //$this->methodResolver = $methodResolver;
38 1
    }
39
40
    /**
41
     * Resolve xml config
42
     *
43
     * @param string $xmlConfig
44
     * @return array
45
     */
46 1
    public function resolveConfig($xmlConfig) : array
47
    {
48 1
        $listClassMetadata = [];
49
        // Convert xml to array
50 1
        $arrayData = $this->xml2array(new \SimpleXMLElement($xmlConfig));
51
        // Iterate config and resolve single instance
52 1
        foreach ($arrayData as $key => $classArrayData) {
53 1
            if ($key === CollectionClassResolver::KEY) {
54
                // Store metadata
55 1
                $listClassMetadata[] = $this->resolve($classArrayData);
56
            }
57
        }
58 1
        return $listClassMetadata;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1
    public function resolve($classArrayData, string $identifier = null) : ClassMetadata
65
    {
66
        // Create and fill class metadata base fields
67 1
        $classMetadata = new ClassMetadata();
68
69
        // Resolve class definition annotations
70 1
        $this->classResolver->resolve($classArrayData, $classMetadata);
71
        // Resolve class properties annotations
72 1
        $this->propertyResolver->resolve($classArrayData, $classMetadata);
73
        // Resolve class methods annotations
74
        //$this->methodResolver->resolve($classData, $classMetadata);
75
76 1
        return $classMetadata;
77
    }
78
79
    /**
80
     * function xml2array
81
     *
82
     * This function is part of the PHP manual.
83
     *
84
     * The PHP manual text and comments are covered by the Creative Commons
85
     * Attribution 3.0 License, copyright (c) the PHP Documentation Group
86
     *
87
     * @author  k dot antczak at livedata dot pl
88
     * @date    2011-04-22 06:08 UTC
89
     * @link    http://www.php.net/manual/en/ref.simplexml.php#103617
90
     * @license http://www.php.net/license/index.php#doc-lic
91
     * @license http://creativecommons.org/licenses/by/3.0/
92
     * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0>
93
     */
94 1
    protected function xml2array($xmlObject, $out = array())
95
    {
96 1
        foreach ((array)$xmlObject as $index => $node) {
97 1
            $out[$index] = (is_object($node) || is_array($node)) ? $this->xml2array($node) : $node;
98
        }
99
100 1
        return $out;
101
    }
102
}
103