1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
6
|
|
|
use Groundskeeper\Exceptions\ValidationException; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A base class for all tokens. |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractToken implements Token |
13
|
|
|
{ |
14
|
|
|
/** @var Configuration */ |
15
|
|
|
protected $configuration; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
private $depth; |
19
|
|
|
|
20
|
|
|
/** @var null|Token */ |
21
|
|
|
private $parent; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $type; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
*/ |
29
|
65 |
|
public function __construct($type, Configuration $configuration) |
30
|
|
|
{ |
31
|
|
|
if ($type !== Token::CDATA |
32
|
65 |
|
&& $type !== Token::COMMENT |
33
|
65 |
|
&& $type !== Token::DOCTYPE |
34
|
65 |
|
&& $type !== Token::ELEMENT |
35
|
65 |
|
&& $type !== Token::TEXT) { |
36
|
1 |
|
throw new \InvalidArgumentException('Invalid type: ' . $type); |
37
|
|
|
} |
38
|
|
|
|
39
|
64 |
|
$this->configuration = $configuration; |
40
|
64 |
|
$this->depth = 0; |
41
|
64 |
|
$this->parent = null; |
42
|
64 |
|
$this->type = $type; |
43
|
64 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Required by the Token interface. |
47
|
|
|
*/ |
48
|
40 |
|
public function getDepth() |
49
|
|
|
{ |
50
|
40 |
|
return $this->depth; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Required by the Token interface. |
55
|
|
|
*/ |
56
|
20 |
|
public function getParent() |
57
|
|
|
{ |
58
|
20 |
|
return $this->parent; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Chainable setter for 'parent'. |
63
|
|
|
*/ |
64
|
39 |
|
public function setParent(Token $parent = null) |
65
|
|
|
{ |
66
|
39 |
|
$this->depth = 0; |
67
|
39 |
|
if ($parent instanceof Token) { |
68
|
39 |
|
$this->depth = $parent->getDepth() + 1; |
69
|
39 |
|
} |
70
|
|
|
|
71
|
39 |
|
$this->parent = $parent; |
72
|
|
|
|
73
|
39 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Required by the Token interface. |
78
|
|
|
*/ |
79
|
27 |
|
public function getType() |
80
|
|
|
{ |
81
|
27 |
|
return $this->type; |
82
|
|
|
} |
83
|
|
|
|
84
|
45 |
|
public static function cleanChildTokens(Configuration $configuration, array &$children, LoggerInterface $logger = null) |
85
|
|
|
{ |
86
|
45 |
|
if ($configuration->get('clean-strategy') == Configuration::CLEAN_STRATEGY_NONE) { |
87
|
33 |
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
44 |
|
foreach ($children as $key => $child) { |
91
|
40 |
|
if ($child instanceof Cleanable) { |
92
|
34 |
|
$isClean = $child->clean($logger); |
93
|
34 |
|
if (!$isClean && $configuration->get('clean-strategy') !== Configuration::CLEAN_STRATEGY_LENIENT) { |
94
|
2 |
|
if ($logger !== null) { |
95
|
2 |
|
$logger->debug('Unable to fix. Removing ' . $child); |
96
|
2 |
|
} |
97
|
|
|
|
98
|
2 |
|
unset($children[$key]); |
99
|
2 |
|
} |
100
|
34 |
|
} |
101
|
44 |
|
} |
102
|
|
|
|
103
|
44 |
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
|
public function __toString() |
107
|
|
|
{ |
108
|
5 |
|
return ucfirst($this->getType()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|