-
Notifications
You must be signed in to change notification settings - Fork 0
/
Constraint.php
86 lines (76 loc) · 2.03 KB
/
Constraint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Prettus\FIQLParser;
use \Prettus\FIQLParser\Exceptions\FIQLObjectException;
use \Prettus\FIQLParser\Element as BaseElement;
const COMPARISON_MAP = [
'==' => '==',
'!=' => '!=',
'=gt=' => '>',
'=ge=' => '>=',
'=lt=' => '<',
'=le=' => '<=',
];
function isValidComparison($comparison)
{
preg_match_all(Constants::COMPARISON_COMP, $comparison, $matches, PREG_SET_ORDER);
return sizeof($matches) > 0;
}
/**
* @author Anderson Andrade <[email protected]>
*/
class Constraint extends BaseElement
{
/**
* @param string $selector
* @param string $comparison
* @param string $argument
* @throws FIQLObjectException
*/
function __construct(string $selector, $comparison = '', $argument = '')
{
parent::__construct();
if ($comparison and !isValidComparison($comparison)) {
throw new FIQLObjectException(sprintf("'%s' is not a valid FIQL comparison", $comparison));
}
$this->selector = $selector;
$this->comparison = $comparison;
$this->argument = $argument;
}
/**
* @return array
*/
public function toArray(): array
{
$value = array_key_exists($this->comparison, COMPARISON_MAP) ? COMPARISON_MAP[$this->comparison] : null;
return [
$this->selector,
$value ? : $this->comparison,
$this->argument
];
}
/**
* @return string
*/
public function __toString(): string
{
return sprintf('%s%s%s', $this->selector, $this->comparison, $this->argument);
}
/**
* @param ...$elements
* @return Expression
*/
public function opOr(...$elements): Expression
{
$expression = new Expression();
return $expression->opOr($this, ...$elements);
}
/**
* @param ...$elements
* @return Expression
*/
public function opAnd(...$elements): Expression
{
$expression = new Expression();
return $expression->opAnd($this, ...$elements);
}
}