ConcatOperator::configure()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 3
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\Exceptions\InvalidEvaluationResultType;
12
use ArekX\ArrayExpression\Interfaces\ExpressionParser;
13
use ArekX\ArrayExpression\Interfaces\Operator;
14
use ArekX\ArrayExpression\Interfaces\ValueParser;
15
16
/**
17
 * Class ConcatOperator
18
 * Operator for returning concatenated strings
19
 *
20
 * @package ArekX\ArrayExpression\Operators
21
 */
22
class ConcatOperator extends BaseOperator
23
{
24
    /**
25
     * Expressions of which will be in concat operation.
26
     *
27
     * @var Operator[]
28
     */
29
    public $concatOperators = [];
30
31
    /**
32
     * Passes data from operator configuration.
33
     *
34
     * Depending on the operator this data can contain other sub-expressions which need to be parsed using
35
     * ExpressionParser
36
     *
37
     * @param array $config Expressions to be processed
38
     * @see ExpressionParser
39
     */
40 6
    public function configure(array $config)
41
    {
42 6
        $this->setName($config[0] ?? 'unknown');
43
44 6
        if (count($config) < 2) {
45 2
            throw new \InvalidArgumentException("Minimum format must be satisfied: ['{$this->getName()}', <expression>]");
46
        }
47
48 4
        $this->concatOperators = [];
49
50 4
        $maxCount = count($config);
51 4
        for ($i = 1; $i < $maxCount; $i++) {
52 4
            $this->assertIsExpression($config[$i]);
53 4
            $this->concatOperators[] = $this->parser->parse($config[$i]);
54
        }
55 4
    }
56
57
    /**
58
     * Evaluates one value.
59
     *
60
     * @param ValueParser $value Value to be evaluated
61
     * @return string Evaluation result
62
     * @throws InvalidEvaluationResultType
63
     */
64 2
    public function evaluate(ValueParser $value)
65
    {
66 2
        $result = "";
67 2
        foreach ($this->concatOperators as $operator) {
68 2
            $concatResult = $operator->evaluate($value);
69
70 2
            if (!is_string($concatResult)) {
71 1
                throw new InvalidEvaluationResultType($concatResult, 'string');
72
            }
73
74 2
            $result .= $concatResult;
75
        }
76
77 1
        return $result;
78
    }
79
80
}