EnumNodeDefinition.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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);
  45. }
  46. }