-
Notifications
You must be signed in to change notification settings - Fork 25
/
BaseTransformerConfiguration.php
159 lines (140 loc) · 4.94 KB
/
BaseTransformerConfiguration.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace AmpProject\Optimizer\Configuration;
use AmpProject\Optimizer\Exception\InvalidConfigurationKey;
use AmpProject\Optimizer\Exception\UnknownConfigurationKey;
use AmpProject\Optimizer\TransformerConfiguration;
/**
* Configuration for the AmpRuntimeCss transformer.
*
* @property string $version Version string to use. Defaults to an empty string.
* @property boolean $canary Whether to use the canary version or not. Defaults to false.
*
* @package ampproject/amp-toolbox
*/
abstract class BaseTransformerConfiguration implements TransformerConfiguration
{
/**
* Associative array of allowed keys and their respective default values.
*
* @var array
*/
private $allowedKeys;
/**
* Associative array of configuration data.
*
* @var array
*/
private $configuration = [];
/**
* Instantiate an AmpRuntimeCssConfiguration object.
*
* @param array $configuration Optional. Associative array of configuration data. Defaults to an empty array.
*/
public function __construct($configuration = [])
{
$this->allowedKeys = $this->getAllowedKeys();
$configuration = array_merge($this->allowedKeys, $configuration);
foreach ($configuration as $key => $value) {
if (! array_key_exists($key, $this->allowedKeys)) {
throw InvalidConfigurationKey::fromTransformerKey(static::class, $key);
}
$this->configuration[$key] = $this->validate($key, $value);
}
}
/**
* Get the value for a given key.
*
* The key is assumed to exist and will throw an exception if it can't be retrieved.
* This means that all configuration entries should come with a default value.
*
* @param string $key Key of the configuration entry to retrieve.
* @return mixed Value stored under the given configuration key.
* @throws UnknownConfigurationKey If an unknown key was provided.
*/
public function get($key)
{
if (! array_key_exists($key, $this->allowedKeys)) {
throw UnknownConfigurationKey::fromTransformerKey(static::class, $key);
}
// At this point, the configuration should either have received this value or filled it with a default.
return $this->configuration[$key];
}
/**
* Magic getter to get value for a given key.
*
* Mostly for backward compatibility.
*
* @param string $name Name of the property to set.
*/
public function __get($name)
{
if (! array_key_exists($name, $this->allowedKeys)) {
// Mimic regular PHP behavior for missing notices.
trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
return null;
}
return $this->configuration[$name];
}
/**
* Magic setter for configurations.
*
* Mostly for backward compatibility.
*
* @param string $name Name of the property to set.
* @param mixed $value Value of the property.
*/
public function __set($name, $value)
{
if (! array_key_exists($name, $this->allowedKeys)) {
// Mimic regular PHP behavior for missing notices.
trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
return;
}
$this->configuration[$name] = $value;
}
/**
* Magic method to check whether a configuration exists.
*
* Mostly for backward compatibility.
*
* @param string $name Name of the property to set.
*/
public function __isset($name)
{
if (! array_key_exists($name, $this->allowedKeys)) {
// Mimic regular PHP behavior for missing notices.
trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
return false;
}
return isset($this->configuration[$name]);
}
/**
* Get an array of configuration entries for this transformer configuration.
*
* @return array Associative array of configuration entries.
*/
public function toArray()
{
$configArray = [];
foreach (array_keys($this->allowedKeys) as $key) {
$configArray[$key] = $this->configuration[$key];
}
return $configArray;
}
/**
* 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.
*/
abstract protected function getAllowedKeys();
/**
* 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.
*/
abstract protected function validate($key, $value);
}