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

PHPLIB-1367 Add enum for System variables #64

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions generator/config/expressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@
'returnType' => Type\Sort::class,
'acceptedTypes' => [Type\Sort::class],
],
'redact' => [
'returnType' => Type\Redact::class,
'acceptedTypes' => [Type\Redact::class, ResolvesToString::class, ...$bsonTypes['string']],
],

// @todo add enum values
'Granularity' => [
Expand Down
36 changes: 36 additions & 0 deletions src/Builder/Type/Redact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace MongoDB\Builder\Type;

use MongoDB\Builder\Expression\ResolvesToString;

enum Redact: string implements DictionaryInterface, ResolvesToString
{
/**
* $redact returns the fields at the current document level, excluding embedded documents. To include embedded
* documents and embedded documents within arrays, apply the $cond expression to the embedded documents to determine
* access for these embedded documents.
*/
case Descend = 'DESCEND';

/**
* $redact excludes all fields at this current document/embedded document level, without further inspection of any
* of the excluded fields. This applies even if the excluded field contains embedded documents that may have
* different access levels.
*/
case Prune = 'PRUNE';

/**
* $redact returns or keeps all fields at this current document/embedded document level, without further inspection
* of the fields at this level. This applies even if the included field contains embedded documents that may have
* different access levels.
*/
case Keep = 'KEEP';

public function getValue(): string
{
return '$$' . $this->value;
}
}
58 changes: 58 additions & 0 deletions src/Builder/Type/Variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace MongoDB\Builder\Type;

use MongoDB\Builder\Expression\ResolvesToAny;
use MongoDB\Builder\Expression\Variable as VariableExpression;

enum Variable: string implements DictionaryInterface, ResolvesToAny
{
/**
* A variable that returns the current datetime value.
*/
case Now = 'NOW';

/**
* A variable that returns the current timestamp value.
*/
case ClusterTime = 'CLUSTER_TIME';

/**
* References the root document, i.e. the top-level document, currently being processed in the aggregation pipeline stage.
*/
case Root = 'ROOT';

/**
* References the start of the field path being processed in the aggregation pipeline stage.
* Use Variable::Current->dot('field') to reference $field
*/
case Current = 'CURRENT';

/**
* 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.
*/
case Remove = 'REMOVE';

/**
* A variable that stores the metadata results of an Atlas Search query. In all supported aggregation pipeline
* stages, a field set to the variable $$SEARCH_META returns the metadata results for the query.
*/
case SearchMeta = 'SEARCH_META';

/**
* Returns the roles assigned to the current user.
*/
case UserRoles = 'USER_ROLES';

public function dot(string $name): VariableExpression
{
return new VariableExpression($this->value . '.' . $name);
}

public function getValue(): string
{
return '$$' . $this->value;
}
}
158 changes: 0 additions & 158 deletions src/Builder/Variable.php

This file was deleted.

6 changes: 3 additions & 3 deletions tests/Builder/BuilderEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Query;
use MongoDB\Builder\Stage;
use MongoDB\Builder\Type\Redact;
use MongoDB\Builder\Type\Sort;
use MongoDB\Builder\Variable;
use PHPUnit\Framework\TestCase;

use function array_merge;
Expand Down Expand Up @@ -314,8 +314,8 @@ public function testRedactStage(): void
Stage::redact(
Expression::cond(
if: Expression::eq(Expression::fieldPath('level'), 5),
then: Variable::prune(),
else: Variable::descend(),
then: Redact::Prune,
else: Redact::Descend,
),
),
);
Expand Down
5 changes: 3 additions & 2 deletions tests/Builder/Expression/BsonSizeOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Stage;
use MongoDB\Builder\Type\Sort;
use MongoDB\Builder\Type\Variable;
use MongoDB\Tests\Builder\PipelineTestCase;

/**
Expand All @@ -23,7 +24,7 @@ public function testReturnCombinedSizeOfAllDocumentsInACollection(): void
_id: null,
combined_object_size: Accumulator::sum(
Expression::bsonSize(
Expression::variable('ROOT'),
Variable::Root,
),
),
),
Expand Down Expand Up @@ -56,7 +57,7 @@ public function testReturnSizesOfDocuments(): void
Stage::project(
name: 1,
object_size: Expression::bsonSize(
Expression::variable('ROOT'),
Variable::Root,
),
),
);
Expand Down
3 changes: 2 additions & 1 deletion tests/Builder/Expression/DateSubtractOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MongoDB\Builder\Query;
use MongoDB\Builder\Stage;
use MongoDB\Builder\Type\TimeUnit;
use MongoDB\Builder\Type\Variable;
use MongoDB\Tests\Builder\PipelineTestCase;

/**
Expand Down Expand Up @@ -83,7 +84,7 @@ public function testFilterByRelativeDates(): void
Expression::gt(
Expression::dateFieldPath('logoutTime'),
Expression::dateSubtract(
startDate: Expression::variable('NOW'),
startDate: Variable::Now,
unit: TimeUnit::Week,
amount: 1,
),
Expand Down
17 changes: 9 additions & 8 deletions tests/Builder/Expression/SetFieldOperatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MongoDB\Builder\Expression;
use MongoDB\Builder\Pipeline;
use MongoDB\Builder\Stage;
use MongoDB\Builder\Type\Variable;
use MongoDB\Tests\Builder\PipelineTestCase;

/**
Expand All @@ -20,7 +21,7 @@ public function testAddFieldsThatContainPeriods(): void
Stage::replaceWith(
Expression::setField(
field: 'price.usd',
input: Expression::variable('ROOT'),
input: Variable::Root,
value: Expression::fieldPath('price'),
),
),
Expand All @@ -36,7 +37,7 @@ public function testAddFieldsThatStartWithADollarSign(): void
Stage::replaceWith(
Expression::setField(
field: Expression::literal('$price'),
input: Expression::variable('ROOT'),
input: Variable::Root,
value: Expression::fieldPath('price'),
),
),
Expand All @@ -52,8 +53,8 @@ public function testRemoveFieldsThatContainPeriods(): void
Stage::replaceWith(
Expression::setField(
field: 'price.usd',
input: Expression::variable('ROOT'),
value: Expression::variable('REMOVE'),
input: Variable::Root,
value: Variable::Remove,
),
),
);
Expand All @@ -67,8 +68,8 @@ public function testRemoveFieldsThatStartWithADollarSign(): void
Stage::replaceWith(
Expression::setField(
field: Expression::literal('$price'),
input: Expression::variable('ROOT'),
value: Expression::variable('REMOVE'),
input: Variable::Root,
value: Variable::Remove,
),
),
);
Expand All @@ -85,7 +86,7 @@ public function testUpdateFieldsThatContainPeriods(): void
Stage::replaceWith(
Expression::setField(
field: 'price.usd',
input: Expression::variable('ROOT'),
input: Variable::Root,
value: 49.99,
),
),
Expand All @@ -103,7 +104,7 @@ public function testUpdateFieldsThatStartWithADollarSign(): void
Stage::replaceWith(
Expression::setField(
field: Expression::literal('$price'),
input: Expression::variable('ROOT'),
input: Variable::Root,
value: 49.99,
),
),
Expand Down
Loading