Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 0dc0974

Browse files
committed
Add Sort enum for sort order and meta
1 parent 9718c65 commit 0dc0974

File tree

58 files changed

+182
-102
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+182
-102
lines changed

generator/config/expression/sortArray.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ arguments:
2020
type:
2121
- object # SortSpec
2222
- int
23+
- sortSpec
2324
description: |
2425
The document specifies a sort ordering.
2526
tests:

generator/config/expressions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@
117117
'returnType' => Type\TimeUnit::class,
118118
'acceptedTypes' => [Type\TimeUnit::class, ResolvesToString::class, ...$bsonTypes['string']],
119119
],
120+
'sortSpec' => [
121+
'returnType' => Type\Sort::class,
122+
'acceptedTypes' => [Type\Sort::class],
123+
],
120124

121125
// @todo add enum values
122126
'Granularity' => [

generator/config/schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
"geometry",
115115
"fieldPath",
116116
"timeUnit",
117+
"sortSpec",
117118
"any",
118119
"resolvesToNumber", "numberFieldPath", "number",
119120
"resolvesToDouble", "doubleFieldPath", "double",

generator/config/stage/sort.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ arguments:
1111
name: sort
1212
type:
1313
- expression
14+
- sortSpec
1415
variadic: object
1516
tests:
1617
-

src/Builder/BuilderEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function canEncode(mixed $value): bool
6767
return (bool) $this->getEncoderFor($value)?->canEncode($value);
6868
}
6969

70-
public function encode(mixed $value): stdClass|array|string
70+
public function encode(mixed $value): stdClass|array|string|int
7171
{
7272
$encoder = $this->getEncoderFor($value);
7373

src/Builder/Encoder/AbstractExpressionEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use function is_array;
1212

1313
/**
14-
* @template BSONType of stdClass|array|string
14+
* @template BSONType of stdClass|array|string|int
1515
* @template NativeType
1616
* @template-implements ExpressionEncoder<BSONType, NativeType>
1717
*/

src/Builder/Encoder/ExpressionEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use stdClass;
1010

1111
/**
12-
* @template BSONType of stdClass|array|string
12+
* @template BSONType of stdClass|array|string|int
1313
* @template NativeType
1414
* @template-extends Encoder<BSONType, NativeType>
1515
*/

src/Builder/Expression/FactoryTrait.php

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Expression/SortArrayOperator.php

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Stage/FactoryTrait.php

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Stage/SortStage.php

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Type/Dictionnary.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace MongoDB\Builder\Type;
66

7-
use stdClass;
8-
97
interface Dictionnary
108
{
11-
public function getValue(): string|int|array|stdClass;
9+
public function getValue(): string|int|array;
1210
}

src/Builder/Type/Sort.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Builder\Type;
6+
7+
enum Sort implements Dictionnary
8+
{
9+
case Asc;
10+
case Desc;
11+
case TextScore;
12+
case SearchScoreAsc;
13+
case SearchScoreDesc;
14+
15+
public function getValue(): int|array
16+
{
17+
return match ($this) {
18+
self::Asc => 1,
19+
self::Desc => -1,
20+
self::TextScore => ['$meta' => 'textScore'],
21+
self::SearchScoreAsc => ['$meta' => 'searchScore', 'order' => 1],
22+
self::SearchScoreDesc => ['$meta' => 'searchScore', 'order' => -1],
23+
};
24+
}
25+
}

tests/Builder/Accumulator/AddToSetAccumulatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Builder\Expression;
99
use MongoDB\Builder\Pipeline;
1010
use MongoDB\Builder\Stage;
11+
use MongoDB\Builder\Type\Sort;
1112
use MongoDB\Tests\Builder\PipelineTestCase;
1213

1314
use function MongoDB\object;
@@ -38,7 +39,7 @@ public function testUseInSetWindowFieldsStage(): void
3839
Stage::setWindowFields(
3940
partitionBy: Expression::fieldPath('state'),
4041
sortBy: object(
41-
orderDate: 1,
42+
orderDate: Sort::Asc,
4243
),
4344
output: object(
4445
cakeTypesForState: Accumulator::outputWindow(

tests/Builder/Accumulator/AvgAccumulatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Builder\Expression;
99
use MongoDB\Builder\Pipeline;
1010
use MongoDB\Builder\Stage;
11+
use MongoDB\Builder\Type\Sort;
1112
use MongoDB\Tests\Builder\PipelineTestCase;
1213

1314
use function MongoDB\object;
@@ -43,7 +44,7 @@ public function testUseInSetWindowFieldsStage(): void
4344
Stage::setWindowFields(
4445
partitionBy: Expression::fieldPath('state'),
4546
sortBy: object(
46-
orderDate: 1,
47+
orderDate: Sort::Asc,
4748
),
4849
output: object(
4950
averageQuantityForState: Accumulator::outputWindow(

tests/Builder/Accumulator/BottomAccumulatorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Builder\Expression;
99
use MongoDB\Builder\Pipeline;
1010
use MongoDB\Builder\Stage;
11+
use MongoDB\Builder\Type\Sort;
1112
use MongoDB\Tests\Builder\PipelineTestCase;
1213

1314
use function MongoDB\object;
@@ -31,7 +32,7 @@ public function testFindTheBottomScore(): void
3132
Expression::fieldPath('score'),
3233
],
3334
sortBy: object(
34-
score: -1,
35+
score: Sort::Desc,
3536
),
3637
),
3738
),
@@ -51,7 +52,7 @@ public function testFindingTheBottomScoreAcrossMultipleGames(): void
5152
Expression::fieldPath('score'),
5253
],
5354
sortBy: object(
54-
score: -1,
55+
score: Sort::Desc,
5556
),
5657
),
5758
),

tests/Builder/Accumulator/BottomNAccumulatorTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Builder\Expression;
99
use MongoDB\Builder\Pipeline;
1010
use MongoDB\Builder\Stage;
11+
use MongoDB\Builder\Type\Sort;
1112
use MongoDB\Tests\Builder\PipelineTestCase;
1213

1314
use function MongoDB\object;
@@ -35,7 +36,7 @@ public function testComputingNBasedOnTheGroupKeyForGroup(): void
3536
else: 3,
3637
),
3738
sortBy: object(
38-
score: -1,
39+
score: Sort::Desc,
3940
),
4041
),
4142
),
@@ -58,7 +59,7 @@ public function testFindTheThreeLowestScores(): void
5859
Expression::fieldPath('score'),
5960
],
6061
sortBy: object(
61-
score: -1,
62+
score: Sort::Desc,
6263
),
6364
n: 3,
6465
),
@@ -79,7 +80,7 @@ public function testFindingTheThreeLowestScoreDocumentsAcrossMultipleGames(): vo
7980
Expression::fieldPath('score'),
8081
],
8182
sortBy: object(
82-
score: -1,
83+
score: Sort::Desc,
8384
),
8485
n: 3,
8586
),

