Test Failed
Pull Request — master (#160)
by
unknown
11:10
created

Tag::setAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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