|
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 null|Token */ |
|
11
|
|
|
private $parent; |
|
12
|
|
|
|
|
13
|
|
|
/** @var boolean */ |
|
14
|
|
|
private $throwOnError; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $type; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor |
|
21
|
|
|
*/ |
|
22
|
87 |
|
public function __construct($type, Token $parent = null, $throwOnError = false) |
|
23
|
|
|
{ |
|
24
|
87 |
|
if (!$this->isValidType($type)) { |
|
25
|
1 |
|
throw new \InvalidArgumentException('Invalid type: ' . $type); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
86 |
|
$this->depth = 0; |
|
29
|
86 |
|
if ($parent instanceof Token) { |
|
30
|
28 |
|
$this->depth = $parent->getDepth() + 1; |
|
31
|
28 |
|
} |
|
32
|
|
|
|
|
33
|
86 |
|
$this->parent = $parent; |
|
34
|
86 |
|
$this->throwOnError = (boolean) $throwOnError; |
|
35
|
86 |
|
$this->type = $type; |
|
36
|
86 |
|
} |
|
37
|
|
|
|
|
38
|
28 |
|
public function getDepth() |
|
39
|
|
|
{ |
|
40
|
28 |
|
return $this->depth; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
9 |
|
public function isClosingElementImplied($html) |
|
44
|
|
|
{ |
|
45
|
9 |
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
21 |
|
public function getParent() |
|
49
|
|
|
{ |
|
50
|
21 |
|
return $this->parent; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Getter for 'throwOnError'. |
|
55
|
|
|
* |
|
56
|
|
|
* @return boolean |
|
57
|
|
|
*/ |
|
58
|
24 |
|
protected function getThrowOnError() |
|
59
|
|
|
{ |
|
60
|
24 |
|
return $this->throwOnError; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Getter for 'type'. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getType() |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->type; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
public function isCDATA() |
|
74
|
|
|
{ |
|
75
|
1 |
|
return $this->type === Token::CDATA; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
public function isComment() |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->type === Token::COMMENT; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function isDocType() |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->type === Token::DOCTYPE; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function isElement() |
|
89
|
|
|
{ |
|
90
|
1 |
|
return $this->type === Token::ELEMENT; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public function isText() |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $this->type === Token::TEXT; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
87 |
|
protected function isValidType($type) |
|
99
|
|
|
{ |
|
100
|
|
|
return $type === Token::CDATA |
|
101
|
87 |
|
|| $type === Token::COMMENT |
|
102
|
80 |
|
|| $type === Token::DOCTYPE |
|
103
|
70 |
|
|| $type === Token::ELEMENT |
|
104
|
87 |
|
|| $type === Token::TEXT; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|