Skip to content

Commit

Permalink
Partial implementation of
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 2, 2023
1 parent 0e80bb6 commit d4e53d4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 23 deletions.
3 changes: 2 additions & 1 deletion generator/config/stages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
type: stage
args:
- name: sortSpecification
type: resolvesToObject
type: number # should be an enum PHPLIB-1269
isVariadic: true
- name: limit
type: stage
args:
Expand Down
9 changes: 8 additions & 1 deletion generator/src/OperatorGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ final protected function generateExpressionTypes(ArgumentDefinition $arg): objec
$nativeTypes = [];
foreach ((array) $arg->type as $type) {
$interface = $this->getExpressionTypeInterface($type);
$nativeTypes = array_merge($nativeTypes, [$interface], $this->expressions[$interface]->types);
$types = $this->expressions[$interface]->types;

// Add the interface to the allowed types if it is not a scalar
if (! $this->expressions[$interface]->scalar) {
$types = array_merge([$interface], $types);
}

$nativeTypes = array_merge($nativeTypes, $types);
}

$docTypes = $nativeTypes = array_unique($nativeTypes);
Expand Down
5 changes: 5 additions & 0 deletions src/Builder/BuilderEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MongoDB\Builder\Query\OrQuery;
use MongoDB\Builder\Stage\GroupStage;
use MongoDB\Builder\Stage\ProjectStage;
use MongoDB\Builder\Stage\SortStage;
use MongoDB\Builder\Stage\StageInterface;
use MongoDB\Codec\EncodeIfSupported;
use MongoDB\Codec\Encoder;
Expand Down Expand Up @@ -83,6 +84,10 @@ public function encode($value): stdClass|array|string
return $this->wrap($value, $result);
}

if ($value instanceof SortStage) {
return $this->wrap($value, (object) $value->sortSpecification);
}

if ($value instanceof OrQuery) {
$result = [];
foreach ($value->query as $query) {
Expand Down
9 changes: 3 additions & 6 deletions src/Builder/Stage.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions src/Builder/Stage/SortStage.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @todo This annotation is not enough as this PHP file needs to use named arguments, that can't compile on PHP 7.4
* @requires PHP 8.0
*/
class BuilderCodecTest extends TestCase
class BuilderEncoderTest extends TestCase
{
/** @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/#equality-match */
public function testPipeline(): void
Expand All @@ -39,6 +39,20 @@ public function testPipeline(): void
$this->assertSamePipeline($expected, $pipeline);
}

/** @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort/#ascending-descending-sort */
public function testSort(): void
{
$pipeline = new Pipeline(
Stage::sort(...['age' => -1, 'posts' => 1]),
);

$expected = [
['$sort' => ['age' => -1, 'posts' => 1]],
];

$this->assertSamePipeline($expected, $pipeline);
}

/** @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/#perform-a-count */
public function testPerformCount(): void
{
Expand All @@ -47,10 +61,10 @@ public function testPerformCount(): void
['score' => [Query::gt(70), Query::lt(90)]],
['views' => Query::gte(1000)],
)),
Stage::group(
_id: null,
count: Aggregation::sum(1),
),
Stage::group(...[
'_id' => null,
'count' => Aggregation::sum(1),
]),
);

$expected = [
Expand Down Expand Up @@ -80,14 +94,14 @@ public function testPerformCount(): void
public function testAggregationFilter(array $limit, array $expectedLimit): void
{
$pipeline = new Pipeline(
Stage::project(
items: Aggregation::filter(
Stage::project(...[
'items' => Aggregation::filter(
Expression::arrayFieldPath('items'),
Aggregation::gte(Expression::variable('item.price'), 100),
'item',
...$limit,
),
),
]),
);

$expected = [
Expand Down

0 comments on commit d4e53d4

Please sign in to comment.