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

Commit 49ceb39

Browse files
committed
PHPLIB-1367 Add enum for System variables
1 parent 63dd036 commit 49ceb39

19 files changed

+141
-256
lines changed

generator/config/expressions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@
125125
'returnType' => Type\Sort::class,
126126
'acceptedTypes' => [Type\Sort::class],
127127
],
128+
'redact' => [
129+
'returnType' => Type\Redact::class,
130+
'acceptedTypes' => [Type\Redact::class, ResolvesToString::class, ...$bsonTypes['string']],
131+
],
128132

129133
// @todo add enum values
130134
'Granularity' => [

src/Builder/Type/Redact.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Builder\Type;
6+
7+
use MongoDB\Builder\Expression\ResolvesToString;
8+
9+
enum Redact: string implements DictionaryInterface, ResolvesToString
10+
{
11+
/**
12+
* $redact returns the fields at the current document level, excluding embedded documents. To include embedded
13+
* documents and embedded documents within arrays, apply the $cond expression to the embedded documents to determine
14+
* access for these embedded documents.
15+
*/
16+
case Descend = 'DESCEND';
17+
18+
/**
19+
* $redact excludes all fields at this current document/embedded document level, without further inspection of any
20+
* of the excluded fields. This applies even if the excluded field contains embedded documents that may have
21+
* different access levels.
22+
*/
23+
case Prune = 'PRUNE';
24+
25+
/**
26+
* $redact returns or keeps all fields at this current document/embedded document level, without further inspection
27+
* of the fields at this level. This applies even if the included field contains embedded documents that may have
28+
* different access levels.
29+
*/
30+
case Keep = 'KEEP';
31+
32+
public function getValue(): string
33+
{
34+
return '$$' . $this->value;
35+
}
36+
}

src/Builder/Type/Variable.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Builder\Type;
6+
7+
use MongoDB\Builder\Expression\ResolvesToAny;
8+
use MongoDB\Builder\Expression\Variable as VariableExpression;
9+
10+
enum Variable: string implements DictionaryInterface, ResolvesToAny
11+
{
12+
/**
13+
* A variable that returns the current datetime value.
14+
*/
15+
case Now = 'NOW';
16+
17+
/**
18+
* A variable that returns the current timestamp value.
19+
*/
20+
case ClusterTime = 'CLUSTER_TIME';
21+
22+
/**
23+
* References the root document, i.e. the top-level document, currently being processed in the aggregation pipeline stage.
24+
*/
25+
case Root = 'ROOT';
26+
27+
/**
28+
* References the start of the field path being processed in the aggregation pipeline stage.
29+
* Use Variable::Current->dot('field') to reference $field
30+
*/
31+
case Current = 'CURRENT';
32+
33+
/**
34+
* A variable which evaluates to the missing value. Allows for the conditional exclusion of fields. In a $project, a field set to the variable REMOVE is excluded from the output.
35+
*/
36+
case Remove = 'REMOVE';
37+
38+
/**
39+
* A variable that stores the metadata results of an Atlas Search query. In all supported aggregation pipeline
40+
* stages, a field set to the variable $$SEARCH_META returns the metadata results for the query.
41+
*/
42+
case SearchMeta = 'SEARCH_META';
43+
44+
/**
45+
* Returns the roles assigned to the current user.
46+
*/
47+
case UserRoles = 'USER_ROLES';
48+
49+
public function dot(string $name): VariableExpression
50+
{
51+
return new VariableExpression($this->value . '.' . $name);
52+
}
53+
54+
public function getValue(): string
55+
{
56+
return '$$' . $this->value;
57+
}
58+
}

src/Builder/Variable.php

Lines changed: 0 additions & 158 deletions
This file was deleted.

tests/Builder/BuilderEncoderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use MongoDB\Builder\Pipeline;
1313
use MongoDB\Builder\Query;
1414
use MongoDB\Builder\Stage;
15+
use MongoDB\Builder\Type\Redact;
1516
use MongoDB\Builder\Type\Sort;
16-
use MongoDB\Builder\Variable;
1717
use PHPUnit\Framework\TestCase;
1818

1919
use function array_merge;
@@ -314,8 +314,8 @@ public function testRedactStage(): void
314314
Stage::redact(
315315
Expression::cond(
316316
if: Expression::eq(Expression::fieldPath('level'), 5),
317-
then: Variable::prune(),
318-
else: Variable::descend(),
317+
then: Redact::Prune,
318+
else: Redact::Descend,
319319
),
320320
),
321321
);

