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 3596a4c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 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];
}
}
3 changes: 2 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Fill/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,7 +18,7 @@
*
* @psalm-import-type SortShape from Fill
*/
class Output extends Fill
class Output extends Stage
{
private Fill $fill;

Expand Down
20 changes: 18 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/DensifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
}

0 comments on commit 3596a4c

Please sign in to comment.