Skip to content

Commit

Permalink
Improve handling of required options as typed arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Mar 24, 2023
1 parent 67847c5 commit 385d344
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
18 changes: 9 additions & 9 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Densify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
* }
* }
Expand All @@ -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
Expand Down Expand Up @@ -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];
}
}
18 changes: 7 additions & 11 deletions lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* partitionBy?: string|OperatorExpression,
* partitionByFields?: list<string>,
* sortBy?: SortShape,
* output?: array,
* output: array,
* }
* }
*/
Expand All @@ -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 */
Expand Down Expand Up @@ -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
Expand All @@ -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];
}
}

0 comments on commit 385d344

Please sign in to comment.