-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expression.php
167 lines (141 loc) · 3.97 KB
/
Expression.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Prettus\FIQLParser;
use \Prettus\FIQLParser\Contracts\Element;
use \Prettus\FIQLParser\Element as BaseElement;
/**
* @author Anderson Andrade <[email protected]>
*/
class Expression extends BaseElement
{
/**
* @var array
*/
public $elements;
/**
* @var Expression
*/
public $workingFragment;
/**
* @var mixed
*/
public $lastElement;
/**
* @var Operator
*/
private $operator;
/**
*
*/
function __construct()
{
parent::__construct();
$this->elements = [];
$this->operator = null;
$this->workingFragment = $this;
$this->lastElement = null;
}
/**
* @return bool
*/
public function hasConstraint(): bool
{
return sizeof($this->elements) > 0;
}
/**
* @return Expression
*/
public function createNestedExpression(): Expression
{
$sub = new Expression();
$this->addElement($sub);
return $sub;
}
/**
* @param ...$elements
* @return Expression
*/
public function opAnd(...$elements): Expression
{
$expression = $this->addOperator(new Operator(';'));
foreach ($elements as $element) {
$expression->addElement($element);
}
return $expression;
}
/**
* @param ...$elements
* @return Expression
*/
public function opOr(...$elements): Expression
{
$expression = $this->addOperator(new Operator(','));
foreach ($elements as $element) {
$expression->addElement($element);
}
return $expression;
}
/**
* @param $element
* @return Expression
*/
public function addElement($element): Expression
{
if ($element instanceof Element) {
$element->setParent($this);
array_push($this->workingFragment->elements, $element);
return $this;
} else {
return $this->addOperator($element);
}
}
/**
* @param Operator $operator
* @return $this|Expression
*/
public function addOperator(Operator $operator): Expression
{
if (!$this->workingFragment->operator) {
$this->workingFragment->operator = $operator;
} else if ($operator->isGreaterThan($this->workingFragment->operator)) {
$lastConstraint = array_pop($this->workingFragment->elements);
$this->workingFragment = $this->workingFragment->createNestedExpression();
$this->workingFragment->addElement($lastConstraint);
$this->workingFragment->addOperator($operator);
} else if ($operator->isLessThan($this->workingFragment->operator)) {
if ($this->workingFragment->hasParent()) return $this->workingFragment->getParent()->addOperator($operator);
return (new Expression())->addElement($this->workingFragment)->addOperator($operator);
}
return $this;
}
/**
* @return array
*/
public function toArray(): array
{
$countElements = sizeof($this->elements);
if ($countElements == 0) return [];
if ($countElements == 1) return $this->elements[0]->toArray();
$operator = $this->operator ? : new Operator(';');
return [
$operator->toArray() => array_map(function ($el) {
return $el->toArray();
}, $this->elements)
];
}
/**
* @return string
*/
public function __toString()
{
$operator = $this->operator ?: new Operator(';');
$elementsStr = join(strval($operator), array_map('strval', $this->elements));
if ($this->hasParent()) {
$parent = $this->getParent();
$parentOperator = $parent->operator ?: new Operator(';');
if ($parentOperator->isGreaterThan($operator)) {
return sprintf("(%s)", $elementsStr);
}
}
return $elementsStr;
}
}