4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Definition;
14 use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17 * Node which only allows a finite set of values.
19 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
21 class EnumNode extends ScalarNode
25 public function __construct($name, NodeInterface $parent = null, array $values = array())
27 $values = array_unique($values);
29 throw new \InvalidArgumentException('$values must contain at least one element.');
32 parent::__construct($name, $parent);
33 $this->values = $values;
36 public function getValues()
41 protected function finalizeValue($value)
43 $value = parent::finalizeValue($value);
45 if (!\in_array($value, $this->values, true)) {
46 $ex = new InvalidConfigurationException(sprintf('The value %s is not allowed for path "%s". Permissible values: %s', json_encode($value), $this->getPath(), implode(', ', array_map('json_encode', $this->values))));
47 $ex->setPath($this->getPath());