1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kevintweber\HtmlTokenizer\Tokens; |
4
|
|
|
|
5
|
|
|
abstract class AbstractToken implements Token |
6
|
|
|
{ |
7
|
|
|
/** @var int */ |
8
|
|
|
private $depth; |
9
|
|
|
|
10
|
|
|
/** @var int */ |
11
|
|
|
protected $line; |
12
|
|
|
|
13
|
|
|
/** @var null|Token */ |
14
|
|
|
private $parent; |
15
|
|
|
|
16
|
|
|
/** @var int */ |
17
|
|
|
protected $position; |
18
|
|
|
|
19
|
|
|
/** @var boolean */ |
20
|
|
|
private $throwOnError; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $type; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $value; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor |
30
|
|
|
*/ |
31
|
|
|
public function __construct(string $type, Token $parent = null, bool $throwOnError = false) |
32
|
|
|
{ |
33
|
|
|
if (!$this->isValidType($type)) { |
34
|
|
|
throw new \InvalidArgumentException('Invalid type: ' . $type); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->depth = 0; |
38
|
|
|
if ($parent instanceof Token) { |
39
|
|
|
$this->depth = $parent->getDepth() + 1; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->line = 0; |
43
|
|
|
$this->position = 0; |
44
|
|
|
$this->parent = $parent; |
45
|
|
|
$this->throwOnError = $throwOnError; |
46
|
|
|
$this->type = $type; |
47
|
|
|
$this->value = ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getDepth() : int |
51
|
|
|
{ |
52
|
|
|
return $this->depth; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Getter for 'line'. |
57
|
|
|
*/ |
58
|
|
|
public function getLine() : int |
59
|
|
|
{ |
60
|
|
|
return $this->line; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function isClosingElementImplied(string $html) : bool |
64
|
|
|
{ |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getParent() |
69
|
|
|
{ |
70
|
|
|
return $this->parent; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Getter for 'position'. |
75
|
|
|
*/ |
76
|
|
|
public function getPosition() : int |
77
|
|
|
{ |
78
|
|
|
return $this->position; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Getter for 'throwOnError'. |
83
|
|
|
* |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
|
|
protected function getThrowOnError() |
87
|
|
|
{ |
88
|
|
|
return $this->throwOnError; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Getter for 'type'. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getType() : string |
97
|
|
|
{ |
98
|
|
|
return $this->type; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Getter for "value" |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getValue() : string |
107
|
|
|
{ |
108
|
|
|
return $this->value; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function isCDATA() : bool |
112
|
|
|
{ |
113
|
|
|
return $this->type === Token::CDATA; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function isComment() : bool |
117
|
|
|
{ |
118
|
|
|
return $this->type === Token::COMMENT; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function isDocType() : bool |
122
|
|
|
{ |
123
|
|
|
return $this->type === Token::DOCTYPE; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function isElement() : bool |
127
|
|
|
{ |
128
|
|
|
return $this->type === Token::ELEMENT; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function isPhp() : bool |
132
|
|
|
{ |
133
|
|
|
return $this->type === Token::PHP; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function isText() : bool |
137
|
|
|
{ |
138
|
|
|
return $this->type === Token::TEXT; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function isValidType(string $type) : bool |
142
|
|
|
{ |
143
|
|
|
return $type === Token::CDATA |
144
|
|
|
|| $type === Token::COMMENT |
145
|
|
|
|| $type === Token::DOCTYPE |
146
|
|
|
|| $type === Token::ELEMENT |
147
|
|
|
|| $type === Token::PHP |
148
|
|
|
|| $type === Token::TEXT; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|