|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens; |
|
4
|
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractToken implements Token |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var int */ |
|
10
|
|
|
private $depth; |
|
11
|
|
|
|
|
12
|
|
|
/** @var null|boolean */ |
|
13
|
|
|
protected $isValid; |
|
14
|
|
|
|
|
15
|
|
|
/** @var null|Token */ |
|
16
|
|
|
private $parent; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
private $type; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Constructor |
|
23
|
|
|
*/ |
|
24
|
11 |
|
public function __construct($type, $parent = null) |
|
25
|
|
|
{ |
|
26
|
11 |
|
if (!$this->isValidType($type)) { |
|
27
|
|
|
throw new \InvalidArgumentException('Invalid type: ' . $type); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
11 |
|
$this->isValid = null; |
|
31
|
11 |
|
$this->setParent($parent); |
|
32
|
11 |
|
$this->type = $type; |
|
33
|
11 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getDepth() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->depth; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Getter for 'isValid'. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getIsValid() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->isValid; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Chainable setter for 'isValid'. |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function setIsValid($isValid) |
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->isValid = (boolean) $isValid; |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
11 |
|
* Getter for 'parent'. |
|
60
|
|
|
*/ |
|
61
|
11 |
|
public function getParent() |
|
62
|
11 |
|
{ |
|
63
|
|
|
return $this->parent; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
11 |
|
/** |
|
67
|
|
|
* Chainable setter for 'parent'. |
|
68
|
11 |
|
*/ |
|
69
|
|
|
public function setParent($parent = null) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($parent !== null && |
|
72
|
|
|
!$parent instanceof \Groundskeeper\Tokens\Token && |
|
73
|
|
|
!$parent instanceof \Kevintweber\HtmlTokenizer\Tokens\Token) { |
|
74
|
|
|
throw new \InvalidArgumentException('Invalid parent type.'); |
|
75
|
|
|
} |
|
76
|
10 |
|
|
|
77
|
|
|
$this->depth = 0; |
|
78
|
10 |
|
if ($parent !== null) { |
|
79
|
10 |
|
$this->depth = $parent->getDepth() + 1; |
|
80
|
|
|
} |
|
81
|
11 |
|
|
|
82
|
|
|
$this->parent = $parent; |
|
83
|
|
|
|
|
84
|
11 |
|
return $this; |
|
85
|
9 |
|
} |
|
86
|
7 |
|
|
|
87
|
11 |
|
public function getType() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->type; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function validate(Configuration $configuration) |
|
93
|
|
|
{ |
|
94
|
|
|
// If invalidated externally, then we don't allow it to be changed. |
|
95
|
|
|
if ($this->isValid === false) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->isValid = $configuration->isAllowedType($this->type); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function isValidType($type) |
|
103
|
|
|
{ |
|
104
|
|
|
return $type === Token::CDATA |
|
105
|
|
|
|| $type === Token::COMMENT |
|
106
|
|
|
|| $type === Token::DOCTYPE |
|
107
|
|
|
|| $type === Token::ELEMENT |
|
108
|
|
|
|| $type === Token::TEXT; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|