1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
6
|
|
|
use Kevintweber\HtmlTokenizer\HtmlTokenizer; |
7
|
|
|
use Kevintweber\HtmlTokenizer\Tokens\Element as BasicElement; |
8
|
|
|
use Kevintweber\HtmlTokenizer\Tokens\Token as BasicToken; |
9
|
|
|
|
10
|
|
|
class Tokenizer |
11
|
|
|
{ |
12
|
|
|
/** @var Configuration */ |
13
|
|
|
private $configuration; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Constructor |
17
|
|
|
*/ |
18
|
92 |
|
public function __construct(Configuration $configuration) |
19
|
|
|
{ |
20
|
92 |
|
$this->configuration = $configuration; |
21
|
92 |
|
} |
22
|
|
|
|
23
|
92 |
|
public function tokenize($html) |
24
|
|
|
{ |
25
|
92 |
|
$tokenizer = new HtmlTokenizer(false); |
26
|
92 |
|
$basicTokenCollection = $tokenizer->parse((string) $html); |
27
|
|
|
|
28
|
92 |
|
$tokenContainer = new TokenContainer($this->configuration); |
29
|
92 |
|
foreach ($basicTokenCollection as $basicToken) { |
30
|
92 |
|
$tokenContainer->appendChild($this->createToken($basicToken)); |
31
|
92 |
|
} |
32
|
|
|
|
33
|
92 |
|
return $tokenContainer; |
34
|
|
|
} |
35
|
|
|
|
36
|
92 |
|
private function createToken(BasicToken $basicToken) |
37
|
|
|
{ |
38
|
92 |
|
switch ($basicToken->getType()) { |
39
|
92 |
|
case 'cdata': |
40
|
3 |
|
return new CData( |
41
|
3 |
|
$this->configuration, |
42
|
3 |
|
$basicToken->getValue() |
|
|
|
|
43
|
3 |
|
); |
44
|
|
|
|
45
|
90 |
|
case 'comment': |
46
|
17 |
|
return new Comment( |
47
|
17 |
|
$this->configuration, |
48
|
17 |
|
$basicToken->getValue() |
|
|
|
|
49
|
17 |
|
); |
50
|
|
|
|
51
|
88 |
|
case 'doctype': |
52
|
3 |
|
return new DocType( |
53
|
3 |
|
$this->configuration, |
54
|
3 |
|
$basicToken->getValue() |
|
|
|
|
55
|
3 |
|
); |
56
|
|
|
|
57
|
86 |
|
case 'php': |
58
|
1 |
|
return new Php( |
59
|
1 |
|
$this->configuration, |
60
|
1 |
|
$basicToken->getValue() |
|
|
|
|
61
|
1 |
|
); |
62
|
|
|
|
63
|
86 |
|
case 'text': |
64
|
76 |
|
return new Text( |
65
|
76 |
|
$this->configuration, |
66
|
76 |
|
$basicToken->getValue() |
|
|
|
|
67
|
76 |
|
); |
68
|
84 |
|
} |
69
|
|
|
|
70
|
84 |
|
return $this->createElement($basicToken); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
84 |
|
private function createElement(BasicElement $basicElement) |
74
|
|
|
{ |
75
|
|
|
// Primary class name. |
76
|
|
|
$elementClassName = 'Groundskeeper\\Tokens\\Elements\\' . |
77
|
84 |
|
ucfirst(strtolower($basicElement->getName())); |
78
|
84 |
|
if (!class_exists($elementClassName)) { |
79
|
|
|
// Secondary class name. |
80
|
|
|
// For elements whose names conflict with PHP keywords: var |
81
|
|
|
$elementClassName = 'Groundskeeper\\Tokens\\Elements\\' . |
82
|
7 |
|
ucfirst(strtolower($basicElement->getName())) . 'Element'; |
83
|
7 |
|
if (!class_exists($elementClassName)) { |
84
|
7 |
|
$elementClassName = 'Groundskeeper\\Tokens\\Element'; |
85
|
7 |
|
} |
86
|
7 |
|
} |
87
|
|
|
|
88
|
84 |
|
$cleanableElement = new $elementClassName( |
89
|
84 |
|
$this->configuration, |
90
|
84 |
|
$basicElement->getName(), |
91
|
84 |
|
$basicElement->getAttributes() |
92
|
84 |
|
); |
93
|
|
|
|
94
|
84 |
|
foreach ($basicElement->getChildren() as $basicChild) { |
95
|
82 |
|
$cleanableElement->appendChild( |
96
|
82 |
|
$this->createToken($basicChild) |
97
|
82 |
|
); |
98
|
84 |
|
} |
99
|
|
|
|
100
|
84 |
|
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: