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
|
|
|
} |