tests/Builder/Expression/BsonSizeOperatorTest.php

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

1415
/**
@@ -23,7 +24,7 @@ public function testReturnCombinedSizeOfAllDocumentsInACollection(): void
2324
_id: null,
2425
combined_object_size: Accumulator::sum(
2526
Expression::bsonSize(
26-
Expression::variable('ROOT'),
27+
Variable::Root,
2728
),
2829
),
2930
),
@@ -56,7 +57,7 @@ public function testReturnSizesOfDocuments(): void
5657
Stage::project(
5758
name: 1,
5859
object_size: Expression::bsonSize(
59-
Expression::variable('ROOT'),
60+
Variable::Root,
6061
),
6162
),
6263
);

tests/Builder/Expression/DateSubtractOperatorTest.php

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

1415
/**
@@ -83,7 +84,7 @@ public function testFilterByRelativeDates(): void
8384
Expression::gt(
8485
Expression::dateFieldPath('logoutTime'),
8586
Expression::dateSubtract(
86-
startDate: Expression::variable('NOW'),
87+
startDate: Variable::Now,
8788
unit: TimeUnit::Week,
8889
amount: 1,
8990
),

tests/Builder/Expression/SetFieldOperatorTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MongoDB\Builder\Expression;
88
use MongoDB\Builder\Pipeline;
99
use MongoDB\Builder\Stage;
10+
use MongoDB\Builder\Type\Variable;
1011
use MongoDB\Tests\Builder\PipelineTestCase;
1112

1213
/**
@@ -20,7 +21,7 @@ public function testAddFieldsThatContainPeriods(): void
2021
Stage::replaceWith(
2122
Expression::setField(
2223
field: 'price.usd',
23-
input: Expression::variable('ROOT'),
24+
input: Variable::Root,
2425
value: Expression::fieldPath('price'),
2526
),
2627
),
@@ -36,7 +37,7 @@ public function testAddFieldsThatStartWithADollarSign(): void
3637
Stage::replaceWith(
3738
Expression::setField(
3839
field: Expression::literal('$price'),
39-
input: Expression::variable('ROOT'),
40+
input: Variable::Root,
4041
value: Expression::fieldPath('price'),
4142
),
4243
),
@@ -52,8 +53,8 @@ public function testRemoveFieldsThatContainPeriods(): void
5253
Stage::replaceWith(
5354
Expression::setField(
5455
field: 'price.usd',
55-
input: Expression::variable('ROOT'),
56-
value: Expression::variable('REMOVE'),
56+
input: Variable::Root,
57+
value: Variable::Remove,
5758
),
5859
),
5960
);
@@ -67,8 +68,8 @@ public function testRemoveFieldsThatStartWithADollarSign(): void
6768
Stage::replaceWith(
6869
Expression::setField(
6970
field: Expression::literal('$price'),
70-
input: Expression::variable('ROOT'),
71-
value: Expression::variable('REMOVE'),
71+
input: Variable::Root,
72+
value: Variable::Remove,
7273
),
7374
),
7475
);
@@ -85,7 +86,7 @@ public function testUpdateFieldsThatContainPeriods(): void
8586
Stage::replaceWith(
8687
Expression::setField(
8788
field: 'price.usd',
88-
input: Expression::variable('ROOT'),
89+
input: Variable::Root,
8990
value: 49.99,
9091
),
9192
),
@@ -103,7 +104,7 @@ public function testUpdateFieldsThatStartWithADollarSign(): void
103104
Stage::replaceWith(
104105
Expression::setField(
105106
field: Expression::literal('$price'),
106-
input: Expression::variable('ROOT'),
107+
input: Variable::Root,
107108
value: 49.99,
108109
),
109110
),

0 commit comments

Comments
 (0)