NumericNodeDefinition.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\Exception\InvalidDefinitionException;
  12. /**
  13. * Abstract class that contains common code of integer and float node definitions.
  14. *
  15. * @author David Jeanmonod <david.jeanmonod@gmail.com>
  16. */
  17. abstract class NumericNodeDefinition extends ScalarNodeDefinition
  18. {
  19. protected $min;
  20. protected $max;
  21. /**
  22. * Ensures that the value is smaller than the given reference.
  23. *
  24. * @param mixed $max
  25. *
  26. * @return $this
  27. *
  28. * @throws \InvalidArgumentException when the constraint is inconsistent
  29. */
  30. public function max($max)
  31. {
  32. if (isset($this->min) && $this->min > $max) {
  33. throw new \InvalidArgumentException(sprintf('You cannot define a max(%s) as you already have a min(%s).', $max, $this->min));
  34. }
  35. $this->max = $max;
  36. return $this;
  37. }
  38. /**
  39. * Ensures that the value is bigger than the given reference.
  40. *
  41. * @param mixed $min
  42. *
  43. * @return $this
  44. *
  45. * @throws \InvalidArgumentException when the constraint is inconsistent
  46. */
  47. public function min($min)
  48. {
  49. if (isset($this->max) && $this->max < $min) {
  50. throw new \InvalidArgumentException(sprintf('You cannot define a min(%s) as you already have a max(%s).', $min, $this->max));
  51. }
  52. $this->min = $min;
  53. return $this;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. *
  58. * @throws InvalidDefinitionException
  59. */
  60. public function cannotBeEmpty()
  61. {
  62. throw new InvalidDefinitionException('->cannotBeEmpty() is not applicable to NumericNodeDefinition.');
  63. }
  64. }