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

Commit a4eff1d

Browse files
committed
PHPLIB-1269 Update $sort stage to accept variadic object
1 parent 387d38f commit a4eff1d

22 files changed

+66
-89
lines changed

generator/config/query/text.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ tests:
102102
$text:
103103
$search: 'CAFÉ'
104104
$diacriticSensitive: true
105+
-
106+
$project:
105107
score:
106108
$meta: 'textScore'
107109
-

generator/config/stage/sort.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ arguments:
1010
-
1111
name: sort
1212
type:
13-
- object # SortSpec
13+
- expression
14+
variadic: object
1415
tests:
1516
-
1617
name: 'Ascending Descending Sort'

src/Builder/Stage/FactoryTrait.php

Lines changed: 5 additions & 3 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: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Builder/Accumulator/FirstAccumulatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class FirstAccumulatorTest extends PipelineTestCase
2020
public function testUseInGroupStage(): void
2121
{
2222
$pipeline = new Pipeline(
23-
Stage::sort(object(
23+
Stage::sort(
2424
item: 1,
2525
date: 1,
26-
)),
26+
),
2727
Stage::group(
2828
_id: Expression::fieldPath('item'),
2929
firstSale: Accumulator::first(Expression::dateFieldPath('date')),

tests/Builder/Accumulator/FirstNAccumulatorTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ public function testUsingSortWithFirstN(): void
106106
{
107107
$pipeline = new Pipeline(
108108
Stage::sort(
109-
object(
110-
score: -1,
111-
),
109+
score: -1,
112110
),
113111
Stage::group(
114112
_id: Expression::fieldPath('gameId'),

tests/Builder/Accumulator/LastAccumulatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class LastAccumulatorTest extends PipelineTestCase
2020
public function testUseInGroupStage(): void
2121
{
2222
$pipeline = new Pipeline(
23-
Stage::sort(object(
23+
Stage::sort(
2424
item: 1,
2525
date: 1,
26-
)),
26+
),
2727
Stage::group(
2828
_id: '$item',
2929
lastSalesDate: Accumulator::last(Expression::dateFieldPath('date')),

tests/Builder/Accumulator/LastNAccumulatorTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use MongoDB\Builder\Stage;
1111
use MongoDB\Tests\Builder\PipelineTestCase;
1212

13-
use function MongoDB\object;
14-
1513
/**
1614
* Test $lastN accumulator
1715
*/
@@ -81,9 +79,9 @@ public function testFindingTheLastThreePlayerScoresAcrossMultipleGames(): void
8179
public function testUsingSortWithLastN(): void
8280
{
8381
$pipeline = new Pipeline(
84-
Stage::sort(object(
82+
Stage::sort(
8583
score: -1,
86-
)),
84+
),
8785
Stage::group(
8886
_id: Expression::fieldPath('gameId'),
8987
playerId: Accumulator::lastN(

tests/Builder/Accumulator/PushAccumulatorTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ public function testUseInGroupStage(): void
2121
{
2222
$pipeline = new Pipeline(
2323
Stage::sort(
24-
object(
25-
date: 1,
26-
item: 1,
27-
),
24+
date: 1,
25+
item: 1,
2826
),
2927
Stage::group(
3028
_id: object(

tests/Builder/BuilderEncoderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function testMatchNumericFieldName(): void
9898
public function testSort(): void
9999
{
100100
$pipeline = new Pipeline(
101-
Stage::sort(object(age: -1, posts: 1)),
101+
Stage::sort(age: -1, posts: 1),
102102
);
103103

104104
$expected = [

tests/Builder/Expression/BsonSizeOperatorTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use MongoDB\Builder\Stage;
1111
use MongoDB\Tests\Builder\PipelineTestCase;
1212

13-
use function MongoDB\object;
14-
1513
/**
1614
* Test $bsonSize expression
1715
*/
@@ -43,7 +41,7 @@ public function testReturnDocumentWithLargestSpecifiedField(): void
4341
),
4442
),
4543
Stage::sort(
46-
object(task_object_size: -1),
44+
task_object_size: -1,
4745
),
4846
Stage::limit(1),
4947
);

tests/Builder/Expression/SplitOperatorTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public function testExample(): void
4343
),
4444
),
4545
Stage::sort(
46-
object(
47-
total_qty: -1,
48-
),
46+
total_qty: -1,
4947
),
5048
);
5149

tests/Builder/Expression/ToDateOperatorTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
use MongoDB\Builder\Stage;
1010
use MongoDB\Tests\Builder\PipelineTestCase;
1111

12-
use function MongoDB\object;
13-
1412
/**
1513
* Test $toDate expression
1614
*/
@@ -25,9 +23,7 @@ public function testExample(): void
2523
),
2624
),
2725
Stage::sort(
28-
object(
29-
convertedDate: 1,
30-
),
26+
convertedDate: 1,
3127
),
3228
);
3329

tests/Builder/Expression/ToLongOperatorTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
use MongoDB\Builder\Stage;
1010
use MongoDB\Tests\Builder\PipelineTestCase;
1111

12-
use function MongoDB\object;
13-
1412
/**
1513
* Test $toLong expression
1614
*/
@@ -25,9 +23,7 @@ public function testExample(): void
2523
),
2624
),
2725
Stage::sort(
28-
object(
29-
convertedQty: -1,
30-
),
26+
convertedQty: -1,
3127
),
3228
);
3329

tests/Builder/Expression/ToObjectIdOperatorTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
use MongoDB\Builder\Stage;
1010
use MongoDB\Tests\Builder\PipelineTestCase;
1111

12-
use function MongoDB\object;
13-
1412
/**
1513
* Test $toObjectId expression
1614
*/
@@ -25,9 +23,7 @@ public function testExample(): void
2523
),
2624
),
2725
Stage::sort(
28-
object(
29-
convertedId: -1,
30-
),
26+
convertedId: -1,
3127
),
3228
);
3329

tests/Builder/Expression/ToStringOperatorTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
use MongoDB\Builder\Stage;
1010
use MongoDB\Tests\Builder\PipelineTestCase;
1111

12-
use function MongoDB\object;
13-
1412
/**
1513
* Test $toString expression
1614
*/
@@ -25,9 +23,7 @@ public function testExample(): void
2523
),
2624
),
2725
Stage::sort(
28-
object(
29-
convertedZipCode: 1,
30-
),
26+
convertedZipCode: 1,
3127
),
3228
);
3329

tests/Builder/Query/Pipelines.php

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

tests/Builder/Query/TextOperatorTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MongoDB\Tests\Builder\Query;
66

7+
use MongoDB\Builder\Expression;
78
use MongoDB\Builder\Pipeline;
89
use MongoDB\Builder\Query;
910
use MongoDB\Builder\Stage;
@@ -97,20 +98,18 @@ public function testSearchForASingleWord(): void
9798

9899
public function testTextSearchScoreExamples(): void
99100
{
100-
/**
101-
* @todo: add support for $meta: "textScore"
102-
* @todo: add support for $sort spec with object
103-
*/
104101
$pipeline = new Pipeline(
105102
Stage::match(
106103
Query::text(
107104
search: 'CAFÉ',
108105
diacriticSensitive: true,
109106
),
110-
score: ['$meta' => 'textScore'],
107+
),
108+
Stage::project(
109+
score: Expression::meta('textScore'),
111110
),
112111
Stage::sort(
113-
['score' => ['$meta' => 'textScore']],
112+
score: Expression::meta('textScore'),
114113
),
115114
Stage::limit(5),
116115
);

tests/Builder/Stage/GroupStageTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
use MongoDB\Builder\Stage;
1414
use MongoDB\Tests\Builder\PipelineTestCase;
1515

16-
use function MongoDB\object;
17-
1816
/**
1917
* Test $group stage
2018
*/
@@ -43,9 +41,7 @@ public function testCalculateCountSumAndAverage(): void
4341
count: Accumulator::sum(1),
4442
),
4543
Stage::sort(
46-
object(
47-
totalSaleAmount: -1,
48-
),
44+
totalSaleAmount: -1,
4945
),
5046
);
5147

tests/Builder/Stage/SortStageTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
namespace MongoDB\Tests\Builder\Stage;
66

7+
use MongoDB\Builder\Expression;
78
use MongoDB\Builder\Pipeline;
89
use MongoDB\Builder\Query;
910
use MongoDB\Builder\Stage;
1011
use MongoDB\Tests\Builder\PipelineTestCase;
1112

12-
use function MongoDB\object;
13-
1413
/**
1514
* Test $sort stage
1615
*/
@@ -20,10 +19,8 @@ public function testAscendingDescendingSort(): void
2019
{
2120
$pipeline = new Pipeline(
2221
Stage::sort(
23-
object(
24-
age: -1,
25-
posts: 1,
26-
),
22+
age: -1,
23+
posts: 1,
2724
),
2825
);
2926

@@ -37,10 +34,8 @@ public function testTextScoreMetadataSort(): void
3734
Query::text('operating'),
3835
),
3936
Stage::sort(
40-
object(
41-
score: ['$meta' => 'textScore'],
42-
posts: -1,
43-
),
37+
score: Expression::meta('textScore'),
38+
posts: -1,
4439
),
4540
);
4641

0 commit comments

Comments
 (0)