1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Queries\Builders\Interpretations; |
4
|
|
|
|
5
|
|
|
use Pinq\IQueryable; |
6
|
|
|
use Pinq\Queries; |
7
|
|
|
use Pinq\Queries\Builders\Functions\IFunction; |
8
|
|
|
use Pinq\Queries\Functions; |
9
|
|
|
use Pinq\Queries\Segments; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Implementation of the scope parser |
13
|
|
|
* |
14
|
|
|
* @author Elliot Levin <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ScopeParser extends BaseParser implements IScopeParser |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The parsed query segments. |
20
|
|
|
* |
21
|
|
|
* @var Queries\ISegment[] |
22
|
|
|
*/ |
23
|
|
|
protected $segments = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Queries\ISourceInfo |
27
|
|
|
*/ |
28
|
|
|
protected $sourceInfo; |
29
|
|
|
|
30
|
|
|
public function getScope() |
31
|
|
|
{ |
32
|
|
|
return new Queries\Scope($this->sourceInfo, $this->segments); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function buildSourceInterpretation() |
36
|
|
|
{ |
37
|
|
|
return new SourceParser($this->functionInterpreter); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function buildJoinOptionsInterpretation() |
41
|
|
|
{ |
42
|
|
|
return new JoinOptionsParser($this->functionInterpreter); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function buildScopeInterpretation() |
46
|
|
|
{ |
47
|
|
|
return new self($this->functionInterpreter); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function interpretScopeSource(IQueryable $queryable) |
51
|
|
|
{ |
52
|
|
|
$this->sourceInfo = $queryable->getSourceInfo(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param IFunction $function |
57
|
|
|
* |
58
|
|
|
* @return Functions\ElementProjection |
59
|
|
|
*/ |
60
|
|
|
final protected function buildElementProjection(IFunction $function) |
61
|
|
|
{ |
62
|
|
|
return $this->buildFunction( |
63
|
|
|
$function, |
64
|
|
|
Functions\ElementProjection::factory() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function interpretWhere($segmentId, IFunction $predicate) |
69
|
|
|
{ |
70
|
|
|
$this->segments[] = new Segments\Filter($this->buildElementProjection($predicate)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function interpretOrderings($segmentId, array $orderings) |
74
|
|
|
{ |
75
|
|
|
$orderingSections = []; |
76
|
|
|
|
77
|
|
|
foreach ($orderings as $ordering) { |
78
|
|
|
list($projection, $isAscendingId, $isAscendingValue) = $ordering; |
79
|
|
|
$orderingSections[] = new Segments\Ordering( |
80
|
|
|
$this->buildElementProjection($projection), |
81
|
|
|
$isAscendingId); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->segments[] = new Segments\OrderBy($orderingSections); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function interpretSlice($segmentId, $startId, $start, $amountId, $amount) |
88
|
|
|
{ |
89
|
|
|
$this->segments[] = new Segments\Range($startId, $amountId); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function interpretIndexBy($segmentId, IFunction $projection) |
93
|
|
|
{ |
94
|
|
|
$this->segments[] = new Segments\IndexBy($this->buildElementProjection($projection)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function interpretKeys($segmentId) |
98
|
|
|
{ |
99
|
|
|
$this->segments[] = new Segments\Keys(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function interpretReindex($segmentId) |
103
|
|
|
{ |
104
|
|
|
$this->segments[] = new Segments\Reindex(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function interpretGroupBy($segmentId, IFunction $projection) |
108
|
|
|
{ |
109
|
|
|
$this->segments[] = new Segments\GroupBy($this->buildElementProjection($projection)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function interpretJoin( |
113
|
|
|
$segmentId, |
114
|
|
|
IJoinOptionsInterpretation $joinOptionsInterpretation, |
115
|
|
|
IFunction $joinToFunction |
116
|
|
|
) { |
117
|
|
|
/* @var $joinOptionsInterpretation IJoinOptionsParser */ |
118
|
|
|
$this->segments[] = new Segments\Join( |
119
|
|
|
$joinOptionsInterpretation->getJoinOptions(), |
120
|
|
|
$this->buildFunction($joinToFunction, Functions\ConnectorProjection::factory())); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function interpretSelect($segmentId, IFunction $projection) |
124
|
|
|
{ |
125
|
|
|
$this->segments[] = new Segments\Select($this->buildElementProjection($projection)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function interpretSelectMany($segmentId, IFunction $projection) |
129
|
|
|
{ |
130
|
|
|
$this->segments[] = new Segments\SelectMany($this->buildElementProjection($projection)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function interpretUnique($segmentId) |
134
|
|
|
{ |
135
|
|
|
$this->segments[] = new Segments\Unique(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function interpretOperation($segmentId, $operationType, ISourceInterpretation $sourceInterpretation) |
139
|
|
|
{ |
140
|
|
|
/* @var $sourceInterpretation ISourceParser */ |
141
|
|
|
$this->segments[] = new Segments\Operation( |
142
|
|
|
$operationType, |
143
|
|
|
$sourceInterpretation->getSource()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|