Evaluator::__construct()   A
last analyzed

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;
9
10
use ArekX\ArrayExpression\Interfaces\ValueParser;
11
use ArekX\ArrayExpression\ValueParsers\ArrayValueParser;
12
use ArekX\ArrayExpression\ValueParsers\ObjectValueParser;
13
use ArekX\ArrayExpression\ValueParsers\SingleValueParser;
14
15
/**
16
 * Class Evaluator
17
 *
18
 * Class which performs evaluation of arrays against passed values.
19
 *
20
 * @package ArekX\ArrayExpression
21
 */
22
class Evaluator
23
{
24
    /**
25
     * Expression parser used.
26
     *
27
     * @var null|ExpressionParser
28
     */
29
    protected $expressionParser = null;
30
31
    /**
32
     * Value Parser used for value retrieval.
33
     *
34
     * @var ValueParser
35
     */
36
    protected $valueParser;
37
38
    /**
39
     * Evaluator constructor.
40
     * @param null|ExpressionParser $expressionParser Parser which will be used to parse array expression.
41
     */
42 5
    public function __construct($expressionParser = null)
43
    {
44 5
        $this->expressionParser = $expressionParser;
45 5
    }
46
47
    /**
48
     * Creates new instance of an evaluator.
49
     *
50
     * @param null|ExpressionParser $expressionParser Parser which will be used to parse array expression.
51
     * @return static
52
     */
53 5
    public static function create($expressionParser = null)
54
    {
55 5
        return new static($expressionParser);
56
    }
57
58
    /**
59
     * Run performs evaluation of expression against a value.
60
     *
61
     * @param array $expression Expression which will be used
62
     * @param mixed $value Value which will be used.
63
     * @return mixed
64
     * @throws Exceptions\TypeNotMappedException
65
     * @throws Exceptions\InvalidValueTypeException
66
     */
67 3
    public function run(array $expression, $value)
68
    {
69 3
        return $this->getExpressionParser()
70 3
            ->parse($expression)
71 3
            ->evaluate($this->determineValueParser($value));
72
    }
73
74
    /**
75
     * Determines Value Parser from a value.
76
     *
77
     * @param mixed $value
78
     * @return ValueParser|ArrayValueParser|ObjectValueParser|SingleValueParser
79
     * @throws Exceptions\InvalidValueTypeException
80
     */
81 3
    protected function determineValueParser($value)
82
    {
83 3
        $valueParser = $this->valueParser;
84
85 3
        if (is_array($value)) {
86 1
            if (!($this->valueParser instanceof ArrayValueParser)) {
87 1
                $valueParser = $this->valueParser = new ArrayValueParser();
88
            }
89
90 1
            $valueParser->setRaw($value);
91 1
            return $valueParser;
92
        }
93
94 2
        if (is_object($value)) {
95 1
            if (!($this->valueParser instanceof ObjectValueParser)) {
96 1
                $valueParser = $this->valueParser = new ObjectValueParser();
97
            }
98
99 1
            $valueParser->setRaw($value);
100 1
            return $valueParser;
101
        }
102
103 1
        if (!($valueParser instanceof SingleValueParser)) {
104 1
            $valueParser = $this->valueParser = new SingleValueParser();
105 1
            $valueParser->setRaw($value);
106
        }
107
108 1
        return $valueParser;
109
    }
110
111
    /**
112
     * Returns expression parser.
113
     *
114
     * @return ExpressionParser
115
     */
116 3
    public function getExpressionParser()
117
    {
118 3
        if ($this->expressionParser === null) {
119 3
            $this->expressionParser = new DefaultExpressionParser();
120
        }
121
122 3
        return $this->expressionParser;
123
    }
124
}