Version 1
[yaffs-website] / vendor / symfony / config / Definition / EnumNode.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Config\Definition;
13
14 use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
15
16 /**
17  * Node which only allows a finite set of values.
18  *
19  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
20  */
21 class EnumNode extends ScalarNode
22 {
23     private $values;
24
25     public function __construct($name, NodeInterface $parent = null, array $values = array())
26     {
27         $values = array_unique($values);
28         if (empty($values)) {
29             throw new \InvalidArgumentException('$values must contain at least one element.');
30         }
31
32         parent::__construct($name, $parent);
33         $this->values = $values;
34     }
35
36     public function getValues()
37     {
38         return $this->values;
39     }
40
41     protected function finalizeValue($value)
42     {
43         $value = parent::finalizeValue($value);
44
45         if (!in_array($value, $this->values, true)) {
46             $ex = new InvalidConfigurationException(sprintf(
47                 'The value %s is not allowed for path "%s". Permissible values: %s',
48                 json_encode($value),
49                 $this->getPath(),
50                 implode(', ', array_map('json_encode', $this->values))));
51             $ex->setPath($this->getPath());
52
53             throw $ex;
54         }
55
56         return $value;
57     }
58 }