Completed
Push — master ( ec5659...28f2f4 )
by Pol
01:27
created

Attribute::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace drupol\htmltag\Attribute;
4
5
use drupol\htmltag\AbstractBaseHtmlTagObject;
6
7
/**
8
 * Class Attribute.
9
 */
10
class Attribute extends AbstractBaseHtmlTagObject implements AttributeInterface
11
{
12
    /**
13
     * Store the attribute name.
14
     *
15
     * @var string|null
16
     */
17
    private $name;
18
19
    /**
20
     * Store the attribute value.
21
     *
22
     * @var array|null
23
     */
24
    private $values;
25
26
    /**
27
     * Attribute constructor.
28
     *
29
     * @param string $name
30
     *   The attribute name.
31
     * @param string|string[]|mixed[] ...$value
32
     *   The attribute value.
33
     */
34 35
    public function __construct($name, ...$value)
35
    {
36 35
        $this->name = trim($name);
37 35
        $this->set($value);
38 35
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 35
    public function set(...$value)
44
    {
45 35
        $this->values = $value;
46
47 35
        return $this;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 28
    public function getName()
54
    {
55 28
        return $this->name;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 27
    public function getValuesAsArray()
62
    {
63 27
        return array_values(
64 27
            array_map(
65
                function ($item) {
66 24
                    return addcslashes($item, '\"');
67 27
                },
68 27
                $this->preprocess(
69 27
                    $this->normalizeValue($this->values),
70 27
                    $this->getName()
71
                )
72
            )
73
        );
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 23
    public function render()
80
    {
81 23
        $output = (string) $this->name;
82
83 23
        if (!$this->isBoolean()) {
84 20
            $output .= '="' . implode(' ', $this->getValuesAsArray()) . '"';
85
        }
86
87 23
        return $output;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function __toString()
94
    {
95
        return $this->render();
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 23
    public function isBoolean()
102
    {
103 23
        if ([] === $this->getValuesAsArray()) {
104 13
            $this->values = null;
105
        }
106
107 23
        return null === $this->values;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 25
    public function append(...$value)
114
    {
115 25
        $this->values[] = $value;
116
117 25
        return $this;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 4
    public function remove(...$value)
124
    {
125 4
        $this->values = array_diff(
126 4
            $this->normalizeValue($this->values),
127 4
            $this->normalizeValue($value)
128
        );
129
130 4
        return $this;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 2
    public function replace($original, ...$replacement)
137
    {
138 2
        $count_start = count($this->normalizeValue($this->values));
139 2
        $this->remove($original);
140 2
        $count_end = count($this->normalizeValue($this->values));
141
142 2
        if ($count_end !== $count_start) {
143 2
            $this->append($replacement);
144
        }
145
146 2
        return $this;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 4
    public function contains(...$substring)
153
    {
154 4
        $values = $this->normalizeValue($this->values);
155
156 4
        $array = array_map(
157
            function ($substring_item) use ($values) {
158 4
                return in_array($substring_item, $values, true);
159 4
            },
160 4
            $this->normalizeValue($substring)
161
        );
162
163 4
        return $array &&
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
164 4
            array_reduce(
165 4
                $array,
166
                function ($left, $right) {
167 4
                    return $left && $right;
168 4
                },
169 4
                true
170
            );
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176 2
    public function setBoolean($boolean = true)
177
    {
178 2
        if (true === $boolean) {
179 2
            $this->values = null;
180
        }
181
182 2
        return $this;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 1
    public function delete()
189
    {
190 1
        $this->name = null;
191 1
        $this->values = null;
192
193 1
        return $this;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 1
    public function alter(callable ...$closures)
200
    {
201 1
        $name = $this->getName();
202
203 1
        foreach ($closures as $closure) {
204 1
            $values = $this->normalizeValue($this->values);
205 1
            $this->values = call_user_func_array($closure, [&$values, $name]);
206
        }
207
208 1
        return $this;
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214 1
    public function serialize()
215
    {
216 1
        return serialize([
217 1
            'name' => $this->getName(),
218 1
            'values' => $this->getValuesAsArray(),
219
        ]);
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225 1
    public function unserialize($serialized)
226
    {
227 1
        $unserialized = unserialize($serialized);
228
229 1
        $this->name = $unserialized['name'];
230 1
        $this->values = $unserialized['values'];
231 1
    }
232
}
233