EnumNodeDefinition.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. /**
  13. * Enum Node Definition.
  14. *
  15. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  16. */
  17. class EnumNodeDefinition extends ScalarNodeDefinition
  18. {
  19. private $values;
  20. /**
  21. * @param array $values
  22. *
  23. * @return $this
  24. */
  25. public function values(array $values)
  26. {
  27. $values = array_unique($values);
  28. if (empty($values)) {
  29. throw new \InvalidArgumentException('->values() must be called with at least one value.');
  30. }
  31. $this->values = $values;
  32. return $this;
  33. }
  34. /**
  35. * Instantiate a Node.
  36. *
  37. * @return EnumNode The node
  38. *
  39. * @throws \RuntimeException
  40. */
  41. protected function instantiateNode()
  42. {
  43. if (null === $this->values) {
  44. throw new \RuntimeException('You must call ->values() on enum nodes.');
  45. }
  46. return new EnumNode($this->name, $this->parent, $this->values);
  47. }
  48. }