NumericNode.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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(?string $name, NodeInterface $parent = null, $min = null, $max = null, string $pathSeparator = BaseNode::DEFAULT_PATH_SEPARATOR)
  22. {
  23. parent::__construct($name, $parent, $pathSeparator);
  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. }