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