NumericNodeDefinition.php 1.6 KB

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