ConstantNode.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\ExpressionLanguage\Node;
  11. use Symfony\Component\ExpressionLanguage\Compiler;
  12. /**
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. *
  15. * @internal
  16. */
  17. class ConstantNode extends Node
  18. {
  19. private $isIdentifier;
  20. public function __construct($value, $isIdentifier = false)
  21. {
  22. $this->isIdentifier = $isIdentifier;
  23. parent::__construct(
  24. [],
  25. ['value' => $value]
  26. );
  27. }
  28. public function compile(Compiler $compiler)
  29. {
  30. $compiler->repr($this->attributes['value']);
  31. }
  32. public function evaluate($functions, $values)
  33. {
  34. return $this->attributes['value'];
  35. }
  36. public function toArray()
  37. {
  38. $array = [];
  39. $value = $this->attributes['value'];
  40. if ($this->isIdentifier) {
  41. $array[] = $value;
  42. } elseif (true === $value) {
  43. $array[] = 'true';
  44. } elseif (false === $value) {
  45. $array[] = 'false';
  46. } elseif (null === $value) {
  47. $array[] = 'null';
  48. } elseif (is_numeric($value)) {
  49. $array[] = $value;
  50. } elseif (!\is_array($value)) {
  51. $array[] = $this->dumpString($value);
  52. } elseif ($this->isHash($value)) {
  53. foreach ($value as $k => $v) {
  54. $array[] = ', ';
  55. $array[] = new self($k);
  56. $array[] = ': ';
  57. $array[] = new self($v);
  58. }
  59. $array[0] = '{';
  60. $array[] = '}';
  61. } else {
  62. foreach ($value as $v) {
  63. $array[] = ', ';
  64. $array[] = new self($v);
  65. }
  66. $array[0] = '[';
  67. $array[] = ']';
  68. }
  69. return $array;
  70. }
  71. }