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