NumericNode.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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;
  11. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  12. /**
  13. * This node represents a numeric value in the config tree.
  14. *
  15. * @author David Jeanmonod <david.jeanmonod@gmail.com>
  16. */
  17. class NumericNode extends ScalarNode
  18. {
  19. protected $min;
  20. protected $max;
  21. public function __construct($name, NodeInterface $parent = null, $min = null, $max = null)
  22. {
  23. parent::__construct($name, $parent);
  24. $this->min = $min;
  25. $this->max = $max;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function finalizeValue($value)
  31. {
  32. $value = parent::finalizeValue($value);
  33. $errorMsg = null;
  34. if (isset($this->min) && $value < $this->min) {
  35. $errorMsg = sprintf('The value %s is too small for path "%s". Should be greater than or equal to %s', $value, $this->getPath(), $this->min);
  36. }
  37. if (isset($this->max) && $value > $this->max) {
  38. $errorMsg = sprintf('The value %s is too big for path "%s". Should be less than or equal to %s', $value, $this->getPath(), $this->max);
  39. }
  40. if (isset($errorMsg)) {
  41. $ex = new InvalidConfigurationException($errorMsg);
  42. $ex->setPath($this->getPath());
  43. throw $ex;
  44. }
  45. return $value;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function isValueEmpty($value)
  51. {
  52. // a numeric value cannot be empty
  53. return false;
  54. }
  55. }