Skip to content

Commit 8f005fa

Browse files
author
TCB13
committed
Custom pipeline with the QueryBuilder::pipeline() method. More PSR.
1 parent 250d25b commit 8f005fa

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/QueryBuilder.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class QueryBuilder
3434
private $group = [];
3535
private $indexBy = null;
3636

37+
private $customPipeline = [];
38+
3739
private $expectedMultipleResults = true;
3840

3941
protected static $mongoOperatorMap = [
@@ -104,7 +106,11 @@ public function find(): self
104106
public function findAll(): self
105107
{
106108
// Build the pipeline
107-
$pipeline = [];
109+
if (is_object($this->customPipeline) && !empty($this->customPipeline->pipeline) && $this->customPipeline->beforeQb) {
110+
$pipeline = $this->customPipeline->pipeline;
111+
} else {
112+
$pipeline = [];
113+
}
108114

109115
if (!empty($this->lookup)) // Lookups should ALWAYS be the first ones
110116
{
@@ -125,7 +131,9 @@ public function findAll(): self
125131
];
126132
}
127133

128-
$pipeline[] = ["\$sort" => $this->order];
134+
if (!is_object($this->customPipeline) || empty($this->customPipeline->pipeline)) {
135+
$pipeline[] = ["\$sort" => $this->order];
136+
}
129137

130138
if ($this->skip > 0) {
131139
$pipeline[] = ["\$skip" => $this->skip];
@@ -146,6 +154,10 @@ public function findAll(): self
146154
$pipeline[] = $this->group;
147155
}
148156

157+
if (is_object($this->customPipeline) && !empty($this->customPipeline->pipeline) && !$this->customPipeline->beforeQb) {
158+
$pipeline = array_merge($pipeline, $this->customPipeline->pipeline);
159+
}
160+
149161
// @todo: check if the pipeline has enough information to run a query!
150162
if (!isset($this->collection)) {
151163
throw new Exception("You must set a collection!");
@@ -169,7 +181,7 @@ public function select($fields): self
169181
}
170182
// ArrayLength Function or COUNT feature
171183
if ($field instanceof ArrayLength /*|| $field instanceof Count*/) {
172-
$arrayLen = $field->asArray();
184+
$arrayLen = $field->asArray();
173185
$this->addFields[] = $arrayLen;
174186
return [key($arrayLen) => 1];
175187
}
@@ -351,6 +363,15 @@ public function getNormalizedFilters(): array
351363
return $this->filters;
352364
}
353365

366+
public function pipeline(array $pipeline, bool $beforeQueryBuilderPipeline = false): self
367+
{
368+
$this->customPipeline = new \stdClass();
369+
$this->customPipeline->beforeQb = $beforeQueryBuilderPipeline;
370+
$this->customPipeline->pipeline = $pipeline;
371+
372+
return $this;
373+
}
374+
354375
public function join($collection, $localField, $operatorOrForeignField, $foreignField = null): self
355376
{
356377
// Allow users to skip the JOIN operator
@@ -438,8 +459,7 @@ public function order(string $field, $sort = "DESC"): self
438459
public function count(): int
439460
{
440461
$this->count = ["\$count" => "count"];
441-
$result = $this->findAll()
442-
->toArray();
462+
$result = $this->findAll()->toArray();
443463
if (empty($result)) {
444464
return 0;
445465
}

0 commit comments

Comments
 (0)