Completed
Push — master ( 5ed2e8...45d18c )
by Gilles
02:48
created

Tag::removeAllAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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
     * Tag noise
38
     */
39
    protected $noise = '';
40
41
    /**
42
     * The encoding class to... encode the tags
43
     *
44
     * @var mixed
45
     */
46
    protected $encode = null;
47
48
    /**
49
     * Sets up the tag with a name.
50
     *
51
     * @param $name
52
     */
53
    public function __construct($name)
54
    {
55
        $this->name = $name;
56
    }
57
58
    /**
59
     * Magic method to get any of the attributes.
60
     *
61
     * @param string $key
62
     * @return mixed
63
     */
64
    public function __get($key)
65
    {
66
        return $this->getAttribute($key);
67
    }
68
69
    /**
70
     * Magic method to set any attribute.
71
     *
72
     * @param string $key
73
     * @param mixed $value
74
     */
75
    public function __set($key, $value)
76
    {
77
        $this->setAttribute($key, $value);
78
    }
79
80
    /**
81
     * Returns the name of this tag.
82
     *
83
     * @return string
84
     */
85
    public function name()
86
    {
87
        return $this->name;
88
    }
89
90
    /**
91
     * Sets the tag to be self closing.
92
     *
93
     * @return $this
94
     */
95
    public function selfClosing()
96
    {
97
        $this->selfClosing = true;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Checks if the tag is self closing.
104
     *
105
     * @return bool
106
     */
107
    public function isSelfClosing()
108
    {
109
        return $this->selfClosing;
110
    }
111
112
    /**
113
     * Sets the encoding type to be used.
114
     *
115
     * @param Encode $encode
116
     */
117
    public function setEncoding(Encode $encode)
118
    {
119
        $this->encode = $encode;
120
    }
121
122
    /**
123
     * Sets the noise for this tag (if any)
124
     *
125
     * @param $noise
126
     * @return $this
127
     */
128
    public function noise($noise)
129
    {
130
        $this->noise = $noise;
131
132
        return $this;
133
    }
134
135
    /**
136
     * Set an attribute for this tag.
137
     *
138
     * @param string $key
139
     * @param string|array $value
140
     * @return $this
141
     */
142
    public function setAttribute($key, $value)
143
    {
144
        $key = strtolower($key);
145
        if ( ! is_array($value)) {
146
            $value = [
147
                'value'       => $value,
148
                'doubleQuote' => true,
149
            ];
150
        }
151
        $this->attr[$key] = $value;
152
153
        return $this;
154
    }
155
156
    /**
157
     * Removes an attribute from this tag.
158
     *
159
     * @param $key
160
     * @return void
161
     */
162
    public function removeAttribute($key)
163
    {
164
        $key = strtolower($key);
165
        unset($this->attr[$key]);
166
    }
167
168
    /**
169
     * Removes all attributes on this tag.
170
     *
171
     * @return void
172
     */
173
    public function removeAllAttributes()
174
    {
175
        $this->attr = [];
176
    }
177
178
    /**
179
     * Sets the attributes for this tag
180
     *
181
     * @param array $attr
182
     * @return $this
183
     */
184
    public function setAttributes(array $attr)
185
    {
186
        foreach ($attr as $key => $value) {
187
            $this->setAttribute($key, $value);
188
        }
189
190
        return $this;
191
    }
192
193
    /**
194
     * Returns all attributes of this tag.
195
     *
196
     * @return array
197
     */
198
    public function getAttributes()
199
    {
200
        $return = [];
201
        foreach ($this->attr as $attr => $info) {
202
            $return[$attr] = $this->getAttribute($attr);
203
        }
204
205
        return $return;
206
    }
207
208
    /**
209
     * Returns an attribute by the key
210
     *
211
     * @param string $key
212
     * @return mixed
213
     */
214
    public function getAttribute($key)
215
    {
216
        if ( ! isset($this->attr[$key])) {
217
            return null;
218
        }
219
        $value = $this->attr[$key]['value'];
220
        if (is_string($value) && ! is_null($this->encode)) {
221
            // convert charset
222
            $this->attr[$key]['value'] = $this->encode->convert($value);
223
        }
224
225
        return $this->attr[$key];
226
    }
227
228
    /**
229
     * Generates the opening tag for this object.
230
     *
231
     * @return string
232
     */
233
    public function makeOpeningTag()
234
    {
235
        $return = '<'.$this->name;
236
237
        // add the attributes
238
        foreach ($this->attr as $key => $info) {
239
            $info = $this->getAttribute($key);
240
            $val  = $info['value'];
241
            if (is_null($val)) {
242
                $return .= ' '.$key;
243
            } elseif ($info['doubleQuote']) {
244
                $return .= ' '.$key.'="'.$val.'"';
245
            } else {
246
                $return .= ' '.$key.'=\''.$val.'\'';
247
            }
248
        }
249
250
        if ($this->selfClosing) {
251
            return $return.' />';
252
        } else {
253
            return $return.'>';
254
        }
255
    }
256
257
    /**
258
     * Generates the closing tag for this object.
259
     *
260
     * @return string
261
     */
262
    public function makeClosingTag()
263
    {
264
        if ($this->selfClosing) {
265
            return '';
266
        }
267
268
        return '</'.$this->name.'>';
269
    }
270
}
271