forked from php-etl/akeneo-expression-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocale.php
43 lines (35 loc) · 1.1 KB
/
Locale.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
37
38
39
40
41
42
43
<?php declare(strict_types=1);
namespace Kiboko\Component\ETL\ExpressionLanguage\Akeneo;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
final class Locale 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 ...$locales)
{
$pattern =<<<"PATTERN"
function (array \$input): array {
\$output = array_filter(\$input, function(array \$item) {
return in_array(\$item['locale'], [%s]);
});
return \$output;
}
PATTERN;
return sprintf($pattern, implode(', ', $locales));
}
private function evaluate(array $context, string ...$locales)
{
return function (array $input) use($locales): array {
$output = array_filter($input, function(array $item) use($locales) {
return in_array($item['locale'], $locales);
});
return $output;
};
}
}