ScalarNode.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\InvalidTypeException;
  12. /**
  13. * This node represents a scalar value in the config tree.
  14. *
  15. * The following values are considered scalars:
  16. * * booleans
  17. * * strings
  18. * * null
  19. * * integers
  20. * * floats
  21. *
  22. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23. */
  24. class ScalarNode extends VariableNode
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function validateType($value)
  30. {
  31. if (!is_scalar($value) && null !== $value) {
  32. $ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected "scalar", but got "%s".', $this->getPath(), get_debug_type($value)));
  33. if ($hint = $this->getInfo()) {
  34. $ex->addHint($hint);
  35. }
  36. $ex->setPath($this->getPath());
  37. throw $ex;
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. protected function isValueEmpty($value)
  44. {
  45. // assume environment variables are never empty (which in practice is likely to be true during runtime)
  46. // not doing so breaks many configs that are valid today
  47. if ($this->isHandlingPlaceholder()) {
  48. return false;
  49. }
  50. return null === $value || '' === $value;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function getValidPlaceholderTypes(): array
  56. {
  57. return ['bool', 'int', 'float', 'string'];
  58. }
  59. }