1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kevintweber\HtmlTokenizer\Tokens; |
4
|
|
|
|
5
|
|
|
abstract class AbstractToken implements Token |
6
|
|
|
{ |
7
|
|
|
/** @var null|Token */ |
8
|
|
|
private $parent; |
9
|
|
|
|
10
|
|
|
/** @var boolean */ |
11
|
|
|
private $throwOnError; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $type; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Constructor |
18
|
|
|
*/ |
19
|
85 |
|
public function __construct($type, Token $parent = null, $throwOnError = false) |
20
|
|
|
{ |
21
|
85 |
|
if (!$this->isValidType($type)) { |
22
|
1 |
|
throw new \InvalidArgumentException('Invalid type: ' . $type); |
23
|
|
|
} |
24
|
|
|
|
25
|
84 |
|
$this->parent = $parent; |
26
|
84 |
|
$this->throwOnError = (boolean) $throwOnError; |
27
|
84 |
|
$this->type = $type; |
28
|
84 |
|
} |
29
|
|
|
|
30
|
6 |
|
public function isClosingElementImplied($html) |
31
|
|
|
{ |
32
|
6 |
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
23 |
|
public function getParent() |
36
|
|
|
{ |
37
|
23 |
|
return $this->parent; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Getter for 'throwOnError'. |
42
|
|
|
* |
43
|
|
|
* @return boolean |
44
|
|
|
*/ |
45
|
19 |
|
protected function getThrowOnError() |
46
|
|
|
{ |
47
|
19 |
|
return $this->throwOnError; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Getter for 'type'. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
1 |
|
public function getType() |
56
|
|
|
{ |
57
|
1 |
|
return $this->type; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function isCDATA() |
61
|
|
|
{ |
62
|
1 |
|
return $this->type === Token::CDATA; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function isComment() |
66
|
|
|
{ |
67
|
1 |
|
return $this->type === Token::COMMENT; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function isDocType() |
71
|
|
|
{ |
72
|
1 |
|
return $this->type === Token::DOCTYPE; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function isElement() |
76
|
|
|
{ |
77
|
1 |
|
return $this->type === Token::ELEMENT; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function isText() |
81
|
|
|
{ |
82
|
1 |
|
return $this->type === Token::TEXT; |
83
|
|
|
} |
84
|
|
|
|
85
|
85 |
|
protected function isValidType($type) |
86
|
|
|
{ |
87
|
|
|
return $type === Token::CDATA |
88
|
85 |
|
|| $type === Token::COMMENT |
89
|
78 |
|
|| $type === Token::DOCTYPE |
90
|
68 |
|
|| $type === Token::ELEMENT |
91
|
85 |
|
|| $type === Token::TEXT; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|