diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Densify.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Densify.php index 5126c924ab..36c93778a1 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Densify.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Densify.php @@ -19,9 +19,9 @@ * '$densify': object{ * field: string, * partitionByFields?: list<string>, - * range?: object{ - * bounds: BoundsType, - * step: int|float, + * range: object{ + * bounds?: BoundsType, + * step?: int|float, * unit?: UnitType * } * } @@ -34,13 +34,14 @@ class Densify extends Stage /** @var array<string> */ private array $partitionByFields = []; - private ?object $range = null; + private object $range; public function __construct(Builder $builder, string $fieldName) { parent::__construct($builder); $this->field = $fieldName; + $this->range = (object) []; } public function partitionByFields(string ...$fields): self @@ -73,16 +74,15 @@ public function range($bounds, $step, string $unit = ''): self /** @psalm-return DensifyStageExpression */ public function getExpression(): array { - $params = (object) ['field' => $this->field]; + $params = (object) [ + 'field' => $this->field, + 'range' => $this->range, + ]; if ($this->partitionByFields) { $params->partitionByFields = $this->partitionByFields; } - if ($this->range) { - $params->range = $this->range; - } - return ['$densify' => $params]; } } diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill.php index 7fd6fccaef..8a594be506 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill.php @@ -26,7 +26,7 @@ * partitionBy?: string|OperatorExpression, * partitionByFields?: list<string>, * sortBy?: SortShape, - * output?: array, + * output: array, * } * } */ @@ -41,11 +41,13 @@ class Fill extends Stage /** @var array<string, int> */ private array $sortBy = []; - private ?Output $output = null; + private Output $output; public function __construct(Builder $builder) { parent::__construct($builder); + + $this->output = new Output($this->builder, $this); } /** @param mixed|Expr $expression */ @@ -86,16 +88,14 @@ public function sortBy($fieldName, $order = null): self public function output(): Output { - if (! $this->output) { - $this->output = new Output($this->builder, $this); - } - return $this->output; } public function getExpression(): array { - $params = (object) []; + $params = (object) [ + 'output' => (object) $this->output->getExpression(), + ]; if ($this->partitionBy) { $params->partitionBy = $this->partitionBy instanceof Expr @@ -111,10 +111,6 @@ public function getExpression(): array $params->sortBy = (object) $this->sortBy; } - if ($this->output) { - $params->output = (object) $this->output->getExpression(); - } - return ['$fill' => $params]; } } diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill/Output.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill/Output.php index f1a4bc38b8..3591672759 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill/Output.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill/Output.php @@ -6,6 +6,7 @@ use Doctrine\ODM\MongoDB\Aggregation\Builder; use Doctrine\ODM\MongoDB\Aggregation\Expr; +use Doctrine\ODM\MongoDB\Aggregation\Stage; use Doctrine\ODM\MongoDB\Aggregation\Stage\Fill; use LogicException; @@ -17,7 +18,7 @@ * * @psalm-import-type SortShape from Fill */ -class Output extends Fill +class Output extends Stage { private Fill $fill; diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/DensifyTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/DensifyTest.php index 84132c1998..7e581859df 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/DensifyTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/DensifyTest.php @@ -82,8 +82,24 @@ public function testStageWithRangeUnit(): void public function testFromBuilder(): void { $builder = $this->getTestAggregationBuilder(); - $builder->densify('someField'); + $builder + ->densify('someField') + ->range('full', 1, 'minute'); - self::assertEquals([['$densify' => (object) ['field' => 'someField']]], $builder->getPipeline()); + self::assertEquals( + [ + [ + '$densify' => (object) [ + 'field' => 'someField', + 'range' => (object) [ + 'bounds' => 'full', + 'step' => 1, + 'unit' => 'minute', + ], + ], + ], + ], + $builder->getPipeline(), + ); } }