GetOperator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 46
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A evaluate() 0 3 1
A configure() 0 10 2
1
<?php
2
/**
3
 * @author Aleksandar Panic
4
 * @license http://www.apache.org/licenses/LICENSE-2.0
5
 * @since 1.0.0
6
 **/
7
8
namespace ArekX\ArrayExpression\Operators;
9
10
11
use ArekX\ArrayExpression\Interfaces\Operator;
12
use ArekX\ArrayExpression\Interfaces\ExpressionParser;
13
use ArekX\ArrayExpression\Interfaces\ValueParser;
14
15
/**
16
 * Class GetOperator
17
 * Operator for returning static values from value parser
18
 *
19
 * @package ArekX\ArrayExpression\Operators
20
 */
21
class GetOperator extends BaseOperator
22
{
23
    /**
24
     * Name of the key from which value will be returned.
25
     *
26
     * @var string
27
     */
28
    public $key;
29
30
    /**
31
     * Default value to be returned if that key was not found.
32
     *
33
     * @var mixed
34
     */
35
    public $default;
36
37
    /**
38
     * Passes data from operator configuration.
39
     *
40
     * Depending on the operator this data can contain other sub-expressions which need to be parsed using
41
     * ExpressionParser
42
     *
43
     * @param array $config Expressions to be processed
44
     * @see ExpressionParser
45
     */
46 17
    public function configure(array $config)
47
    {
48 17
        $this->setName($config[0] ?? 'unknown');
49
50 17
        if (count($config) < 1) {
51 1
            throw new \InvalidArgumentException("Minimum format must be satisfied: ['{$this->getName()}']");
52
        }
53
54 16
        $this->key = $config[1] ?? '';
55 16
        $this->default = $config['default'] ?? null;
56 16
    }
57
58
    /**
59
     * Evaluates one value.
60
     *
61
     * @param ValueParser $value Value to be evaluated
62
     * @return mixed Evaluation result
63
     */
64 13
    public function evaluate(ValueParser $value)
65
    {
66 13
        return $value->getValue($this->key, $this->default);
67
    }
68
69
}