aboutsummaryrefslogtreecommitdiff
path: root/lib/exTpl/FunctionExpression.php
blob: 48d4413e6e71e1eea4498d31ff121ab35ab16806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php

namespace exTpl;

/**
 * FunctionExpression represents a function call.
 */
class FunctionExpression implements Expression
{
    protected Expression $name;
    protected array $arguments;

    /**
     * Initializes a new Expression instance.
     *
     * @param Expression $name function name
     * @param array $arguments function arguments
     */
    public function __construct(Expression $name, array $arguments)
    {
        $this->name      = $name;
        $this->arguments = $arguments;
    }

    /**
     * Returns the value of this expression.
     *
     * @param Context $context symbol table
     */
    public function value(Context $context): mixed
    {
        $callable  = $this->name->value($context);
        $arguments = [];

        foreach ($this->arguments as $expr) {
            $arguments[] = $expr->value($context);
        }

        if (is_callable($callable)) {
            return $callable(...$arguments);
        }

        return null;
    }
}