Completed
Pull Request — master (#56)
by
unknown
04:05
created

Tag::getAttribute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 13
rs 9.2
cc 4
eloc 7
nc 3
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
     * 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 mixed $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
     * Sets the attributes for this tag
158
     *
159
     * @param array $attr
160
     * @return $this
161
     */
162
    public function setAttributes(array $attr)
163
    {
164
        foreach ($attr as $key => $value) {
165
            $this->setAttribute($key, $value);
166
        }
167
168
        return $this;
169
    }
170
171
    /**
172
     * Returns all attributes of this tag.
173
     *
174
     * @return array
175
     */
176
    public function getAttributes()
177
    {
178
        $return = [];
179
        foreach ($this->attr as $attr => $info) {
180
            $return[$attr] = $this->getAttribute($attr);
181
        }
182
183
        return $return;
184
    }
185
186
    /**
187
     * Returns an attribute by the key
188
     *
189
     * @param string $key
190
     * @return mixed
191
     */
192
    public function getAttribute($key)
193
    {
194
        if ( ! isset($this->attr[$key])) {
195
            return null;
196
        }
197
        $value = $this->attr[$key]['value'];
198
        if (is_string($value) AND ! is_null($this->encode)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
199
            // convert charset
200
            $this->attr[$key]['value'] = $this->encode->convert($value);
201
        }
202
203
        return $this->attr[$key];
204
    }
205
206
    /**
207
     * Removes an attribute for this tag
208
     *
209
     * @param string $key
210
     * @return mixed
211
     */
212
    public function removeAttribute($key)
213
    {
214
        $key = strtolower($key);
215
        unset($this->attr[$key]);
216
217
        return $this;
218
    }
219
220
    /**
221
     * Removes all attributes for this tag except the ones in the $keys array
222
     *
223
     * @param array $keys
224
     * @return mixed
225
     */
226
    public function removeAttributes($keys)
227
    {
228
        $attributes = $this->getAttributes();
229
        foreach($attributes as $key => $attribute) {
230
            $key = strtolower($key);
231
            if(!in_array($key, $keys)) {
232
                unset($this->attr[$key]);
233
            }
234
        }
235
236
        return $this;
237
    }
238
239
    /**
240
     * Generates the opening tag for this object.
241
     *
242
     * @return string
243
     */
244
    public function makeOpeningTag()
245
    {
246
        $return = '<'.$this->name;
247
248
        // add the attributes
249
        foreach ($this->attr as $key => $info) {
250
            $info = $this->getAttribute($key);
251
            $val  = $info['value'];
252
            if (is_null($val)) {
253
                $return .= ' '.$key;
254
            } elseif ($info['doubleQuote']) {
255
                $return .= ' '.$key.'="'.$val.'"';
256
            } else {
257
                $return .= ' '.$key.'=\''.$val.'\'';
258
            }
259
        }
260
261
        if ($this->selfClosing) {
262
            return $return.' />';
263
        } else {
264
            return $return.'>';
265
        }
266
    }
267
268
    /**
269
     * Generates the closing tag for this object.
270
     *
271
     * @return string
272
     */
273
    public function makeClosingTag()
274
    {
275
        if ($this->selfClosing) {
276
            return '';
277
        }
278
279
        return '</'.$this->name.'>';
280
    }
281
}
282