1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Queries\Builders\Interpretations; |
4
|
|
|
|
5
|
|
|
use Pinq\Parsing\IFunctionInterpreter; |
6
|
|
|
use Pinq\Queries; |
7
|
|
|
use Pinq\Queries\Builders\Functions\IFunction; |
8
|
|
|
use Pinq\Queries\Requests; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Implementation of the request interpreter. |
12
|
|
|
* |
13
|
|
|
* @author Elliot Levin <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class RequestParser extends BaseParser implements IRequestParser |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Queries\IRequest |
19
|
|
|
*/ |
20
|
|
|
protected $request; |
21
|
|
|
|
22
|
|
|
public function __construct(IFunctionInterpreter $functionInterpreter) |
23
|
|
|
{ |
24
|
|
|
parent::__construct($functionInterpreter); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getRequest() |
28
|
|
|
{ |
29
|
|
|
return $this->request ?: new Requests\Values(Requests\Values::AS_SELF); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function interpretGetIterator($requestId) |
33
|
|
|
{ |
34
|
|
|
$this->request = new Requests\Values(Requests\Values::AS_ARRAY_COMPATIBLE_ITERATOR); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function interpretGetTrueIterator($requestId) |
38
|
|
|
{ |
39
|
|
|
$this->request = new Requests\Values(Requests\Values::AS_TRUE_ITERATOR); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function interpretAsArray($requestId) |
43
|
|
|
{ |
44
|
|
|
$this->request = new Requests\Values(Requests\Values::AS_ARRAY); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function interpretAsCollection($requestId) |
48
|
|
|
{ |
49
|
|
|
$this->request = new Requests\Values(Requests\Values::AS_COLLECTION); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function interpretAsTraversable($requestId) |
53
|
|
|
{ |
54
|
|
|
$this->request = new Requests\Values(Requests\Values::AS_TRAVERSABLE); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function interpretOffsetGet($requestId, $indexId, $index) |
58
|
|
|
{ |
59
|
|
|
$this->request = new Requests\GetIndex($indexId); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function interpretOffsetExists($requestId, $indexId, $index) |
63
|
|
|
{ |
64
|
|
|
$this->request = new Requests\IssetIndex($indexId); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function interpretContains($requestId, $valueId, $value) |
68
|
|
|
{ |
69
|
|
|
$this->request = new Requests\Contains($valueId); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function interpretFirst($requestId) |
73
|
|
|
{ |
74
|
|
|
$this->request = new Requests\First(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function interpretLast($requestId) |
78
|
|
|
{ |
79
|
|
|
$this->request = new Requests\Last(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function interpretCount($requestId) |
83
|
|
|
{ |
84
|
|
|
$this->request = new Requests\Count(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function interpretIsEmpty($requestId) |
88
|
|
|
{ |
89
|
|
|
$this->request = new Requests\IsEmpty(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function interpretAggregate($requestId, IFunction $function) |
93
|
|
|
{ |
94
|
|
|
$this->request = new Requests\Aggregate( |
95
|
|
|
$this->buildFunction( |
96
|
|
|
$function, |
97
|
|
|
Queries\Functions\Aggregator::factory() |
98
|
|
|
)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function interpretMaximum($requestId, IFunction $function = null) |
102
|
|
|
{ |
103
|
|
|
$this->request = new Requests\Maximum($this->buildOptionalProjection($function)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function buildOptionalProjection(IFunction $function = null) |
107
|
|
|
{ |
108
|
|
|
if ($function === null) { |
109
|
|
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->buildFunction( |
113
|
|
|
$function, |
114
|
|
|
Queries\Functions\ElementProjection::factory() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function interpretMinimum($requestId, IFunction $function = null) |
119
|
|
|
{ |
120
|
|
|
$this->request = new Requests\Minimum($this->buildOptionalProjection($function)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function interpretSum($requestId, IFunction $function = null) |
124
|
|
|
{ |
125
|
|
|
$this->request = new Requests\Sum($this->buildOptionalProjection($function)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function interpretAverage($requestId, IFunction $function = null) |
129
|
|
|
{ |
130
|
|
|
$this->request = new Requests\Average($this->buildOptionalProjection($function)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function interpretAll($requestId, IFunction $function = null) |
134
|
|
|
{ |
135
|
|
|
$this->request = new Requests\All($this->buildOptionalProjection($function)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function interpretAny($requestId, IFunction $function = null) |
139
|
|
|
{ |
140
|
|
|
$this->request = new Requests\Any($this->buildOptionalProjection($function)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function interpretImplode($requestId, $delimiterId, $delimiter, IFunction $function = null) |
144
|
|
|
{ |
145
|
|
|
$this->request = new Requests\Implode( |
146
|
|
|
$delimiterId, |
147
|
|
|
$this->buildOptionalProjection($function)); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|