tests/Builder/Accumulator/CountAccumulatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Builder\Expression;
99
use MongoDB\Builder\Pipeline;
1010
use MongoDB\Builder\Stage;
11+
use MongoDB\Builder\Type\Sort;
1112
use MongoDB\Tests\Builder\PipelineTestCase;
1213

1314
use function MongoDB\object;
@@ -35,7 +36,7 @@ public function testUseInSetWindowFieldsStage(): void
3536
Stage::setWindowFields(
3637
partitionBy: Expression::fieldPath('state'),
3738
sortBy: object(
38-
orderDate: 1,
39+
orderDate: Sort::Asc,
3940
),
4041
output: object(
4142
countNumberOfDocumentsForState: Accumulator::outputWindow(

tests/Builder/Accumulator/CovariancePopAccumulatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Builder\Expression;
99
use MongoDB\Builder\Pipeline;
1010
use MongoDB\Builder\Stage;
11+
use MongoDB\Builder\Type\Sort;
1112
use MongoDB\Tests\Builder\PipelineTestCase;
1213

1314
use function MongoDB\object;
@@ -23,7 +24,7 @@ public function testExample(): void
2324
Stage::setWindowFields(
2425
partitionBy: Expression::stringFieldPath('state'),
2526
sortBy: object(
26-
orderDate: 1,
27+
orderDate: Sort::Asc,
2728
),
2829
output: object(
2930
covariancePopForState: Accumulator::outputWindow(

tests/Builder/Accumulator/CovarianceSampAccumulatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Builder\Expression;
99
use MongoDB\Builder\Pipeline;
1010
use MongoDB\Builder\Stage;
11+
use MongoDB\Builder\Type\Sort;
1112
use MongoDB\Tests\Builder\PipelineTestCase;
1213

1314
use function MongoDB\object;
@@ -23,7 +24,7 @@ public function testExample(): void
2324
Stage::setWindowFields(
2425
partitionBy: Expression::stringFieldPath('state'),
2526
sortBy: object(
26-
orderDate: 1,
27+
orderDate: Sort::Asc,
2728
),
2829
output: object(
2930
covarianceSampForState: Accumulator::outputWindow(

tests/Builder/Accumulator/DenseRankAccumulatorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Builder\Expression;
99
use MongoDB\Builder\Pipeline;
1010
use MongoDB\Builder\Stage;
11+
use MongoDB\Builder\Type\Sort;
1112
use MongoDB\Tests\Builder\PipelineTestCase;
1213

1314
use function MongoDB\object;
@@ -23,7 +24,7 @@ public function testDenseRankPartitionsByADateField(): void
2324
Stage::setWindowFields(
2425
partitionBy: Expression::stringFieldPath('state'),
2526
sortBy: object(
26-
orderDate: 1,
27+
orderDate: Sort::Asc,
2728
),
2829
output: object(
2930
denseRankOrderDateForState: Accumulator::outputWindow(
@@ -42,7 +43,7 @@ public function testDenseRankPartitionsByAnIntegerField(): void
4243
Stage::setWindowFields(
4344
partitionBy: Expression::stringFieldPath('state'),
4445
sortBy: object(
45-
quantity: -1,
46+
quantity: Sort::Desc,
4647
),
4748
output: object(
4849
// The outputWindow is optional when no window property is set.

tests/Builder/Accumulator/DerivativeAccumulatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use MongoDB\Builder\Pipeline;
1010
use MongoDB\Builder\Query;
1111
use MongoDB\Builder\Stage;
12+
use MongoDB\Builder\Type\Sort;
1213
use MongoDB\Builder\Type\TimeUnit;
1314
use MongoDB\Tests\Builder\PipelineTestCase;
1415

@@ -25,7 +26,7 @@ public function testExample(): void
2526
Stage::setWindowFields(
2627
partitionBy: Expression::stringFieldPath('truckID'),
2728
sortBy: object(
28-
timeStamp: 1,
29+
timeStamp: Sort::Asc,
2930
),
3031
output: object(
3132
truckAverageSpeed: Accumulator::outputWindow(

0 commit comments

Comments
 (0)