1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
6
|
|
|
use Groundskeeper\Tokens\Token as CleanableToken; |
7
|
|
|
use Kevintweber\HtmlTokenizer\HtmlTokenizer; |
8
|
|
|
use Kevintweber\HtmlTokenizer\Tokens\Element as BasicElement; |
9
|
|
|
use Kevintweber\HtmlTokenizer\Tokens\Token as BasicToken; |
10
|
|
|
|
11
|
|
|
class Tokenizer |
12
|
|
|
{ |
13
|
|
|
/** @var Configuration */ |
14
|
|
|
private $configuration; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor |
18
|
|
|
*/ |
19
|
21 |
|
public function __construct(Configuration $configuration) |
20
|
|
|
{ |
21
|
21 |
|
$this->configuration = $configuration; |
22
|
21 |
|
} |
23
|
|
|
|
24
|
21 |
|
public function tokenize($html) |
25
|
|
|
{ |
26
|
21 |
|
$tokenizer = new HtmlTokenizer( |
27
|
21 |
|
$this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_THROW |
28
|
21 |
|
); |
29
|
21 |
|
$basicTokenCollection = $tokenizer->parse((string) $html); |
30
|
|
|
|
31
|
21 |
|
$tokenContainer = new TokenContainer($this->configuration); |
32
|
21 |
|
foreach ($basicTokenCollection as $basicToken) { |
33
|
21 |
|
$tokenContainer->addChild($this->createToken($basicToken)); |
34
|
21 |
|
} |
35
|
|
|
|
36
|
21 |
|
return $tokenContainer; |
37
|
|
|
} |
38
|
|
|
|
39
|
21 |
|
private function createToken(BasicToken $basicToken, CleanableToken $parent = null) |
40
|
|
|
{ |
41
|
21 |
|
switch ($basicToken->getType()) { |
42
|
21 |
|
case 'cdata': |
43
|
2 |
|
return new CData( |
44
|
21 |
|
$this->configuration, |
45
|
2 |
|
$parent, |
46
|
2 |
|
$basicToken->getValue() |
|
|
|
|
47
|
2 |
|
); |
48
|
|
|
|
49
|
19 |
|
case 'comment': |
50
|
2 |
|
return new Comment( |
51
|
2 |
|
$this->configuration, |
52
|
2 |
|
$parent, |
53
|
2 |
|
$basicToken->getValue() |
|
|
|
|
54
|
2 |
|
); |
55
|
|
|
|
56
|
17 |
|
case 'doctype': |
57
|
2 |
|
return new DocType( |
58
|
2 |
|
$this->configuration, |
59
|
2 |
|
$parent, |
60
|
2 |
|
$basicToken->getValue() |
|
|
|
|
61
|
2 |
|
); |
62
|
|
|
|
63
|
15 |
|
case 'element': |
64
|
13 |
|
return $this->createElement($basicToken, $parent); |
|
|
|
|
65
|
|
|
|
66
|
6 |
|
case 'text': |
67
|
6 |
|
return new Text( |
68
|
6 |
|
$this->configuration, |
69
|
6 |
|
$parent, |
70
|
6 |
|
$basicToken->getValue() |
|
|
|
|
71
|
6 |
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new \RuntimeException( |
75
|
|
|
'Invalid token type: ' . $basicToken->getType() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
13 |
|
private function createElement(BasicElement $basicElement, CleanableToken $parent = null) |
80
|
|
|
{ |
81
|
|
|
$elementClassName = 'Groundskeeper\\Tokens\\Elements\\' . |
82
|
13 |
|
ucfirst(strtolower($basicElement->getName())); |
83
|
13 |
|
if (!class_exists($elementClassName)) { |
84
|
13 |
|
$elementClassName = 'Groundskeeper\\Tokens\\Elements\\Element'; |
85
|
13 |
|
} |
86
|
|
|
|
87
|
13 |
|
$cleanableElement = new $elementClassName( |
88
|
13 |
|
$this->configuration, |
89
|
13 |
|
$basicElement->getName(), |
90
|
13 |
|
$basicElement->getAttributes(), |
91
|
|
|
$parent |
92
|
13 |
|
); |
93
|
|
|
|
94
|
13 |
|
foreach ($basicElement->getChildren() as $basicChild) { |
95
|
11 |
|
$cleanableElement->addChild( |
96
|
11 |
|
$this->createToken($basicChild, $cleanableElement) |
97
|
11 |
|
); |
98
|
13 |
|
} |
99
|
|
|
|
100
|
13 |
|
return $cleanableElement; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: