Skip to content

Commit 0e82e24

Browse files
committedMar 11, 2024
Rename AggregationBuilder class
1 parent bbfd062 commit 0e82e24

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed
 

‎src/Query/PipelineBuilder.php renamed to ‎src/Query/AggregationBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use function array_replace;
1313
use function collect;
1414

15-
final class PipelineBuilder extends FluentFactory
15+
final class AggregationBuilder extends FluentFactory
1616
{
1717
public function __construct(
1818
array $pipeline,

‎src/Query/Builder.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -282,40 +282,40 @@ public function dump(mixed ...$args)
282282
return $this;
283283
}
284284

285-
private function getPipelineBuilder(): PipelineBuilder
285+
private function getAggregationBuilder(): AggregationBuilder
286286
{
287-
$pipelineBuilder = new PipelineBuilder([], $this->collection, $this->options);
287+
$agg = new AggregationBuilder([], $this->collection, $this->options);
288288

289289
$wheres = $this->compileWheres();
290290

291291
if (count($wheres)) {
292-
$pipelineBuilder->match(...$wheres);
292+
$agg->match(...$wheres);
293293
}
294294

295295
// Distinct query
296296
if ($this->distinct) {
297297
// Return distinct results directly
298298
$column = $columns[0] ?? '_id';
299299

300-
$pipelineBuilder->group(
300+
$agg->group(
301301
_id: \MongoDB\Builder\Expression::fieldPath($column),
302302
_document: Accumulator::first(Variable::root()),
303303
);
304-
$pipelineBuilder->replaceRoot(
304+
$agg->replaceRoot(
305305
newRoot: new FieldPath('_document'),
306306
);
307307
}
308308

309309
if ($this->orders) {
310-
$pipelineBuilder->sort(...$this->orders);
310+
$agg->sort(...$this->orders);
311311
}
312312

313313
if ($this->offset) {
314-
$pipelineBuilder->skip($this->offset);
314+
$agg->skip($this->offset);
315315
}
316316

317317
if ($this->limit) {
318-
$pipelineBuilder->limit($this->limit);
318+
$agg->limit($this->limit);
319319
}
320320

321321
// Normal query
@@ -324,11 +324,11 @@ private function getPipelineBuilder(): PipelineBuilder
324324
$columns = in_array('*', $this->columns) ? [] : $this->columns;
325325
$projection = array_fill_keys($columns, true) + $this->projections;
326326
if ($projection) {
327-
$pipelineBuilder->project(...$projection);
327+
$agg->project(...$projection);
328328
}
329329
}
330330

331-
return $pipelineBuilder;
331+
return $agg;
332332
}
333333

334334
/**
@@ -594,7 +594,7 @@ public function generateCacheKey()
594594
public function aggregate($function = null, $columns = [])
595595
{
596596
if ($function === null) {
597-
return $this->getPipelineBuilder();
597+
return $this->getAggregationBuilder();
598598
}
599599

600600
$this->aggregate = [

‎tests/Models/User.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function username(): Attribute
130130
{
131131
return Attribute::make(
132132
get: fn ($value) => $value,
133-
set: fn ($value) => Str::slug($value)
133+
set: fn ($value) => Str::slug($value),
134134
);
135135
}
136136

‎tests/Query/PipelineBuilderTest.php renamed to ‎tests/Query/AggregationBuilderTest.php

+11-13
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
use MongoDB\Builder\Type\Sort;
1515
use MongoDB\Builder\Type\TimeUnit;
1616
use MongoDB\Builder\Variable;
17-
use MongoDB\Laravel\Query\PipelineBuilder;
17+
use MongoDB\Laravel\Query\AggregationBuilder;
1818
use MongoDB\Laravel\Tests\Models\User;
1919
use MongoDB\Laravel\Tests\TestCase;
2020

2121
use function assert;
2222

23-
class PipelineBuilderTest extends TestCase
23+
class AggregationBuilderTest extends TestCase
2424
{
2525
public function tearDown(): void
2626
{
@@ -35,8 +35,11 @@ public function testCreateFromQueryBuilder(): void
3535
]);
3636

3737
// Create the aggregation pipeline from the query builder
38-
$pipeline = User::where('name', 'John Doe')->aggregate();
39-
assert($pipeline instanceof PipelineBuilder);
38+
$pipeline = User::where('name', 'John Doe')
39+
->limit(10)
40+
->offset(0)
41+
->aggregate();
42+
assert($pipeline instanceof AggregationBuilder);
4043
$pipeline
4144
->addFields(
4245
age: Expression::dateDiff(
@@ -57,9 +60,8 @@ public function testCreateFromQueryBuilder(): void
5760
// Compare with the expected pipeline
5861
$expected = Document::fromPHP([
5962
'pipeline' => [
60-
[
61-
'$match' => ['name' => 'John Doe'],
62-
],
63+
['$match' => ['name' => 'John Doe']],
64+
['$limit' => 10],
6365
[
6466
'$addFields' => [
6567
'age' => [
@@ -71,12 +73,8 @@ public function testCreateFromQueryBuilder(): void
7173
],
7274
],
7375
],
74-
[
75-
'$sort' => ['age' => -1, 'name' => 1],
76-
],
77-
[
78-
'$unset' => ['birthday'],
79-
],
76+
['$sort' => ['age' => -1, 'name' => 1]],
77+
['$unset' => ['birthday']],
8078
],
8179
])->toCanonicalExtendedJSON();
8280

‎tests/Query/BuilderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
use MongoDB\BSON\UTCDateTime;
1818
use MongoDB\Builder\BuilderEncoder;
1919
use MongoDB\Laravel\Connection;
20+
use MongoDB\Laravel\Query\AggregationBuilder;
2021
use MongoDB\Laravel\Query\Builder;
2122
use MongoDB\Laravel\Query\Grammar;
22-
use MongoDB\Laravel\Query\PipelineBuilder;
2323
use MongoDB\Laravel\Query\Processor;
2424
use PHPUnit\Framework\TestCase;
2525
use stdClass;
@@ -67,7 +67,7 @@ public function testAggregate(?array $expectedFind, ?array $expectedPipeline, Cl
6767
$this->assertInstanceOf(Builder::class, $builder);
6868
$encoder = new BuilderEncoder();
6969
$pipelineBuilder = $builder->aggregate();
70-
$this->assertInstanceOf(PipelineBuilder::class, $pipelineBuilder);
70+
$this->assertInstanceOf(AggregationBuilder::class, $pipelineBuilder);
7171
$pipeline = $encoder->encode($pipelineBuilder->getPipeline());
7272

7373
$expected = Document::fromPHP($expectedPipeline)->toCanonicalExtendedJSON();

0 commit comments

Comments
 (0)