1: <?php
2:
3: namespace Symfony\Component\Config\Definition;
4:
5: use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6: use Symfony\Component\Config\Definition\ScalarNode;
7:
8: 9: 10: 11: 12:
13: class EnumNode extends ScalarNode
14: {
15: private $values;
16:
17: public function __construct($name, NodeInterface $parent = null, array $values = array())
18: {
19: $values = array_unique($values);
20: if (count($values) <= 1) {
21: throw new \InvalidArgumentException('$values must contain at least two distinct elements.');
22: }
23:
24: parent::__construct($name, $parent);
25: $this->values = $values;
26: }
27:
28: public function getValues()
29: {
30: return $this->values;
31: }
32:
33: protected function finalizeValue($value)
34: {
35: $value = parent::finalizeValue($value);
36:
37: if (!in_array($value, $this->values, true)) {
38: $ex = new InvalidConfigurationException(sprintf(
39: 'The value %s is not allowed for path "%s". Permissible values: %s',
40: json_encode($value),
41: $this->getPath(),
42: implode(', ', array_map('json_encode', $this->values))));
43: $ex->setPath($this->getPath());
44:
45: throw $ex;
46: }
47:
48: return $value;
49: }
50: }
51: