FunctionNode.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\ExpressionLanguage\Node;
  11. use Symfony\Component\ExpressionLanguage\Compiler;
  12. /**
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. *
  15. * @internal
  16. */
  17. class FunctionNode extends Node
  18. {
  19. public function __construct(string $name, Node $arguments)
  20. {
  21. parent::__construct(
  22. ['arguments' => $arguments],
  23. ['name' => $name]
  24. );
  25. }
  26. public function compile(Compiler $compiler)
  27. {
  28. $arguments = [];
  29. foreach ($this->nodes['arguments']->nodes as $node) {
  30. $arguments[] = $compiler->subcompile($node);
  31. }
  32. $function = $compiler->getFunction($this->attributes['name']);
  33. $compiler->raw($function['compiler'](...$arguments));
  34. }
  35. public function evaluate(array $functions, array $values)
  36. {
  37. $arguments = [$values];
  38. foreach ($this->nodes['arguments']->nodes as $node) {
  39. $arguments[] = $node->evaluate($functions, $values);
  40. }
  41. return $functions[$this->attributes['name']]['evaluator'](...$arguments);
  42. }
  43. public function toArray()
  44. {
  45. $array = [];
  46. $array[] = $this->attributes['name'];
  47. foreach ($this->nodes['arguments']->nodes as $node) {
  48. $array[] = ', ';
  49. $array[] = $node;
  50. }
  51. $array[1] = '(';
  52. $array[] = ')';
  53. return $array;
  54. }
  55. }