Skip to content

Commit b84ea02

Browse files
committed
Added the attribute type dto
1 parent 1353671 commit b84ea02

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+167
-114
lines changed

.gitattributes

100644100755
File mode changed.

.gitignore

100644100755
File mode changed.

.phan/config.php

100644100755
File mode changed.

.scrutinizer.yml

100644100755
File mode changed.

.travis.yml

100644100755
File mode changed.

CHANGELOG.md

100644100755
+3

CONTRIBUTING.md

100644100755
File mode changed.

LICENSE.md

100644100755
File mode changed.

README.md

100644100755
File mode changed.

composer.json

100644100755
File mode changed.

infection.json.dist

100644100755
File mode changed.

phpunit.php

100644100755
File mode changed.

phpunit.xml

100644100755
File mode changed.

src/PHPHtmlParser/Content.php

100644100755
File mode changed.

src/PHPHtmlParser/Curl.php

100644100755
File mode changed.

src/PHPHtmlParser/CurlInterface.php

100644100755
File mode changed.
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPHtmlParser\DTO\Tag;
6+
7+
use stringEncode\Encode;
8+
use stringEncode\Exception;
9+
10+
class AttributeDTO
11+
{
12+
/**
13+
* @var ?string
14+
*/
15+
protected $value;
16+
17+
/**
18+
* @var bool
19+
*/
20+
protected $doubleQuote = true;
21+
22+
public function __construct(array $values)
23+
{
24+
$this->value = $values['value'];
25+
$this->doubleQuote = $values['doubleQuote'];
26+
}
27+
28+
/**
29+
* @return string
30+
*/
31+
public function getValue(): ?string
32+
{
33+
return $this->value;
34+
}
35+
36+
/**
37+
* @return bool
38+
*/
39+
public function isDoubleQuote(): bool
40+
{
41+
return $this->doubleQuote;
42+
}
43+
44+
public function htmlspecialcharsDecode(): void
45+
{
46+
$this->value = htmlspecialchars_decode($this->value);
47+
}
48+
49+
/**
50+
* @param Encode $encode
51+
* @throws Exception
52+
*/
53+
public function encodeValue(Encode $encode)
54+
{
55+
$this->value = $encode->convert($this->value);
56+
}
57+
}

src/PHPHtmlParser/Dom.php

100644100755
+4-14
Original file line numberDiff line numberDiff line change
@@ -746,38 +746,31 @@ protected function parseTag(): array
746746

747747
$this->content->skipByToken('blank');
748748
if ($this->content->char() == '=') {
749-
$attr = [];
750749
$this->content->fastForward(1)
751750
->skipByToken('blank');
752751
switch ($this->content->char()) {
753752
case '"':
754-
$attr['doubleQuote'] = true;
755753
$this->content->fastForward(1);
756754
$string = $this->content->copyUntil('"', true);
757755
do {
758756
$moreString = $this->content->copyUntilUnless('"', '=>');
759757
$string .= $moreString;
760758
} while ( ! empty($moreString));
761-
$attr['value'] = $string;
762759
$this->content->fastForward(1);
763-
$node->getTag()->$name = $attr;
760+
$node->getTag()->setAttribute($name, $string);
764761
break;
765762
case "'":
766-
$attr['doubleQuote'] = false;
767763
$this->content->fastForward(1);
768764
$string = $this->content->copyUntil("'", true);
769765
do {
770766
$moreString = $this->content->copyUntilUnless("'", '=>');
771767
$string .= $moreString;
772768
} while ( ! empty($moreString));
773-
$attr['value'] = $string;
774769
$this->content->fastForward(1);
775-
$node->getTag()->$name = $attr;
770+
$node->getTag()->setAttribute($name, $string, false);
776771
break;
777772
default:
778-
$attr['doubleQuote'] = true;
779-
$attr['value'] = $this->content->copyByToken('attr', true);
780-
$node->getTag()->$name = $attr;
773+
$node->getTag()->setAttribute($name, $this->content->copyByToken('attr', true));
781774
break;
782775
}
783776
} else {
@@ -787,10 +780,7 @@ protected function parseTag(): array
787780
$character = $this->content->getPosition();
788781
throw new StrictException("Tag '$tag' has an attribute '$name' with out a value! (character #$character)");
789782
}
790-
$node->getTag()->$name = [
791-
'value' => null,
792-
'doubleQuote' => true,
793-
];
783+
$node->getTag()->setAttribute($name, null);
794784
if ($this->content->char() != '>') {
795785
$this->content->rewind(1);
796786
}

