From 385d34436c92eac299d9ef20fc18bbb4d1ff1ed0 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Fri, 24 Mar 2023 11:24:11 +0100 Subject: [PATCH] Improve handling of required options as typed arguments --- .../ODM/MongoDB/Aggregation/Stage/Densify.php | 18 +++++++++--------- .../ODM/MongoDB/Aggregation/Stage/Fill.php | 18 +++++++----------- 2 files changed, 16 insertions(+), 20 deletions(-) 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, - * 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 */ 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, * sortBy?: SortShape, - * output?: array, + * output: array, * } * } */ @@ -41,11 +41,13 @@ class Fill extends Stage /** @var array */ 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]; } }