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
|
|
|
use ArekX\ArrayExpression\Exceptions\InvalidEvaluationResultType;
|
11
|
|
|
use ArekX\ArrayExpression\Interfaces\Operator;
|
12
|
|
|
use ArekX\ArrayExpression\Interfaces\ValueParser;
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* Class RegexOperator
|
16
|
|
|
* Operator for handling comparisons.
|
17
|
|
|
*
|
18
|
|
|
* @package ArekX\ArrayExpression\Operators
|
19
|
|
|
*/
|
20
|
|
|
class RegexOperator extends BaseOperator
|
21
|
|
|
{
|
22
|
|
|
/**
|
23
|
|
|
* Value which will be taken from value parser.
|
24
|
|
|
*
|
25
|
|
|
* @var Operator
|
26
|
|
|
*/
|
27
|
|
|
public $from;
|
28
|
|
|
|
29
|
|
|
/**
|
30
|
|
|
* @var Operator|string Regex result against which it will be matched.
|
31
|
|
|
*/
|
32
|
|
|
public $match;
|
33
|
|
|
|
34
|
|
|
/**
|
35
|
|
|
* @inheritDoc
|
36
|
|
|
*/
|
37
|
7 |
|
public function configure(array $config)
|
38
|
|
|
{
|
39
|
7 |
|
$this->setName($config[0] ?? 'unknown');
|
40
|
|
|
|
41
|
7 |
|
if (count($config) <= 1) {
|
42
|
1 |
|
throw new \InvalidArgumentException("Minimum format must be satisfied: ['{$this->getName()}', <expression>, '/pattern/']");
|
43
|
|
|
}
|
44
|
|
|
|
45
|
6 |
|
$this->assertIsExpression($config[1]);
|
46
|
|
|
|
47
|
6 |
|
$this->match = $config[2];
|
48
|
6 |
|
$this->from = $this->parser->parse($config[1]);
|
49
|
|
|
|
50
|
6 |
|
if (is_array($this->match)) {
|
51
|
2 |
|
$this->assertIsExpression($this->match);
|
52
|
2 |
|
$this->match = $this->parser->parse($this->match);
|
53
|
|
|
}
|
54
|
6 |
|
}
|
55
|
|
|
|
56
|
|
|
/**
|
57
|
|
|
* @inheritDoc
|
58
|
|
|
*/
|
59
|
6 |
|
public function evaluate(ValueParser $value)
|
60
|
|
|
{
|
61
|
6 |
|
$result = $this->from->evaluate($value);
|
62
|
|
|
|
63
|
6 |
|
if (!is_string($result)) {
|
64
|
1 |
|
throw new InvalidEvaluationResultType($result, 'string');
|
65
|
|
|
}
|
66
|
|
|
|
67
|
5 |
|
if (is_string($this->match)) {
|
68
|
3 |
|
return (bool)preg_match($this->match, $result);
|
69
|
|
|
}
|
70
|
|
|
|
71
|
2 |
|
$matchResult = $this->match->evaluate($value);
|
72
|
|
|
|
73
|
2 |
|
if (!is_string($matchResult)) {
|
74
|
1 |
|
throw new InvalidEvaluationResultType($result, 'string');
|
75
|
|
|
}
|
76
|
|
|
|
77
|
1 |
|
return (bool)preg_match($matchResult, $result);
|
78
|
|
|
}
|
79
|
|
|
} |