EnumNodeDefinition.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. * @return $this
  22. */
  23. public function values(array $values)
  24. {
  25. $values = array_unique($values);
  26. if (empty($values)) {
  27. throw new \InvalidArgumentException('->values() must be called with at least one value.');
  28. }
  29. $this->values = $values;
  30. return $this;
  31. }
  32. /**
  33. * Instantiate a Node.
  34. *
  35. * @return EnumNode The node
  36. *
  37. * @throws \RuntimeException
  38. */
  39. protected function instantiateNode()
  40. {
  41. if (null === $this->values) {
  42. throw new \RuntimeException('You must call ->values() on enum nodes.');
  43. }
  44. return new EnumNode($this->name, $this->parent, $this->values, $this->pathSeparator);
  45. }
  46. }