-
Notifications
You must be signed in to change notification settings - Fork 25
/
MinifyHtmlConfiguration.php
123 lines (112 loc) · 3.75 KB
/
MinifyHtmlConfiguration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace AmpProject\Optimizer\Configuration;
use AmpProject\Optimizer\Exception\InvalidConfigurationValue;
/**
* Configuration for the MinifyHtml transformer.
*
* @property bool $minify Whether minification is enabled.
* @property bool $minifyAmpScript Whether amp-script minification is enabled.
* @property bool $minifyJSON Whether JSON data minification is enabled.
* @property bool $collapseWhitespace Whether collapsing whitespace is enabled.
* @property bool $removeComments Whether comments should be removed.
* @property bool $canCollapseWhitespace Whether whitespace can be collapsed.
* @property bool $inBody Whether the node is in the body.
* @property string $commentIgnorePattern Regex pattern of comments to keep.
*
* @package ampproject/amp-toolbox
*/
final class MinifyHtmlConfiguration extends BaseTransformerConfiguration
{
/**
* Whether minification is enabled.
*
* @var string
*/
const MINIFY = 'minify';
/**
* Whether amp-script minification is enabled.
*
* @var string
*/
const MINIFY_AMP_SCRIPT = 'minifyAmpScript';
/**
* Whether JSON data minification is enabled.
*
* @var string
*/
const MINIFY_JSON = 'minifyJSON';
/**
* Whether collapsing whitespace is enabled.
*
* @var string
*/
const COLLAPSE_WHITESPACE = 'collapseWhitespace';
/**
* Whether comments should be removed.
*
* @var string
*/
const REMOVE_COMMENTS = 'removeComments';
/**
* Regular expression pattern of comments to keep.
*
* @var string
*/
const COMMENT_IGNORE_PATTERN = 'commentIgnorePattern';
/**
* Get the associative array of allowed keys and their respective default values.
*
* The array index is the key and the array value is the key's default value.
*
* @return array Associative array of allowed keys and their respective default values.
*/
protected function getAllowedKeys()
{
return [
self::MINIFY => true,
self::MINIFY_AMP_SCRIPT => false,
self::MINIFY_JSON => true,
self::COLLAPSE_WHITESPACE => false,
self::REMOVE_COMMENTS => true,
self::COMMENT_IGNORE_PATTERN => '',
];
}
/**
* Validate an individual configuration entry.
*
* @param string $key Key of the configuration entry to validate.
* @param mixed $value Value of the configuration entry to validate.
* @return mixed Validated value.
*/
protected function validate($key, $value)
{
switch ($key) {
case self::MINIFY:
case self::MINIFY_JSON:
case self::MINIFY_AMP_SCRIPT:
case self::COLLAPSE_WHITESPACE:
case self::REMOVE_COMMENTS:
if (! is_bool($value)) {
throw InvalidConfigurationValue::forInvalidSubValueType(
self::class,
$key,
'boolean',
is_object($value) ? get_class($value) : gettype($value)
);
}
break;
case self::COMMENT_IGNORE_PATTERN:
if (! is_string($value)) {
throw InvalidConfigurationValue::forInvalidSubValueType(
self::class,
self::COMMENT_IGNORE_PATTERN,
'string',
is_object($value) ? get_class($value) : gettype($value)
);
}
$value = trim($value);
break;
}
return $value;
}
}