Completed
Pull Request — master (#106)
by
unknown
09:10
created

Tag   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 29
c 0
b 0
f 0
lcom 2
cbo 0
dl 0
loc 279
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __get() 0 4 1
A __set() 0 4 1
A name() 0 4 1
A selfClosing() 0 6 1
A noTrailingSlash() 0 6 1
A isSelfClosing() 0 4 1
A setEncoding() 0 4 1
A noise() 0 6 1
A setAttribute() 0 13 2
A removeAttribute() 0 5 1
A removeAllAttributes() 0 4 1
A setAttributes() 0 8 2
A getAttributes() 0 9 2
A getAttribute() 0 13 4
B makeOpeningTag() 0 23 6
A makeClosingTag() 0 8 2
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
     * Removes an attribute from this tag.
178
     *
179
     * @param $key
180
     * @return void
181
     */
182
    public function removeAttribute($key)
183
    {
184
        $key = strtolower($key);
185
        unset($this->attr[$key]);
186
    }
187
188
    /**
189
     * Removes all attributes on this tag.
190
     *
191
     * @return void
192
     */
193
    public function removeAllAttributes()
194
    {
195
        $this->attr = [];
196
    }
197
198
    /**
199
     * Sets the attributes for this tag
200
     *
201
     * @param array $attr
202
     * @return $this
203
     */
204
    public function setAttributes(array $attr)
205
    {
206
        foreach ($attr as $key => $value) {
207
            $this->setAttribute($key, $value);
208
        }
209
210
        return $this;
211
    }
212
213
    /**
214
     * Returns all attributes of this tag.
215
     *
216
     * @return array
217
     */
218
    public function getAttributes()
219
    {
220
        $return = [];
221
        foreach ($this->attr as $attr => $info) {
222
            $return[$attr] = $this->getAttribute($attr);
223
        }
224
225
        return $return;
226
    }
227
228
    /**
229
     * Returns an attribute by the key
230
     *
231
     * @param string $key
232
     * @return mixed
233
     */
234
    public function getAttribute($key)
235
    {
236
        if ( ! isset($this->attr[$key])) {
237
            return null;
238
        }
239
        $value = $this->attr[$key]['value'];
240
        if (is_string($value) && ! is_null($this->encode)) {
241
            // convert charset
242
            $this->attr[$key]['value'] = $this->encode->convert($value);
243
        }
244
245
        return $this->attr[$key];
246
    }
247
248
    /**
249
     * Generates the opening tag for this object.
250
     *
251
     * @return string
252
     */
253
    public function makeOpeningTag()
254
    {
255
        $return = '<'.$this->name;
256
257
        // add the attributes
258
        foreach ($this->attr as $key => $info) {
259
            $info = $this->getAttribute($key);
260
            $val  = $info['value'];
261
            if (is_null($val)) {
262
                $return .= ' '.$key;
263
            } elseif ($info['doubleQuote']) {
264
                $return .= ' '.$key.'="'.$val.'"';
265
            } else {
266
                $return .= ' '.$key.'=\''.$val.'\'';
267
            }
268
        }
269
270
        if ($this->selfClosing && $this->trailingSlash) {
271
            return $return.' />';
272
        } else {
273
            return $return.'>';
274
        }
275
    }
276
277
    /**
278
     * Generates the closing tag for this object.
279
     *
280
     * @return string
281
     */
282
    public function makeClosingTag()
283
    {
284
        if ($this->selfClosing) {
285
            return '';
286
        }
287
288
        return '</'.$this->name.'>';
289
    }
290
}
291