-
Notifications
You must be signed in to change notification settings - Fork 25
/
BaseTransformerConfiguration.php
97 lines (85 loc) · 3.16 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
<?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;
/**
* 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->$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->$key;
}
/**
* 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->$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);
}