Skip to content

Commit

Permalink
Rename AggregationBuilder class
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Mar 11, 2024
1 parent bbfd062 commit 0e82e24
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use function array_replace;
use function collect;

final class PipelineBuilder extends FluentFactory
final class AggregationBuilder extends FluentFactory
{
public function __construct(
array $pipeline,
Expand Down
22 changes: 11 additions & 11 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,40 +282,40 @@ public function dump(mixed ...$args)
return $this;
}

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

$wheres = $this->compileWheres();

if (count($wheres)) {
$pipelineBuilder->match(...$wheres);
$agg->match(...$wheres);
}

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

Check failure on line 298 in src/Query/Builder.php

View workflow job for this annotation

GitHub Actions / analysis (8.1)

Variable $columns on left side of ?? is never defined.

Check failure on line 298 in src/Query/Builder.php

View workflow job for this annotation

GitHub Actions / analysis (8.2)

Variable $columns on left side of ?? is never defined.

$pipelineBuilder->group(
$agg->group(
_id: \MongoDB\Builder\Expression::fieldPath($column),
_document: Accumulator::first(Variable::root()),
);
$pipelineBuilder->replaceRoot(
$agg->replaceRoot(
newRoot: new FieldPath('_document'),
);
}

if ($this->orders) {
$pipelineBuilder->sort(...$this->orders);
$agg->sort(...$this->orders);
}

if ($this->offset) {
$pipelineBuilder->skip($this->offset);
$agg->skip($this->offset);
}

if ($this->limit) {
$pipelineBuilder->limit($this->limit);
$agg->limit($this->limit);
}

// Normal query
Expand All @@ -324,11 +324,11 @@ private function getPipelineBuilder(): PipelineBuilder
$columns = in_array('*', $this->columns) ? [] : $this->columns;
$projection = array_fill_keys($columns, true) + $this->projections;
if ($projection) {
$pipelineBuilder->project(...$projection);
$agg->project(...$projection);
}
}

return $pipelineBuilder;
return $agg;
}

/**
Expand Down Expand Up @@ -594,7 +594,7 @@ public function generateCacheKey()
public function aggregate($function = null, $columns = [])

Check failure on line 594 in src/Query/Builder.php

View workflow job for this annotation

GitHub Actions / analysis (8.1)

PHPDoc tag @return has invalid value (($function === null ? PipelineBuilder : self)): Unexpected token "=", expected type ("is") at offset 23

Check failure on line 594 in src/Query/Builder.php

View workflow job for this annotation

GitHub Actions / analysis (8.2)

PHPDoc tag @return has invalid value (($function === null ? PipelineBuilder : self)): Unexpected token "=", expected type ("is") at offset 23
{
if ($function === null) {
return $this->getPipelineBuilder();
return $this->getAggregationBuilder();
}

$this->aggregate = [
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected function username(): Attribute
{
return Attribute::make(
get: fn ($value) => $value,
set: fn ($value) => Str::slug($value)
set: fn ($value) => Str::slug($value),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
use MongoDB\Builder\Type\Sort;
use MongoDB\Builder\Type\TimeUnit;
use MongoDB\Builder\Variable;
use MongoDB\Laravel\Query\PipelineBuilder;
use MongoDB\Laravel\Query\AggregationBuilder;
use MongoDB\Laravel\Tests\Models\User;
use MongoDB\Laravel\Tests\TestCase;

use function assert;

class PipelineBuilderTest extends TestCase
class AggregationBuilderTest extends TestCase
{
public function tearDown(): void
{
Expand All @@ -35,8 +35,11 @@ public function testCreateFromQueryBuilder(): void
]);

// Create the aggregation pipeline from the query builder
$pipeline = User::where('name', 'John Doe')->aggregate();
assert($pipeline instanceof PipelineBuilder);
$pipeline = User::where('name', 'John Doe')
->limit(10)
->offset(0)
->aggregate();
assert($pipeline instanceof AggregationBuilder);
$pipeline
->addFields(
age: Expression::dateDiff(
Expand All @@ -57,9 +60,8 @@ public function testCreateFromQueryBuilder(): void
// Compare with the expected pipeline
$expected = Document::fromPHP([
'pipeline' => [
[
'$match' => ['name' => 'John Doe'],
],
['$match' => ['name' => 'John Doe']],
['$limit' => 10],
[
'$addFields' => [
'age' => [
Expand All @@ -71,12 +73,8 @@ public function testCreateFromQueryBuilder(): void
],
],
],
[
'$sort' => ['age' => -1, 'name' => 1],
],
[
'$unset' => ['birthday'],
],
['$sort' => ['age' => -1, 'name' => 1]],
['$unset' => ['birthday']],
],
])->toCanonicalExtendedJSON();

Expand Down
4 changes: 2 additions & 2 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use MongoDB\BSON\UTCDateTime;
use MongoDB\Builder\BuilderEncoder;
use MongoDB\Laravel\Connection;
use MongoDB\Laravel\Query\AggregationBuilder;
use MongoDB\Laravel\Query\Builder;
use MongoDB\Laravel\Query\Grammar;
use MongoDB\Laravel\Query\PipelineBuilder;
use MongoDB\Laravel\Query\Processor;
use PHPUnit\Framework\TestCase;
use stdClass;
Expand Down Expand Up @@ -67,7 +67,7 @@ public function testAggregate(?array $expectedFind, ?array $expectedPipeline, Cl
$this->assertInstanceOf(Builder::class, $builder);
$encoder = new BuilderEncoder();
$pipelineBuilder = $builder->aggregate();
$this->assertInstanceOf(PipelineBuilder::class, $pipelineBuilder);
$this->assertInstanceOf(AggregationBuilder::class, $pipelineBuilder);
$pipeline = $encoder->encode($pipelineBuilder->getPipeline());

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

0 comments on commit 0e82e24

Please sign in to comment.