1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Tokens\Tokenizer; |
6
|
|
|
use Psr\Log\LoggerAwareInterface; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
class Groundskeeper implements LoggerAwareInterface |
10
|
|
|
{ |
11
|
|
|
/** @var Configuration */ |
12
|
|
|
private $configuration; |
13
|
|
|
|
14
|
|
|
/** @var null|LoggerInterface */ |
15
|
|
|
private $logger; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Constructor |
19
|
|
|
* |
20
|
|
|
* @param array|Configuration $options |
21
|
|
|
*/ |
22
|
44 |
|
public function __construct($options = array()) |
23
|
|
|
{ |
24
|
44 |
|
$this->logger = null; |
25
|
44 |
|
if ($options instanceof Configuration) { |
26
|
2 |
|
$this->configuration = $options; |
27
|
|
|
|
28
|
1 |
|
return; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
44 |
|
if (!is_array($options)) { |
32
|
1 |
|
throw new \InvalidArgumentException('Invalid option type.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
43 |
|
$this->configuration = new Configuration($options); |
36
|
43 |
|
} |
37
|
|
|
|
38
|
42 |
|
public function clean($html) |
39
|
|
|
{ |
40
|
|
|
// Tokenize. |
41
|
42 |
|
$tokenizer = new Tokenizer($this->configuration); |
42
|
42 |
|
$tokenContainer = $tokenizer->tokenize($html); |
43
|
|
|
|
44
|
|
|
// Remove unwanted tokens. |
45
|
42 |
|
$tokenContainer->remove($this->logger); |
46
|
|
|
|
47
|
|
|
// Build output generator. |
48
|
|
|
$outputClassName = 'Groundskeeper\\Output\\' . |
49
|
42 |
|
ucfirst($this->configuration->get('output')); |
50
|
42 |
|
$outputGenerator = new $outputClassName(); |
51
|
|
|
|
52
|
|
|
// Clean |
53
|
42 |
|
$i = 0; |
54
|
|
|
do { |
55
|
42 |
|
$preCleaningOutput = $outputGenerator($tokenContainer); |
56
|
42 |
|
$tokenContainer->clean($this->logger); |
57
|
42 |
|
$cleanedOutput = $outputGenerator($tokenContainer); |
58
|
42 |
|
$i++; |
59
|
42 |
|
} while ($i < 5 && $preCleaningOutput != $cleanedOutput); |
60
|
|
|
|
61
|
42 |
|
return $cleanedOutput; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function getConfiguration() |
65
|
|
|
{ |
66
|
1 |
|
return $this->configuration; |
67
|
|
|
} |
68
|
|
|
|
69
|
43 |
|
public function setLogger(LoggerInterface $logger) |
70
|
|
|
{ |
71
|
43 |
|
$this->logger = $logger; |
72
|
43 |
|
} |
73
|
|
|
} |
74
|
|
|
|