NumericNodeDefinition.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 contains common code of integer and float node definitions.
  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 $this
  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 $this
  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. /**
  55. * {@inheritdoc}
  56. *
  57. * @deprecated Deprecated since version 2.8, to be removed in 3.0.
  58. */
  59. public function cannotBeEmpty()
  60. {
  61. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
  62. return parent::cannotBeEmpty();
  63. }
  64. }