|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace dlindberg\DOMDocumentFactory; |
|
6
|
|
|
|
|
7
|
|
|
use \HTMLPurifier; |
|
8
|
|
|
|
|
9
|
|
|
class DOMDocumentFactoryConfig |
|
10
|
|
|
{ |
|
11
|
|
|
public $version = '1.0'; |
|
12
|
|
|
public $encoding = 'UTF-8'; |
|
13
|
|
|
public $recover = true; |
|
14
|
|
|
public $preserveWhiteSpace = false; |
|
15
|
|
|
public $formatOutput = true; |
|
16
|
|
|
public $DOMOptions = LIBXML_NOERROR | LIBXML_NOWARNING; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \HTMLPurifier |
|
20
|
|
|
*/ |
|
21
|
|
|
private $inputPurifier; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \HTMLPurifier | null |
|
25
|
|
|
*/ |
|
26
|
|
|
private $outputPurifier; |
|
27
|
|
|
|
|
28
|
45 |
|
public function __construct( |
|
29
|
|
|
array $settings = [], |
|
30
|
|
|
\HTMLPurifier $inputPurifier = null, |
|
31
|
|
|
\HTMLPurifier $outputPurifier = null |
|
32
|
|
|
) { |
|
33
|
45 |
|
$this->inputPurifier = $inputPurifier ?? new HTMLPurifier(); |
|
34
|
45 |
|
$this->outputPurifier = $outputPurifier; |
|
35
|
45 |
|
\array_filter($settings, [$this, 'setOption'], ARRAY_FILTER_USE_BOTH); |
|
36
|
45 |
|
} |
|
37
|
|
|
|
|
38
|
9 |
|
public function setOption($value, $option): DOMDocumentFactoryConfig |
|
39
|
|
|
{ |
|
40
|
9 |
|
if (isset($this->$option)) { |
|
41
|
6 |
|
$this->$option = $value; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
9 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
3 |
|
public function setInputPurifier(\HTMLPurifier $purifier): DOMDocumentFactoryConfig |
|
48
|
|
|
{ |
|
49
|
3 |
|
$this->inputPurifier = $purifier; |
|
50
|
|
|
|
|
51
|
3 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
6 |
|
public function setOutputPurifier(?\HTMLPurifier $purifier): DOMDocumentFactoryConfig |
|
55
|
|
|
{ |
|
56
|
6 |
|
$this->outputPurifier = $purifier; |
|
57
|
|
|
|
|
58
|
6 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
33 |
|
public function loadString(string $blob): string |
|
62
|
|
|
{ |
|
63
|
33 |
|
return $this->getDocType() . $this->inputPurifier->purify($blob); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
18 |
|
public function outputString(string $blob): string |
|
67
|
|
|
{ |
|
68
|
18 |
|
return $this->outputPurifier ? $this->outputPurifier->purify($blob) : $blob; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
36 |
|
public function getDocType(): string |
|
72
|
|
|
{ |
|
73
|
36 |
|
return \trim((new \DOMDocument($this->version, $this->encoding))->saveXML()); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|