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