Completed
Push — master ( 56a459...0ee720 )
by Randy
03:34
created

HydrateProcedure::hydrateAttributesOf()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 6
nop 1
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
use Dgame\Soap\Element;
6
use Dgame\Soap\XmlElement;
7
use Dgame\Soap\XmlNode;
8
use function Dgame\Conditional\debug;
9
10
/**
11
 * Class HydrateProcedure
12
 * @package Dgame\Soap\Hydrator
13
 */
14
final class HydrateProcedure implements HydratorInterface
15
{
16
    const DEBUG_LABEL = 'Debug_Soap_Hydrat';
17
18
    /**
19
     * @var string
20
     */
21
    private $tab;
22
    /**
23
     * @var HydratableInterface
24
     */
25
    private $hydratable;
26
    /**
27
     * @var ClassMapper
28
     */
29
    private $mapper;
30
31
    /**
32
     * Hydrat constructor.
33
     *
34
     * @param ClassMapper $mapper
35
     * @param string|null $tab
36
     */
37
    public function __construct(ClassMapper $mapper, string $tab = null)
38
    {
39
        $this->mapper = $mapper;
40
        $this->tab    = "\t" . $tab;
41
    }
42
43
    /**
44
     * @return HydratableInterface
45
     */
46
    public function getHydratable(): HydratableInterface
47
    {
48
        return $this->hydratable;
49
    }
50
51
    /**
52
     * @return bool
53
     */
54
    public function isValid(): bool
55
    {
56
        return $this->hydratable !== null;
57
    }
58
59
    /**
60
     * @param Element $element
61
     */
62
    public function hydrateElement(Element $element)
63
    {
64
        debug(self::DEBUG_LABEL)->output($this->tab . ' - Element: ' . $element->getName());
65
66
        $this->hydrate($element);
67
    }
68
69
    /**
70
     * @param XmlElement $element
71
     */
72
    public function hydrateXmlElement(XmlElement $element)
73
    {
74
        debug(self::DEBUG_LABEL)->output($this->tab . ' - XmlElement: ' . $element->getName());
75
76
        $this->hydrate($element);
77
    }
78
79
    /**
80
     * @param XmlNode $node
81
     */
82
    public function hydrateXmlNode(XmlNode $node)
83
    {
84
        debug(self::DEBUG_LABEL)->output($this->tab . ' - XmlNode: ' . $node->getName());
85
86
        $this->hydrate($node);
87
        $this->hydrateChildrenOf($node);
88
    }
89
90
    /**
91
     * @param Element $element
92
     */
93
    private function hydrate(Element $element)
94
    {
95
        $this->hydratable = $this->mapper->getInstanceOf($element->getName());
96
        if ($this->isValid()) {
97
            $this->hydrateAttributesOf($element);
98
        }
99
    }
100
101
    /**
102
     * @param Element $element
103
     */
104
    private function hydrateAttributesOf(Element $element)
105
    {
106
        foreach ($element->getAttributes() as $attribute) {
107
            if ($attribute->hasValue()) {
108
                $this->hydratable->assignAttribute($attribute);
109
            }
110
        }
111
112
        if ($element->hasValue()) {
113
            $this->hydratable->assign('value', $element->getValue());
114
        }
115
    }
116
117
    /**
118
     * @param XmlNode $node
119
     */
120
    private function hydrateChildrenOf(XmlNode $node)
121
    {
122
        foreach ($node->getChildren() as $child) {
123
            $this->hydrateChild($child);
124
        }
125
    }
126
127
    /**
128
     * @param Element $element
129
     */
130
    private function hydrateChild(Element $element)
131
    {
132
        $hydrat = new self($this->mapper, $this->tab);
133
        $element->hydration($hydrat);
134
135
        if ($this->isValid() && $hydrat->isValid()) {
136
            $this->hydratable->assignHydratable($hydrat->getHydratable());
0 ignored issues
show
Documentation introduced by
$hydrat->getHydratable() is of type object<Dgame\Soap\Hydrator\HydratableInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
137
        } else if ($this->isValid()) {
138
            $this->hydratable->assignElement($element);
139
        } else if ($hydrat->isValid()) {
140
            $this->hydratable = $hydrat->getHydratable();
141
        }
142
    }
143
}