-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExpressionLanguageValueMapper.php
62 lines (52 loc) · 1.92 KB
/
ExpressionLanguageValueMapper.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Kiboko\Component\FastMap\Mapping\Field;
use Kiboko\Component\FastMap\Compiler\Builder\ExpressionLanguageToPhpParserBuilder;
use Kiboko\Contract\Mapping;
use PhpParser\Node;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\PropertyPathInterface;
final class ExpressionLanguageValueMapper implements Mapping\FieldMapperInterface, Mapping\CompilableMapperInterface
{
private readonly PropertyAccessor $accessor;
/** @var Node\Expr\Variable[] */
private iterable $contextVariables = [];
/**
* @param array<string, mixed> $variables
*/
public function __construct(
private readonly ExpressionLanguage $interpreter,
private readonly Expression $expression,
private readonly array $variables = []
) {
$this->accessor = PropertyAccess::createPropertyAccessor();
}
public function __invoke($input, $output, PropertyPathInterface $outputPath)
{
$this->accessor->setValue(
$output,
$outputPath,
$this->interpreter->evaluate($this->expression, [...$this->variables, 'input' => $input, 'output' => $output])
);
return $output;
}
public function addContextVariable(Node\Expr\Variable $variable): self
{
$this->contextVariables[] = $variable;
return $this;
}
public function compile(Node\Expr $outputNode): array
{
return [
new Node\Stmt\Expression(
new Node\Expr\Assign(
$outputNode,
(new ExpressionLanguageToPhpParserBuilder($this->interpreter, $this->expression, array_keys($this->variables)))->getNode()
),
),
];
}
}