1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
6
|
|
|
|
7
|
|
|
abstract class AbstractToken implements Token |
8
|
|
|
{ |
9
|
|
|
/** @var Configuration */ |
10
|
|
|
protected $configuration; |
11
|
|
|
|
12
|
|
|
/** @var int */ |
13
|
|
|
private $depth; |
14
|
|
|
|
15
|
|
|
/** @var null|Token */ |
16
|
|
|
private $parent; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $type; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
*/ |
24
|
39 |
|
public function __construct($type, Configuration $configuration, Token $parent = null) |
25
|
|
|
{ |
26
|
39 |
|
if (!$this->isValidType($type)) { |
27
|
1 |
|
throw new \InvalidArgumentException('Invalid type: ' . $type); |
28
|
|
|
} |
29
|
|
|
|
30
|
38 |
|
$this->configuration = $configuration; |
31
|
38 |
|
$this->setParent($parent); |
32
|
38 |
|
$this->type = $type; |
33
|
38 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Required by the Token interface. |
37
|
|
|
*/ |
38
|
13 |
|
public function getDepth() |
39
|
|
|
{ |
40
|
13 |
|
return $this->depth; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Required by the Token interface. |
45
|
|
|
*/ |
46
|
1 |
|
public function getParent() |
47
|
|
|
{ |
48
|
1 |
|
return $this->parent; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Chainable setter for 'parent'. |
53
|
|
|
*/ |
54
|
38 |
|
public function setParent(Token $parent = null) |
55
|
|
|
{ |
56
|
38 |
|
$this->depth = 0; |
57
|
38 |
|
if ($parent instanceof Token) { |
58
|
12 |
|
$this->depth = $parent->getDepth() + 1; |
59
|
12 |
|
} |
60
|
|
|
|
61
|
38 |
|
$this->parent = $parent; |
62
|
|
|
|
63
|
38 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Required by the Token interface. |
68
|
|
|
*/ |
69
|
1 |
|
public function getType() |
70
|
|
|
{ |
71
|
1 |
|
return $this->type; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Required by the Token interface. |
76
|
|
|
*/ |
77
|
29 |
|
public function toHtml($prefix, $suffix) |
78
|
|
|
{ |
79
|
29 |
|
if (!$this->configuration->isAllowedType($this->type)) { |
80
|
4 |
|
return ''; |
81
|
|
|
} |
82
|
|
|
|
83
|
25 |
|
return $this->buildHtml($prefix, $suffix); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
abstract protected function buildHtml($prefix, $suffix); |
87
|
|
|
|
88
|
39 |
|
protected function isValidType($type) |
89
|
|
|
{ |
90
|
|
|
return $type === Token::CDATA |
91
|
39 |
|
|| $type === Token::COMMENT |
92
|
35 |
|
|| $type === Token::DOCTYPE |
93
|
30 |
|
|| $type === Token::ELEMENT |
94
|
39 |
|
|| $type === Token::TEXT; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|