Skip to content

Commit df29314

Browse files
mr-chetandakur
andcommitted
Refactor encoding to use EncoderInterface
Replaces usage of the concrete Encoder class with EncoderInterface throughout the codebase for improved abstraction and flexibility. Updates type hints, exception handling, and relevant docblocks to reflect the new interface usage. https: //github.com/paquettg/pull/321 Co-Authored-By: dakur <[email protected]>
1 parent 4877615 commit df29314

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

src/PHPHtmlParser/DTO/Tag/AttributeDTO.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace PHPHtmlParser\DTO\Tag;
66

7-
use stringEncode\Exception;
8-
use StringEncoder\Encoder;
7+
use StringEncoder\Contracts\EncoderInterface;
8+
use StringEncoder\Exceptions\InvalidEncodingException;
99

1010
use function htmlspecialchars_decode;
1111
use function is_null;
@@ -54,9 +54,9 @@ public function htmlspecialcharsDecode(): void
5454
}
5555

5656
/**
57-
* @throws Exception
57+
* @throws InvalidEncodingException
5858
*/
59-
public function encodeValue(Encoder $encode)
59+
public function encodeValue(EncoderInterface $encode)
6060
{
6161
if ($this->value !== null) {
6262
$this->value = $encode->convert()->fromString($this->value)->toString();

src/PHPHtmlParser/Dom/Node/AbstractNode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use PHPHtmlParser\Exceptions\Tag\AttributeNotFoundException;
1313
use PHPHtmlParser\Finder;
1414
use PHPHtmlParser\Selector\Selector;
15-
use StringEncoder\Encoder;
15+
use StringEncoder\Contracts\EncoderInterface;
1616

1717
use function is_null;
1818
use function is_string;
@@ -61,7 +61,7 @@ abstract class AbstractNode
6161
/**
6262
* The encoding class used to encode strings.
6363
*
64-
* @var mixed
64+
* @var EncoderInterface|null
6565
*/
6666
protected $encode;
6767

@@ -230,7 +230,7 @@ public function delete()
230230
*
231231
* @return void
232232
*/
233-
public function propagateEncoding(Encoder $encode)
233+
public function propagateEncoding(EncoderInterface $encode)
234234
{
235235
$this->encode = $encode;
236236
$this->tag->setEncoding($encode);

src/PHPHtmlParser/Dom/Node/InnerNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PHPHtmlParser\Exceptions\ChildNotFoundException;
99
use PHPHtmlParser\Exceptions\CircularException;
1010
use PHPHtmlParser\Exceptions\LogicalException;
11-
use StringEncoder\Encoder;
11+
use StringEncoder\Contracts\EncoderInterface;
1212

1313
use function array_combine;
1414
use function array_keys;
@@ -46,7 +46,7 @@ abstract class InnerNode extends ArrayNode
4646
* Sets the encoding class to this node and propagates it
4747
* to all its children.
4848
*/
49-
public function propagateEncoding(Encoder $encode): void
49+
public function propagateEncoding(EncoderInterface $encode): void
5050
{
5151
$this->encode = $encode;
5252
$this->tag->setEncoding($encode);

src/PHPHtmlParser/Dom/Parser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPHtmlParser\Exceptions\StrictException;
1919
use PHPHtmlParser\Options;
2020
use StringEncoder\Encoder;
21+
use StringEncoder\Contracts\EncoderInterface;
2122

2223
use function in_array;
2324
use function is_null;
@@ -251,7 +252,7 @@ private function parseTag(Options $options, Content $content, int $size): TagDTO
251252
/**
252253
* @throws ChildNotFoundException
253254
*/
254-
private function detectHTML5Charset(Encoder $encode, AbstractNode $root): bool
255+
private function detectHTML5Charset(EncoderInterface $encode, AbstractNode $root): bool
255256
{
256257
/** @var AbstractNode|null $meta */
257258
$meta = $root->find('meta[charset]', 0);

src/PHPHtmlParser/Dom/Tag.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
use PHPHtmlParser\DTO\Tag\AttributeDTO;
88
use PHPHtmlParser\Exceptions\Tag\AttributeNotFoundException;
9-
use StringEncoder\Encoder;
9+
use StringEncoder\Contracts\EncoderInterface;
10+
use StringEncoder\Exceptions\InvalidEncodingException;
1011
use TypeError;
1112

1213
use function array_keys;
@@ -58,7 +59,7 @@ class Tag
5859
/**
5960
* The encoding class to... encode the tags.
6061
*
61-
* @var Encoder|null
62+
* @var EncoderInterface|null
6263
*/
6364
protected $encode;
6465

@@ -152,7 +153,7 @@ public function isSelfClosing(): bool
152153
/**
153154
* Sets the encoding type to be used.
154155
*/
155-
public function setEncoding(Encoder $encode): void
156+
public function setEncoding(EncoderInterface $encode): void
156157
{
157158
$this->encode = $encode;
158159
}
@@ -282,7 +283,7 @@ public function setAttributes(array $attr)
282283
*
283284
* @return AttributeDTO[]
284285
*
285-
* @throws \stringEncode\Exception
286+
* @throws InvalidEncodingException
286287
*/
287288
public function getAttributes(): array
288289
{
@@ -303,7 +304,7 @@ public function getAttributes(): array
303304
* Returns an attribute by the key.
304305
*
305306
* @throws AttributeNotFoundException
306-
* @throws \stringEncode\Exception
307+
* @throws InvalidEncodingException
307308
*/
308309
public function getAttribute(string $key): AttributeDTO
309310
{
@@ -358,6 +359,7 @@ public function makeOpeningTag()
358359
}
359360

360361
$val = $attributeDTO->getValue();
362+
361363
if (is_null($val)) {
362364
$return .= ' ' . $key;
363365
} elseif ($attributeDTO->isDoubleQuote()) {

0 commit comments

Comments
 (0)