Passed
Push — master ( bafcc7...55396a )
by Aleksandar
02:08 queued 11s
created

GetOperator::evaluate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 $name;
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 10
    public function configure(array $config)
47
    {
48 10
        $this->setName($config[0] ?? 'unknown');
49
50 10
        $this->name = $config[1] ?? '';
51 10
        $this->default = $config['default'] ?? null;
52 10
    }
53
54
    /**
55
     * Evaluates one value.
56
     *
57
     * @param ValueParser $value Value to be evaluated
58
     * @return mixed Evaluation result
59
     */
60 9
    public function evaluate(ValueParser $value)
61
    {
62 9
        return $value->getValue($this->name, $this->default);
63
    }
64
65
}