AbstractDataType::propagateDomain()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 2
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Items\DataTypes;
15
16
use InvalidArgumentException;
17
use Wszetko\Sitemap\Interfaces\DataType;
18
use Wszetko\Sitemap\Traits\Domain;
19
use Wszetko\Sitemap\Traits\Required;
20
21
/**
22
 * Class AbstractDataType.
23
 *
24
 * @package Wszetko\Sitemap\Items\DataTypes
25
 */
26
abstract class AbstractDataType implements DataType
27
{
28
    use Required;
29
    use Domain;
30
31
    /**
32
     * Property that handle value.
33
     *
34
     * @var mixed
35
     */
36
    protected $value;
37
38
    /**
39
     * Property that handle attributes.
40
     *
41
     * @var array
42
     */
43
    protected $attributes = [];
44
45
    /**
46
     * DataType name.
47
     *
48
     * @var string
49
     */
50
    private $name;
51
52
    /**
53
     * AbstractDataType constructor.
54
     *
55
     * @param string $name
56
     */
57 506
    public function __construct(string $name)
58
    {
59 506
        $this->name = $name;
60 506
    }
61
62
    /**
63
     * To clone properties of object with no reference.
64
     */
65 92
    public function __clone()
66
    {
67 92
        foreach ($this->attributes as $attribute => $value) {
68 58
            if ('object' == gettype($value)) {
69 58
                $this->attributes[$attribute] = clone $this->attributes[$attribute];
70
            }
71
        }
72 92
    }
73
74
    /**
75
     * Return name od DataType.
76
     *
77
     * @return string
78
     */
79 390
    public function getName(): string
80
    {
81 390
        return $this->name;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87 410
    public function getValue()
88
    {
89 410
        $value = $this->value;
90
91
        try {
92 410
            $attributes = $this->getAttributes();
93
94 410
            if ([] !== $attributes) {
95 410
                return [$value => $attributes];
96
            }
97 4
        } catch (InvalidArgumentException $e) {
98 4
            return null;
99
        }
100
101 392
        return $value;
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107 500
    public function setValue($value, $parameters = []): DataType
108
    {
109 500
        $this->value = $value;
110
111 500
        foreach ($parameters as $key => $attribute) {
112 98
            if (null !== $attribute && '' !== $attribute) {
113 82
                $attr = array_keys($this->attributes)[$key];
114
115 82
                if ('' !== $attr) {
116 82
                    $this->attributes[$attr]->setValue($attribute);
117
                }
118
            }
119
        }
120
121 500
        return $this;
122
    }
123
124
    /**
125
     * Add multiple attrubutes do DataType.
126
     *
127
     * @param mixed $attributes
128
     *
129
     * @return static
130
     */
131 506
    public function addAttributes($attributes): self
132
    {
133 506
        foreach ($attributes as $name => $dataType) {
134 330
            $this->attributes[$name] = new $dataType($name);
135
        }
136
137 506
        return $this;
138
    }
139
140
    /**
141
     * Return object used to handle provided attribute.
142
     *
143
     * @param mixed $name
144
     *
145
     * @return null|AbstractDataType
146
     */
147 330
    public function getAttribute($name): ?AbstractDataType
148
    {
149 330
        return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
150
    }
151
152
    /**
153
     * Return all attributes values.
154
     *
155
     * @throws \InvalidArgumentException
156
     *
157
     * @return array
158
     */
159 446
    public function getAttributes(): array
160
    {
161 446
        $attributes = [];
162
163 446
        foreach ($this->attributes as $name => $value) {
164 96
            $this->propagateDomain($value);
165
166 96
            if (null !== $value->getValue() && '' !== $value->getValue()) {
167 80
                $attributes[$name] = $value->getValue();
168 44
            } elseif ($value->isRequired()) {
169 4
                throw new InvalidArgumentException('Lack of required value');
170
            }
171
        }
172
173 446
        return $attributes;
174
    }
175
176
    /**
177
     * Pass domain name to attributes if necessary.
178
     *
179
     * @param mixed $target
180
     */
181 130
    public function propagateDomain(&$target): void
182
    {
183 130
        if (method_exists($target, 'setDomain') && null !== $this->getDomain()) {
184 34
            $target->setDomain($this->getDomain());
185
        }
186 130
    }
187
}
188