src/PHPHtmlParser/Dom/AbstractNode.php

100644100755
+15-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use PHPHtmlParser\Exceptions\CircularException;
55
use PHPHtmlParser\Exceptions\ParentNotFoundException;
66
use PHPHtmlParser\Exceptions\ChildNotFoundException;
7+
use PHPHtmlParser\Exceptions\Tag\AttributeNotFoundException;
78
use PHPHtmlParser\Selector\Selector;
89
use PHPHtmlParser\Selector\Parser as SelectorParser;
910
use stringEncode\Encode;
@@ -337,8 +338,8 @@ public function getTag(): Tag
337338
public function getAttributes(): array
338339
{
339340
$attributes = $this->tag->getAttributes();
340-
foreach ($attributes as $name => $info) {
341-
$attributes[$name] = $info['value'];
341+
foreach ($attributes as $name => $attributeDTO) {
342+
$attributes[$name] = $attributeDTO->getValue();
342343
}
343344

344345
return $attributes;
@@ -353,10 +354,14 @@ public function getAttributes(): array
353354
*/
354355
public function getAttribute(string $key): ?string
355356
{
356-
$attribute = $this->tag->getAttribute($key);
357-
$attributeValue = $attribute['value'];
358-
359-
return $attributeValue;
357+
try {
358+
$attributeDTO = $this->tag->getAttribute($key);
359+
} catch (AttributeNotFoundException $e) {
360+
// no attribute with this key exists, returning null.
361+
unset($e);
362+
return null;
363+
}
364+
return $attributeDTO->getValue();
360365
}
361366

362367
/**
@@ -376,13 +381,14 @@ public function hasAttribute(string $key): bool
376381
* on the tag of this node.
377382
*
378383
* @param string $key
379-
* @param string|array $value
384+
* @param string|null $value
385+
* @param bool $doubleQuote
380386
* @return AbstractNode
381387
* @chainable
382388
*/
383-
public function setAttribute(string $key, $value): AbstractNode
389+
public function setAttribute(string $key, ?string $value, bool $doubleQuote = true): AbstractNode
384390
{
385-
$this->tag->setAttribute($key, $value);
391+
$this->tag->setAttribute($key, $value, $doubleQuote);
386392

387393
//clear any cache
388394
$this->clear();

src/PHPHtmlParser/Dom/ArrayNode.php

100644100755
File mode changed.

src/PHPHtmlParser/Dom/Collection.php

100644100755
File mode changed.

src/PHPHtmlParser/Dom/HtmlNode.php

100644100755
File mode changed.

src/PHPHtmlParser/Dom/InnerNode.php

100644100755
File mode changed.

src/PHPHtmlParser/Dom/LeafNode.php

100644100755
File mode changed.

src/PHPHtmlParser/Dom/Tag.php

100644100755
+48-53
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php declare(strict_types=1);
22
namespace PHPHtmlParser\Dom;
33

4+
use PHPHtmlParser\DTO\Tag\AttributeDTO;
5+
use PHPHtmlParser\Exceptions\Tag\AttributeNotFoundException;
46
use stringEncode\Encode;
57

68
/**
@@ -21,7 +23,7 @@ class Tag
2123
/**
2224
* The attributes of the tag.
2325
*
24-
* @var array
26+
* @var AttributeDTO[]
2527
*/
2628
protected $attr = [];
2729

@@ -47,7 +49,7 @@ class Tag
4749
/**
4850
* The encoding class to... encode the tags
4951
*
50-
* @var mixed
52+
* @var Encode|null
5153
*/
5254
protected $encode = null;
5355

@@ -66,28 +68,6 @@ public function __construct(string $name)
6668
$this->name = $name;
6769
}
6870

69-
/**
70-
* Magic method to get any of the attributes.
71-
*
72-
* @param string $key
73-
* @return mixed
74-
*/
75-
public function __get($key)
76-
{
77-
return $this->getAttribute($key);
78-
}
79-
80-
/**
81-
* Magic method to set any attribute.
82-
*
83-
* @param string $key
84-
* @param mixed $value
85-
*/
86-
public function __set($key, $value)
87-
{
88-
$this->setAttribute($key, $value);
89-
}
90-
9171
/**
9272
* Returns the name of this tag.
9373
*
@@ -173,23 +153,21 @@ public function noise(string $noise): Tag
173153
* Set an attribute for this tag.
174154
*
175155
* @param string $key
176-
* @param string|array $value
156+
* @param string $attributeValue
157+
* @param bool $doubleQuote
177158
* @return Tag
178159
* @chainable
179160
*/
180-
public function setAttribute(string $key, $value): Tag
161+
public function setAttribute(string $key, ?string $attributeValue, bool $doubleQuote = true): Tag
181162
{
182-
$key = strtolower($key);
183-
if ( ! is_array($value)) {
184-
$value = [
185-
'value' => $value,
186-
'doubleQuote' => true,
187-
];
188-
}
163+
$attributeDTO = new AttributeDTO([
164+
'value' => $attributeValue,
165+
'doubleQuote' => $doubleQuote,
166+
]);
189167
if ($this->HtmlSpecialCharsDecode) {
190-
$value['value'] = htmlspecialchars_decode($value['value']);
168+
$attributeDTO->htmlspecialcharsDecode();
191169
}
192-
$this->attr[$key] = $value;
170+
$this->attr[strtolower($key)] = $attributeDTO;
193171

194172
return $this;
195173
}
@@ -220,9 +198,10 @@ public function setStyleAttributeValue($attr_key, $attr_value): void
220198
*/
221199
public function getStyleAttributeArray(): array
222200
{
223-
$value = $this->getAttribute('style')['value'];
224-
225-
if ($value === null) {
201+
try {
202+
$value = $this->getAttribute('style')->getValue();
203+
} catch (AttributeNotFoundException $e) {
204+
unset($e);
226205
return [];
227206
}
228207

@@ -268,8 +247,12 @@ public function removeAllAttributes()
268247
*/
269248
public function setAttributes(array $attr)
270249
{
271-
foreach ($attr as $key => $value) {
272-
$this->setAttribute($key, $value);
250+
foreach ($attr as $key => $info) {
251+
if (is_array($info)) {
252+
$this->setAttribute($key, $info['value'], $info['doubleQuote']);
253+
} else {
254+
$this->setAttribute($key, $info);
255+
}
273256
}
274257

275258
return $this;
@@ -278,13 +261,18 @@ public function setAttributes(array $attr)
278261
/**
279262
* Returns all attributes of this tag.
280263
*
281-
* @return array
264+
* @return AttributeDTO[]
282265
*/
283-
public function getAttributes()
266+
public function getAttributes(): array
284267
{
285268
$return = [];
286269
foreach (array_keys($this->attr) as $attr) {
287-
$return[$attr] = $this->getAttribute($attr);
270+
try {
271+
$return[$attr] = $this->getAttribute($attr);
272+
} catch (AttributeNotFoundException $e) {
273+
// attribute that was in the array was not found in the array....
274+
unset($e);
275+
}
288276
}
289277

290278
return $return;
@@ -294,21 +282,23 @@ public function getAttributes()
294282
* Returns an attribute by the key
295283
*
296284
* @param string $key
297-
* @return array
285+
* @return AttributeDTO
286+
* @throws AttributeNotFoundException
287+
* @throws \stringEncode\Exception
298288
*/
299-
public function getAttribute(string $key):array
289+
public function getAttribute(string $key):AttributeDTO
300290
{
301291
$key = strtolower($key);
302292
if ( ! isset($this->attr[$key])) {
303-
return ['value' => null, 'doubleQuote' => true];
293+
throw new AttributeNotFoundException('Attribute with key "'.$key.'" not found.');
304294
}
305-
$value = $this->attr[$key]['value'];
306-
if (is_string($value) && ! is_null($this->encode)) {
295+
$attributeDTO = $this->attr[$key];
296+
if (! is_null($this->encode)) {
307297
// convert charset
308-
$this->attr[$key]['value'] = $this->encode->convert($value);
298+
$attributeDTO->encodeValue($this->encode);
309299
}
310300

311-
return $this->attr[$key];
301+
return $attributeDTO;
312302
}
313303

314304
/**
@@ -333,11 +323,16 @@ public function makeOpeningTag()
333323

334324
// add the attributes
335325
foreach (array_keys($this->attr) as $key) {
336-
$info = $this->getAttribute($key);
337-
$val = $info['value'];
326+
try {
327+
$attributeDTO = $this->getAttribute($key);
328+
} catch (AttributeNotFoundException $e) {
329+
// attribute that was in the array not found in the array... let's continue.
330+
continue;
331+
}
332+
$val = $attributeDTO->getValue();
338333
if (is_null($val)) {
339334
$return .= ' '.$key;
340-
} elseif ($info['doubleQuote']) {
335+
} elseif ($attributeDTO->isDoubleQuote()) {
341336
$return .= ' '.$key.'="'.$val.'"';
342337
} else {
343338
$return .= ' '.$key.'=\''.$val.'\'';

src/PHPHtmlParser/Dom/TextNode.php

100644100755
File mode changed.

src/PHPHtmlParser/Exceptions/ChildNotFoundException.php

100644100755
File mode changed.

src/PHPHtmlParser/Exceptions/CircularException.php

100644100755
File mode changed.

src/PHPHtmlParser/Exceptions/CurlException.php

100644100755
File mode changed.

src/PHPHtmlParser/Exceptions/EmptyCollectionException.php

100644100755
File mode changed.

src/PHPHtmlParser/Exceptions/LogicalException.php

100644100755
File mode changed.

src/PHPHtmlParser/Exceptions/NotLoadedException.php

100644100755
File mode changed.

src/PHPHtmlParser/Exceptions/ParentNotFoundException.php

100644100755
File mode changed.

src/PHPHtmlParser/Exceptions/StrictException.php

100644100755
File mode changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPHtmlParser\Exceptions\Tag;
6+
7+
/**
8+
* Class AttributeNotFoundException
9+
* @package PHPHtmlParser\Exceptions\Tag
10+
*/
11+
class AttributeNotFoundException extends \Exception
12+
{
13+
}

src/PHPHtmlParser/Exceptions/UnknownChildTypeException.php

100644100755
File mode changed.

src/PHPHtmlParser/Finder.php

100644100755
File mode changed.

src/PHPHtmlParser/Options.php

100644100755
File mode changed.

src/PHPHtmlParser/Selector/Parser.php

100644100755
File mode changed.

src/PHPHtmlParser/Selector/ParserInterface.php

100644100755
File mode changed.

src/PHPHtmlParser/Selector/Selector.php

100644100755
File mode changed.

src/PHPHtmlParser/StaticDom.php

100644100755
File mode changed.

tests/CollectionTest.php

100644100755
File mode changed.

tests/ContentTest.php

100644100755
File mode changed.

tests/DomTest.php

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ public function testCaseInSensitivity()
480480
$dom->loadStr($str);
481481

482482
$FooBar = $dom->find('FooBar');
483-
$this->assertEquals('asdf', $FooBar->attribute);
483+
$this->assertEquals('asdf', $FooBar->getAttribute('attribute'));
484484
}
485485

486486
public function testCaseSensitivity()

tests/Node/ChildrenTest.php

100644100755
File mode changed.

0 commit comments

Comments
 (0)