Node.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * Represents a node in the AST.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Node
  18. {
  19. public $nodes = array();
  20. public $attributes = array();
  21. /**
  22. * @param array $nodes An array of nodes
  23. * @param array $attributes An array of attributes
  24. */
  25. public function __construct(array $nodes = array(), array $attributes = array())
  26. {
  27. $this->nodes = $nodes;
  28. $this->attributes = $attributes;
  29. }
  30. public function __toString()
  31. {
  32. $attributes = array();
  33. foreach ($this->attributes as $name => $value) {
  34. $attributes[] = sprintf('%s: %s', $name, str_replace("\n", '', var_export($value, true)));
  35. }
  36. $repr = array(str_replace('Symfony\Component\ExpressionLanguage\Node\\', '', \get_class($this)).'('.implode(', ', $attributes));
  37. if (\count($this->nodes)) {
  38. foreach ($this->nodes as $node) {
  39. foreach (explode("\n", (string) $node) as $line) {
  40. $repr[] = ' '.$line;
  41. }
  42. }
  43. $repr[] = ')';
  44. } else {
  45. $repr[0] .= ')';
  46. }
  47. return implode("\n", $repr);
  48. }
  49. public function compile(Compiler $compiler)
  50. {
  51. foreach ($this->nodes as $node) {
  52. $node->compile($compiler);
  53. }
  54. }
  55. public function evaluate($functions, $values)
  56. {
  57. $results = array();
  58. foreach ($this->nodes as $node) {
  59. $results[] = $node->evaluate($functions, $values);
  60. }
  61. return $results;
  62. }
  63. }