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