Skip to content

Commit da116c7

Browse files
+ added SetField stage
+ added tests for SetField stage
1 parent eb60a6f commit da116c7

File tree

5 files changed

+140
-6
lines changed

5 files changed

+140
-6
lines changed

Diff for: lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php

+16
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,22 @@ public function sample(int $size): Stage\Sample
520520
return $stage;
521521
}
522522

523+
/**
524+
* Adds, updates, or removes a specified field in a document.
525+
*
526+
* You can use $setField to add, update, or remove fields with names that contain periods (.) or start with
527+
* dollar signs ($).
528+
*
529+
* @see https://www.mongodb.com/docs/v5.0/reference/operator/aggregation/setField/#-setfield--aggregation-
530+
*/
531+
public function setField(): Stage\SetField
532+
{
533+
$stage = new Stage\SetField($this);
534+
$this->addStage($stage);
535+
536+
return $stage;
537+
}
538+
523539
/**
524540
* Skips over the specified number of documents that pass into the stage and
525541
* passes the remaining documents to the next stage in the pipeline.
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ODM\MongoDB\Aggregation\Stage;
6+
7+
/**
8+
* Fluent interface for adding a $setField stage to an aggregation pipeline.
9+
*/
10+
final class SetField extends Operator
11+
{
12+
private ?string $field = null;
13+
14+
private ?string $input = null;
15+
16+
private ?string $value = null;
17+
18+
public function getExpression(): array
19+
{
20+
return [
21+
'$setField' => [
22+
'field' => $this->field,
23+
'input' => $this->input,
24+
'value' => $this->value,
25+
],
26+
];
27+
}
28+
29+
/**
30+
* Field in the input object that you want to add, update, or remove.
31+
* Field can be any valid expression that resolves to a string constant.
32+
*/
33+
public function field(string $field): self
34+
{
35+
$this->field = $field;
36+
37+
return $this;
38+
}
39+
40+
/**
41+
* Document that will contain the field that you want to add or update.
42+
* Input must resolve to an object, missing, null, or undefined.
43+
*
44+
* Use $$ROOT to refer at the root document
45+
*/
46+
public function input(string $input): self
47+
{
48+
$this->input = $input;
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* The value that you want to assign to field. Value can be any valid expression.
55+
* Set to $$REMOVE to remove field from the input document.
56+
*/
57+
public function value(string $value): self
58+
{
59+
$this->value = $value;
60+
61+
return $this;
62+
}
63+
}

Diff for: lib/Doctrine/ODM/MongoDB/Query/Builder.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,8 @@ public function equals($value): self
464464
* If fields have been selected for inclusion, only the "_id" field may be
465465
* excluded.
466466
*
467-
* @param string[]|string $fieldName,...
468-
* @param null|string|string[] $fieldName
469-
*
467+
* @param string[]|string $fieldName,...
468+
* @param string|string[]|null $fieldName
470469
* @psalm-param 'comments'|array{0: 'name', 1: 'issues'}|null $fieldName
471470
*/
472471
public function exclude($fieldName = null): self
@@ -1291,7 +1290,7 @@ public function returnNew(bool $bool = true): self
12911290
/**
12921291
* Set one or more fields to be included in the query projection.
12931292
*
1294-
* @param null|string|string[] $fieldName
1293+
* @param string|string[]|null $fieldName
12951294
*
12961295
* @return Builder
12971296
*/

Diff for: lib/Doctrine/ODM/MongoDB/Types/DateImmutableType.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DateTime;
88
use DateTimeImmutable;
99
use DateTimeInterface;
10+
use MongoDB\BSON\UTCDateTime;
1011
use RuntimeException;
1112

1213
use function get_class;
@@ -15,9 +16,9 @@
1516
class DateImmutableType extends DateType
1617
{
1718
/**
18-
* @return DateTimeImmutable
19+
* @param UTCDateTime|float $value
1920
*
20-
* @param \MongoDB\BSON\UTCDateTime|float $value
21+
* @return DateTimeImmutable
2122
*/
2223
public static function getDateTime($value): DateTimeInterface
2324
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ODM\MongoDB\Tests\Aggregation\Stage;
6+
7+
use Doctrine\ODM\MongoDB\Aggregation\Stage\SetField;
8+
use Doctrine\ODM\MongoDB\Tests\Aggregation\AggregationTestTrait;
9+
use Doctrine\ODM\MongoDB\Tests\BaseTest;
10+
11+
class SetFieldTest extends BaseTest
12+
{
13+
use AggregationTestTrait;
14+
15+
public function testSetFieldStage(): void
16+
{
17+
$setFieldStage = new SetField($this->getTestAggregationBuilder());
18+
$setFieldStage
19+
->field('hello')
20+
->input('$$ROOT')
21+
->value('world');
22+
23+
$expectedExpression = [
24+
'$setField' => [
25+
'field' => 'hello',
26+
'input' => '$$ROOT',
27+
'value' => 'world',
28+
]
29+
];
30+
31+
self::assertSame($expectedExpression, $setFieldStage->getExpression());
32+
}
33+
34+
public function testStageFromBuilder(): void
35+
{
36+
$builder = $this->getTestAggregationBuilder();
37+
$builder
38+
->setField()
39+
->field('hello')
40+
->input('$$ROOT')
41+
->value('world');
42+
43+
$expectedPipeline = [
44+
[
45+
'$setField' => [
46+
'field' => 'hello',
47+
'input' => '$$ROOT',
48+
'value' => 'world',
49+
]
50+
]
51+
];
52+
53+
self::assertSame($expectedPipeline, $builder->getPipeline());
54+
}
55+
}

0 commit comments

Comments
 (0)