|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pinq\Providers\Traversable; |
|
4
|
|
|
|
|
5
|
|
|
use Pinq\Direction; |
|
6
|
|
|
use Pinq\ITraversable; |
|
7
|
|
|
use Pinq\PinqException; |
|
8
|
|
|
use Pinq\Queries; |
|
9
|
|
|
use Pinq\Queries\Common; |
|
10
|
|
|
use Pinq\Queries\Segments; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Evaluates the query scope against the supplied traversable |
|
14
|
|
|
* |
|
15
|
|
|
* @author Elliot Levin <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class ScopeEvaluator extends Segments\SegmentVisitor |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ITraversable |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $traversable; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Queries\IResolvedParameterRegistry |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $resolvedParameters; |
|
28
|
|
|
|
|
29
|
|
|
private function __construct( |
|
30
|
|
|
ITraversable $traversable, |
|
31
|
|
|
Queries\IResolvedParameterRegistry $resolvedParameters |
|
32
|
|
|
) { |
|
33
|
|
|
$this->traversable = $traversable; |
|
34
|
|
|
$this->resolvedParameters = $resolvedParameters; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function visitFilter(Segments\Filter $query) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->traversable = $this->traversable->where( |
|
40
|
|
|
$this->resolvedParameters[$query->getProjectionFunction()->getCallableId()] |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function visitRange(Segments\Range $query) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->traversable = |
|
47
|
|
|
$this->traversable->slice( |
|
48
|
|
|
$this->resolvedParameters[$query->getStartId()], |
|
49
|
|
|
$this->resolvedParameters[$query->getAmountId()] |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function visitSelect(Segments\Select $query) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->traversable = $this->traversable->select( |
|
56
|
|
|
$this->resolvedParameters[$query->getProjectionFunction()->getCallableId()] |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function visitSelectMany(Segments\SelectMany $query) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->traversable = $this->traversable->selectMany( |
|
63
|
|
|
$this->resolvedParameters[$query->getProjectionFunction()->getCallableId()] |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function visitUnique(Segments\Unique $query) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->traversable = $this->traversable->unique(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function visitOrderBy(Segments\OrderBy $query) |
|
73
|
|
|
{ |
|
74
|
|
|
$first = true; |
|
75
|
|
|
|
|
76
|
|
|
foreach ($query->getOrderings() as $orderFunction) { |
|
77
|
|
|
$direction = $this->resolvedParameters[$orderFunction->getIsAscendingId( |
|
78
|
|
|
)] ? Direction::ASCENDING : Direction::DESCENDING; |
|
79
|
|
|
|
|
80
|
|
|
if ($first) { |
|
81
|
|
|
$this->traversable = $this->traversable->orderBy( |
|
82
|
|
|
$this->resolvedParameters[$orderFunction->getProjectionFunction()->getCallableId()], |
|
83
|
|
|
$direction |
|
84
|
|
|
); |
|
85
|
|
|
$first = false; |
|
86
|
|
|
} else { |
|
87
|
|
|
$this->traversable = $this->traversable->thenBy( |
|
88
|
|
|
$this->resolvedParameters[$orderFunction->getProjectionFunction()->getCallableId()], |
|
89
|
|
|
$direction |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function visitGroupBy(Segments\GroupBy $query) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->traversable = $this->traversable->groupBy( |
|
98
|
|
|
$this->resolvedParameters[$query->getProjectionFunction()->getCallableId()] |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function visitJoin(Segments\Join $query) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->traversable = self::evaluateJoinOptions( |
|
105
|
|
|
$this->traversable, |
|
106
|
|
|
$query->getOptions(), |
|
107
|
|
|
$this->resolvedParameters |
|
108
|
|
|
) |
|
109
|
|
|
->to($this->resolvedParameters[$query->getJoiningFunction()->getCallableId()]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Evaluates the join segment values and filter upon the supplied traversable. |
|
114
|
|
|
* |
|
115
|
|
|
* @param ITraversable $traversable |
|
116
|
|
|
* @param Common\Join\Options $join |
|
117
|
|
|
* @param Queries\IResolvedParameterRegistry $resolvedParameters |
|
118
|
|
|
* |
|
119
|
|
|
* @return \Pinq\Interfaces\IJoiningToTraversable |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function evaluateJoinOptions( |
|
122
|
|
|
ITraversable $traversable, |
|
123
|
|
|
Common\Join\Options $join, |
|
124
|
|
|
Queries\IResolvedParameterRegistry $resolvedParameters |
|
125
|
|
|
) { |
|
126
|
|
|
$values = self::evaluateSource($join->getSource(), $resolvedParameters); |
|
127
|
|
|
$joiningTraversable = $join->isGroupJoin() ? $traversable->groupJoin($values) : $traversable->join($values); |
|
128
|
|
|
|
|
129
|
|
|
if ($join->hasFilter()) { |
|
130
|
|
|
$filter = $join->getFilter(); |
|
131
|
|
|
|
|
132
|
|
|
if ($filter instanceof Common\Join\Filter\Custom) { |
|
133
|
|
|
$joiningTraversable = $joiningTraversable->on( |
|
134
|
|
|
$resolvedParameters[$filter->getOnFunction()->getCallableId()] |
|
135
|
|
|
); |
|
136
|
|
|
} elseif ($filter instanceof Common\Join\Filter\Equality) { |
|
137
|
|
|
$joiningTraversable = $joiningTraversable->onEquality( |
|
138
|
|
|
$resolvedParameters[$filter->getOuterKeyFunction()->getCallableId()], |
|
139
|
|
|
$resolvedParameters[$filter->getInnerKeyFunction()->getCallableId()] |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ($join->hasDefault()) { |
|
145
|
|
|
$joiningTraversable = $joiningTraversable->withDefault( |
|
146
|
|
|
$resolvedParameters[$join->getDefaultValueId()], |
|
147
|
|
|
$resolvedParameters[$join->getDefaultKeyId()] |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $joiningTraversable; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public static function evaluateSource( |
|
155
|
|
|
Common\ISource $source, |
|
156
|
|
|
Queries\IResolvedParameterRegistry $resolvedParameters |
|
157
|
|
|
) { |
|
158
|
|
|
if ($source instanceof Common\Source\ArrayOrIterator) { |
|
159
|
|
|
return $resolvedParameters[$source->getId()]; |
|
160
|
|
|
} elseif ($source instanceof Common\Source\SingleValue) { |
|
161
|
|
|
return [$resolvedParameters[$source->getId()]]; |
|
162
|
|
|
} elseif ($source instanceof Common\Source\QueryScope) { |
|
163
|
|
|
return self::evaluate($source->getScope(), $resolvedParameters); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public static function evaluate( |
|
168
|
|
|
Queries\IScope $scope, |
|
169
|
|
|
Queries\IResolvedParameterRegistry $resolvedParameters |
|
170
|
|
|
) { |
|
171
|
|
|
$sourceInfo = $scope->getSourceInfo(); |
|
172
|
|
|
if (!($sourceInfo instanceof SourceInfo)) { |
|
173
|
|
|
throw new PinqException( |
|
174
|
|
|
'Incompatible query source: expecting source info of type %s, %s given', |
|
175
|
|
|
SourceInfo::SOURCE_INFO_TYPE, |
|
176
|
|
|
get_class($sourceInfo)); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$evaluator = new self($sourceInfo->getTraversable(), $resolvedParameters); |
|
180
|
|
|
$evaluator->visit($scope); |
|
181
|
|
|
|
|
182
|
|
|
return $evaluator->traversable; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function visitIndexBy(Segments\IndexBy $query) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->traversable = $this->traversable->indexBy( |
|
188
|
|
|
$this->resolvedParameters[$query->getProjectionFunction()->getCallableId()] |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function visitKeys(Segments\Keys $query) |
|
193
|
|
|
{ |
|
194
|
|
|
$this->traversable = $this->traversable->keys(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function visitReindex(Segments\Reindex $query) |
|
198
|
|
|
{ |
|
199
|
|
|
$this->traversable = $this->traversable->reindex(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function visitOperation(Segments\Operation $query) |
|
203
|
|
|
{ |
|
204
|
|
|
$otherValues = self::evaluateSource($query->getSource(), $this->resolvedParameters); |
|
205
|
|
|
switch ($query->getOperationType()) { |
|
206
|
|
|
|
|
207
|
|
|
case Segments\Operation::UNION: |
|
208
|
|
|
$this->traversable = $this->traversable->union($otherValues); |
|
209
|
|
|
break; |
|
210
|
|
|
|
|
211
|
|
|
case Segments\Operation::INTERSECT: |
|
212
|
|
|
$this->traversable = $this->traversable->intersect($otherValues); |
|
213
|
|
|
break; |
|
214
|
|
|
|
|
215
|
|
|
case Segments\Operation::DIFFERENCE: |
|
216
|
|
|
$this->traversable = $this->traversable->difference($otherValues); |
|
217
|
|
|
break; |
|
218
|
|
|
|
|
219
|
|
|
case Segments\Operation::APPEND: |
|
220
|
|
|
$this->traversable = $this->traversable->append($otherValues); |
|
221
|
|
|
break; |
|
222
|
|
|
|
|
223
|
|
|
case Segments\Operation::WHERE_IN: |
|
224
|
|
|
$this->traversable = $this->traversable->whereIn($otherValues); |
|
225
|
|
|
break; |
|
226
|
|
|
|
|
227
|
|
|
case Segments\Operation::EXCEPT: |
|
228
|
|
|
$this->traversable = $this->traversable->except($otherValues); |
|
229
|
|
|
break; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|