Passed
Push — next ( ee2197...d54041 )
by Bas
02:37
created

src/Expressions/FunctionExpression.php (3 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\FluentAQL\Expressions;
6
7
use LaravelFreelancerNL\FluentAQL\Traits\NormalizesFunctions;
8
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
9
10
/**
11
 * AQL literal expression.
12
 */
13
class FunctionExpression extends Expression implements ExpressionInterface
14
{
15
    use NormalizesFunctions;
16
17
    protected string $functionName;
18
19
    /**
20
     * @var array<array-key, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
21
     */
22
    protected array $parameters = [];
23
24
    /**
25
     * FunctionExpression constructor.
26
     *
27
     * @param string $functionName
28
     * @param array<array-key, mixed>|object|scalar|null $parameters
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed>|object|scalar|null at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>|object|scalar|null.
Loading history...
29
     */
30 45
    public function __construct(
31
        string $functionName,
32
        object|array|string|int|float|bool|null $parameters = []
33
    ) {
34 45
        $this->functionName = $functionName;
35
36 45
        if (! is_array($parameters)) {
0 ignored issues
show
The condition is_array($parameters) is always true.
Loading history...
37 7
            $parameters = [$parameters];
38
        }
39
40 45
        $this->parameters = $parameters;
41 45
    }
42
43 45
    public function compile(QueryBuilder $queryBuilder): string
44
    {
45 45
        if (! empty($this->parameters)) {
46 44
            $normalizeFunction = $this->getNormalizeFunctionName();
47 44
            $this->$normalizeFunction($queryBuilder);
48
        }
49
50 45
        $output = strtoupper($this->functionName) . '(';
51 45
        $output .= implode(', ', $this->compileParameters($this->parameters, $queryBuilder));
52 45
        $output .= ')';
53
54 45
        return $output;
55
    }
56
57
    /**
58
     * @param array<mixed> $parameters
59
     * @param QueryBuilder $queryBuilder
60
     * @return array<string>
61
     */
62 45
    protected function compileParameters(
63
        array $parameters,
64
        QueryBuilder $queryBuilder
65
    ): array {
66 45
        $compiledParameters = [];
67
68
        /** @var Expression $parameter */
69 45
        foreach ($parameters as $key => $parameter) {
70 44
            if ($key === 'predicates') {
71
                /** @var PredicateExpression $parameter */
72 10
                $compiledParameters[] = $queryBuilder->compilePredicates($parameter);
73
74 10
                continue;
75
            }
76 44
            $compiledParameters[] = $parameter->compile($queryBuilder);
77
        }
78
79 45
        return $compiledParameters;
80
    }
81
82
    /**
83
     * Generate the name of the normalize function for this AQL function.
84
     */
85 44
    protected function getNormalizeFunctionName(): string
86
    {
87 44
        $value = ucwords(str_replace('_', ' ', strtolower($this->functionName)));
88
89 44
        return 'normalize' . str_replace(' ', '', $value);
90
    }
91
}
92