Passed
Branch master (268bdc)
by Gilles
01:26
created

Tag::getAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace PHPHtmlParser\Dom;
3
4
use PHPHtmlParser\Dom;
5
use stringEncode\Encode;
6
7
/**
8
 * Class Tag
9
 *
10
 * @package PHPHtmlParser\Dom
11
 */
12
class Tag
13
{
14
15
    /**
16
     * The name of the tag.
17
     *
18
     * @var string
19
     */
20
    protected $name;
21
22
    /**
23
     * The attributes of the tag.
24
     *
25
     * @var array
26
     */
27
    protected $attr = [];
28
29
    /**
30
     * Is this tag self closing.
31
     *
32
     * @var bool
33
     */
34
    protected $selfClosing = false;
35
36
    /**
37
     * If self-closing, will this use a trailing slash. />
38
     *
39
     * @var bool
40
     */
41
    protected $trailingSlash = true;
42
43
    /**
44
     * Tag noise
45
     */
46
    protected $noise = '';
47
48
    /**
49
     * The encoding class to... encode the tags
50
     *
51
     * @var mixed
52
     */
53
    protected $encode = null;
54
55
    /**
56
     * Sets up the tag with a name.
57
     *
58
     * @param $name
59
     */
60 393
    public function __construct(string $name)
61
    {
62 393
        $this->name = $name;
63 393
    }
64
65
    /**
66
     * Magic method to get any of the attributes.
67
     *
68
     * @param string $key
69
     * @return mixed
70
     */
71 12
    public function __get($key)
72
    {
73 12
        return $this->getAttribute($key);
74
    }
75
76
    /**
77
     * Magic method to set any attribute.
78
     *
79
     * @param string $key
80
     * @param mixed $value
81
     */
82 192
    public function __set($key, $value)
83
    {
84 192
        $this->setAttribute($key, $value);
85 192
    }
86
87
    /**
88
     * Returns the name of this tag.
89
     *
90
     * @return string
91
     */
92 267
    public function name(): string
93
    {
94 267
        return $this->name;
95
    }
96
97
    /**
98
     * Sets the tag to be self closing.
99
     *
100
     * @return Tag
101
     * @chainable
102
     */
103 168
    public function selfClosing(): Tag
104
    {
105 168
        $this->selfClosing = true;
106
107 168
        return $this;
108
    }
109
110
111
    /**
112
     * Sets the tag to not use a trailing slash.
113
     *
114
     * @return Tag
115
     * @chainable
116
     */
117 3
    public function noTrailingSlash(): Tag
118
    {
119 3
        $this->trailingSlash = false;
120
121 3
        return $this;
122
    }
123
124
    /**
125
     * Checks if the tag is self closing.
126
     *
127
     * @return bool
128
     */
129 231
    public function isSelfClosing(): bool
130
    {
131 231
        return $this->selfClosing;
132
    }
133
134
    /**
135
     * Sets the encoding type to be used.
136
     *
137
     * @param Encode $encode
138
     * @return void
139
     */
140 192
    public function setEncoding(Encode $encode): void
141
    {
142 192
        $this->encode = $encode;
143 192
    }
144
145
    /**
146
     * Sets the noise for this tag (if any)
147
     *
148
     * @param string $noise
149
     * @return Tag
150
     * @chainable
151
     */
152 3
    public function noise(string $noise): Tag
153
    {
154 3
        $this->noise = $noise;
155
156 3
        return $this;
157
    }
158
159
    /**
160
     * Set an attribute for this tag.
161
     *
162
     * @param string $key
163
     * @param string|array $value
164
     * @return Tag
165
     * @chainable
166
     */
167 294
    public function setAttribute(string $key, $value): Tag
168
    {
169 294
        $key = strtolower($key);
170 294
        if ( ! is_array($value)) {
171
            $value = [
172 42
                'value'       => $value,
173
                'doubleQuote' => true,
174
            ];
175
        }
176 294
        $this->attr[$key] = $value;
177
178 294
        return $this;
179
    }
180
181
    /**
182
     * Set inline style attribute value.
183
     *
184
     * @param mixed $attr_key
185
     * @param mixed $attr_value
186
     */
187 6
    public function setStyleAttributeValue($attr_key, $attr_value): void
188
    {
189
190 6
        $style_array = $this->getStyleAttributeArray();
191 6
        $style_array[$attr_key] = $attr_value;
192
193 6
        $style_string = '';
194 6
        foreach ($style_array as $key => $value) {
195 6
            $style_string .= $key . ':' . $value . ';';
196
        }
197
198 6
        $this->setAttribute('style', $style_string);
199 6
    }
200
201
    /**
202
     * Get style attribute in array
203
     *
204
     * @return array
205
     */
206 6
    public function getStyleAttributeArray(): array
207
    {
208 6
        $value = $this->getAttribute('style')['value'];
209
210 6
        if ($value === null) {
211 6
            return [];
212
        }
213
214 3
        $value = explode(';', substr(trim($value), 0, -1));
215 3
        $result = [];
216 3
        foreach ($value as $attr) {
217 3
            $attr = explode(':', $attr);
218 3
            $result[$attr[0]] = $attr[1];
219
        }
220
221 3
        return $result;
222
    }
223
224
225
226
    /**
227
     * Removes an attribute from this tag.
228
     *
229
     * @param mixed $key
230
     * @return void
231
     */
232 6
    public function removeAttribute($key)
233
    {
234 6
        $key = strtolower($key);
235 6
        unset($this->attr[$key]);
236 6
    }
237
238
    /**
239
     * Removes all attributes on this tag.
240
     *
241
     * @return void
242
     */
243 6
    public function removeAllAttributes()
244
    {
245 6
        $this->attr = [];
246 6
    }
247
248
    /**
249
     * Sets the attributes for this tag
250
     *
251
     * @param array $attr
252
     * @return $this
253
     */
254 75
    public function setAttributes(array $attr)
255
    {
256 75
        foreach ($attr as $key => $value) {
257 75
            $this->setAttribute($key, $value);
258
        }
259
260 75
        return $this;
261
    }
262
263
    /**
264
     * Returns all attributes of this tag.
265
     *
266
     * @return array
267
     */
268 9
    public function getAttributes()
269
    {
270 9
        $return = [];
271 9
        foreach ($this->attr as $attr => $info) {
272 3
            $return[$attr] = $this->getAttribute($attr);
273
        }
274
275 9
        return $return;
276
    }
277
278
    /**
279
     * Returns an attribute by the key
280
     *
281
     * @param string $key
282
     * @return mixed
283
     */
284 279
    public function getAttribute(string $key)
285
    {
286 279
        if ( ! isset($this->attr[$key])) {
287 156
            return null;
288
        }
289 240
        $value = $this->attr[$key]['value'];
290 240
        if (is_string($value) && ! is_null($this->encode)) {
291
            // convert charset
292 135
            $this->attr[$key]['value'] = $this->encode->convert($value);
293
        }
294
295 240
        return $this->attr[$key];
296
    }
297
298
    /**
299
     * Returns TRUE if node has attribute
300
     *
301
     * @param string $key
302
     * @return bool
303
     */
304 84
    public function hasAttribute(string $key)
305
    {
306 84
        return isset($this->attr[$key]);
307
    }
308
309
    /**
310
     * Generates the opening tag for this object.
311
     *
312
     * @return string
313
     */
314 138
    public function makeOpeningTag()
315
    {
316 138
        $return = '<'.$this->name;
317
318
        // add the attributes
319 138
        foreach ($this->attr as $key => $info) {
320 120
            $info = $this->getAttribute($key);
321 120
            $val  = $info['value'];
322 120
            if (is_null($val)) {
323 18
                $return .= ' '.$key;
324 114
            } elseif ($info['doubleQuote']) {
325 99
                $return .= ' '.$key.'="'.$val.'"';
326
            } else {
327 90
                $return .= ' '.$key.'=\''.$val.'\'';
328
            }
329
        }
330
331 138
        if ($this->selfClosing && $this->trailingSlash) {
332 78
            return $return.' />';
333
        } else {
334 123
            return $return.'>';
335
        }
336
    }
337
338
    /**
339
     * Generates the closing tag for this object.
340
     *
341
     * @return string
342
     */
343 123
    public function makeClosingTag()
344
    {
345 123
        if ($this->selfClosing) {
346 3
            return '';
347
        }
348
349 120
        return '</'.$this->name.'>';
350
    }
351
}
352