EnumNodeDefinition.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Config\Definition\Builder;
  11. use Symfony\Component\Config\Definition\EnumNode;
  12. use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition;
  13. /**
  14. * Enum Node Definition.
  15. *
  16. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  17. */
  18. class EnumNodeDefinition extends ScalarNodeDefinition
  19. {
  20. private $values;
  21. public function values(array $values)
  22. {
  23. $values = array_unique($values);
  24. if (count($values) <= 1) {
  25. throw new \InvalidArgumentException('->values() must be called with at least two distinct values.');
  26. }
  27. $this->values = $values;
  28. return $this;
  29. }
  30. /**
  31. * Instantiate a Node
  32. *
  33. * @return EnumNode The node
  34. *
  35. * @throws \RuntimeException
  36. */
  37. protected function instantiateNode()
  38. {
  39. if (null === $this->values) {
  40. throw new \RuntimeException('You must call ->values() on enum nodes.');
  41. }
  42. return new EnumNode($this->name, $this->parent, $this->values);
  43. }
  44. }