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

Hydratable::assignAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
use Dgame\Soap\Attribute\Attribute;
6
use Dgame\Soap\Element;
7
8
/**
9
 * Class Hydratable
10
 * @package Dgame\Soap\Hydrator
11
 */
12
class Hydratable implements HydratableInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    private $attributes = [];
18
19
    /**
20
     * @param Attribute $attribute
21
     */
22
    final public function assignAttribute(Attribute $attribute)
23
    {
24
        if ($attribute->hasValue()) {
25
            $this->assign($attribute->getName(), $attribute->getValue());
26
        }
27
    }
28
29
    /**
30
     * @param Element $element
31
     */
32
    final public function assignElement(Element $element)
33
    {
34
        if ($element->hasValue()) {
35
            $this->assign($element->getName(), $element->getValue());
36
        }
37
    }
38
39
    /**
40
     * @param string $name
41
     * @param string $value
42
     */
43
    final public function assign(string $name, string $value)
44
    {
45
        if (!Method::of($name, $this)->assign($value)) {
46
            $this->setAttribute($name, $value);
47
        }
48
    }
49
50
    /**
51
     * @param HydratableInterface $hydrat
52
     *
53
     * @return bool
54
     */
55
    final public function assignHydratable(HydratableInterface $hydrat): bool
56
    {
57
        return Method::of($hydrat->getClassName(), $this)->assign($hydrat);
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    final public function getClassName(): string
64
    {
65
        static $name = null;
66
        if ($name === null) {
67
            $name = string(static::class)->lastSegment('\\');
68
        }
69
70
        return $name;
71
    }
72
73
    /**
74
     * @param string $name
75
     * @param        $value
76
     */
77
    final public function setAttribute(string $name, $value)
78
    {
79
        $this->attributes[$name] = $value;
80
    }
81
82
    /**
83
     * @param string $name
84
     *
85
     * @return mixed
86
     */
87
    final public function getAttribute(string $name)
88
    {
89
        return $this->attributes[$name];
90
    }
91
92
    /**
93
     * @param string $name
94
     *
95
     * @return bool
96
     */
97
    final public function hasAttribute(string $name): bool
98
    {
99
        return array_key_exists($name, $this->attributes);
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    final public function getAttributes(): array
106
    {
107
        return $this->attributes;
108
    }
109
110
    /**
111
     * @param array $attributes
112
     */
113
    final public function setAttributes(array $attributes)
114
    {
115
        $this->attributes = $attributes;
116
    }
117
}