/
Configurable.php
145 lines (126 loc) · 4.24 KB
/
Configurable.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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form;
use Symfony\Component\Form\Exception\MissingOptionsException;
use Symfony\Component\Form\Exception\InvalidOptionsException;
/**
* A class configurable via options
*
* Options can be passed to the constructor of this class. After constructions,
* these options cannot be changed anymore. This way, options remain light
* weight. There is no need to monitor changes of options.
*
* If you want options that can change, you're recommended to implement plain
* properties with setters and getters.
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
*/
abstract class Configurable
{
/**
* The options and their values
* @var array
*/
private $options = array();
/**
* The names of the valid options
* @var array
*/
private $knownOptions = array();
/**
* The names of the required options
* @var array
*/
private $getRequiredOptions = array();
/**
* Reads, validates and stores the given options
*
* @param array $options
*/
public function __construct(array $options = array())
{
$this->options = array_merge($this->options, $options);
$this->configure();
// check option names
if ($diff = array_diff_key($this->options, $this->knownOptions)) {
throw new InvalidOptionsException(sprintf('%s does not support the following options: "%s".', get_class($this), implode('", "', array_keys($diff))), array_keys($diff));
}
// check required options
if ($diff = array_diff_key($this->getRequiredOptions, $this->options)) {
throw new MissingOptionsException(sprintf('%s requires the following options: \'%s\'.', get_class($this), implode('", "', array_keys($diff))), array_keys($diff));
}
}
/**
* Configures the valid options
*
* This method should call addOption() or addRequiredOption() for every
* accepted option.
*/
protected function configure()
{
}
/**
* Returns an option value.
*
* @param string $name The option name
*
* @return mixed The option value
*/
public function getOption($name)
{
return isset($this->options[$name]) ? $this->options[$name] : null;
}
/**
* Adds a new option value with a default value.
*
* @param string $name The option name
* @param mixed $value The default value
*/
protected function addOption($name, $value = null, array $allowedValues = array())
{
$this->knownOptions[$name] = true;
if (!array_key_exists($name, $this->options)) {
$this->options[$name] = $value;
}
if (count($allowedValues) > 0 && !in_array($this->options[$name], $allowedValues)) {
throw new InvalidOptionsException(sprintf('The option "%s" is expected to be one of "%s", but is "%s"', $name, implode('", "', $allowedValues), $this->options[$name]), array($name));
}
}
/**
* Adds a required option.
*
* @param string $name The option name
*/
protected function addRequiredOption($name, array $allowedValues = array())
{
$this->knownOptions[$name] = true;
$this->getRequiredOptions[$name] = true;
// only test if the option is set, otherwise an error will be thrown
// anyway
if (isset($this->options[$name]) && count($allowedValues) > 0 && !in_array($this->options[$name], $allowedValues)) {
throw new InvalidOptionsException(sprintf('The option "%s" is expected to be one of "%s", but is "%s"', $name, implode('", "', $allowedValues), $this->options[$name]), array($name));
}
}
/**
* Returns true if the option exists.
*
* @param string $name The option name
*
* @return Boolean true if the option is set, false otherwise
*/
public function hasOption($name)
{
return isset($this->options[$name]);
}
public function getOptions()
{
return $this->options;
}
}