ExpressionLanguage   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 41
rs 10
c 1
b 0
f 0
ccs 0
cts 25
cp 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 3 1
A apply() 0 17 3
A getRuleCode() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Oliverde8\Component\RuleEngine\Rules;
6
7
/**
8
 * Class ExpressionLanguage
9
 *
10
 * @author    de Cramer Oliver<[email protected]>
11
 * @copyright 2018 Oliverde8
12
 * @package Oliverde8\Component\RuleEngine\Rules
13
 */
14
class ExpressionLanguage extends AbstractRule
15
{
16
    /**
17
     * @inheritdoc
18
     */
19
    public function apply(array $rowData, array &$transformedData, array $options = [])
20
    {
21
        $values = [
22
            'rowData' => $rowData,
23
            'transformedData' => $transformedData,
24
        ];
25
26
        if (isset($options['values'])) {
27
            $newOptions = $options;
28
            unset($newOptions['values']);
29
            foreach ($options['values'] as $valueKey => $value) {
30
                $values[$valueKey] = $this->ruleApplier->apply($rowData, $transformedData, $value, $newOptions);
31
            }
32
        }
33
34
        $expressionLanguage = new \Symfony\Component\ExpressionLanguage\ExpressionLanguage();
35
        return $expressionLanguage->evaluate($options['expression'], $values);
36
    }
37
38
    /**
39
     * Get unique code that needs to be used to apply this rule.
40
     *
41
     * @return string
42
     */
43
    public function getRuleCode(): string
44
    {
45
        return 'expression_language';
46
    }
47
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function validate(array $options): void
53
    {
54
        $this->requireOption('expression', $options);
55
    }
56
}