forked from php-etl/akeneo-expression-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilter.php
36 lines (30 loc) · 1017 Bytes
/
Filter.php
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
<?php declare(strict_types=1);
namespace Kiboko\Component\ETL\ExpressionLanguage\Akeneo;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
final class Filter extends ExpressionFunction
{
public function __construct($name)
{
parent::__construct(
$name,
\Closure::fromCallable([$this, 'compile'])->bindTo($this),
\Closure::fromCallable([$this, 'evaluate'])->bindTo($this)
);
}
private function compile(string $input, string ...$callbacks)
{
$output = $input;
foreach (array_reverse($callbacks) as $callback) {
$output = sprintf('(%s)(%s)', $callback, $output);
}
return sprintf('array_values(%s)', $output);
}
private function evaluate(array $context, array $input, callable ...$callbacks)
{
$output = $input;
foreach (array_reverse($callbacks) as $callback) {
$output = $callback($output);
}
return array_values($output);
}
}