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

Commit 3a64f81

Browse files
committed
PHPLIB-1363 Add enum for list
1 parent 2660e6d commit 3a64f81

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

src/Builder/BuilderEncoder.php

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

55
namespace MongoDB\Builder;
66

7+
use BackedEnum;
78
use LogicException;
89
use MongoDB\Builder\Expression\Variable;
910
use MongoDB\Builder\Stage\GroupStage;
@@ -101,6 +102,10 @@ public function encode($value): stdClass|array|string
101102
throw new LogicException(sprintf('Class "%s" does not implement OperatorInterface.', $value::class));
102103
}
103104

105+
if ($value instanceof BackedEnum) {
106+
return $this->encodeAsEnum($value);
107+
}
108+
104109
// The generic but incomplete encoding code
105110
switch ($value::ENCODE) {
106111
case Encode::Single:
@@ -124,6 +129,16 @@ public function encode($value): stdClass|array|string
124129
throw new LogicException(sprintf('Class "%s" does not have a valid ENCODE constant.', $value::class));
125130
}
126131

132+
private function encodeAsEnum(OperatorInterface&BackedEnum $value): stdClass
133+
{
134+
$val = $value->value;
135+
if ($value::ENCODE === Encode::Array) {
136+
$val = [$val];
137+
}
138+
139+
return $this->wrap($value, $val);
140+
}
141+
127142
/**
128143
* Encode the value as an array of properties, in the order they are defined in the class.
129144
*/

src/Builder/QueryType.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Builder;
6+
7+
use MongoDB\Builder\Query\TypeOperator;
8+
use MongoDB\Builder\Type\Encode;
9+
use MongoDB\Builder\Type\FieldQueryInterface;
10+
use MongoDB\Builder\Type\OperatorInterface;
11+
12+
/**
13+
* Shortcut for $type field query operator
14+
*
15+
* @see https://www.mongodb.com/docs/manual/reference/operator/query/type/#available-types
16+
* @see TypeOperator
17+
*/
18+
enum QueryType: string implements OperatorInterface, FieldQueryInterface
19+
{
20+
public const ENCODE = Encode::Array;
21+
22+
case Double = 'double';
23+
case String = 'string';
24+
case Object = 'object';
25+
case Array = 'array';
26+
case BinaryData = 'binData';
27+
case ObjectId = 'objectId';
28+
case Boolean = 'bool';
29+
case Date = 'date';
30+
case Null = 'null';
31+
case Regex = 'regex';
32+
case JavaScript = 'javascript';
33+
case Int = 'int';
34+
case Timestamp = 'timestamp';
35+
case Long = 'long';
36+
case Decimal128 = 'decimal';
37+
case MinKey = 'minKey';
38+
case MaxKey = 'maxKey';
39+
/**
40+
* Alias for Double, Int, Long, Decimal
41+
*/
42+
case Number = 'number';
43+
44+
public function getOperator(): string
45+
{
46+
return '$type';
47+
}
48+
}

tests/Builder/Query/TypeOperatorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use MongoDB\Builder\Pipeline;
88
use MongoDB\Builder\Query;
9+
use MongoDB\Builder\QueryType;
910
use MongoDB\Builder\Stage;
1011
use MongoDB\Tests\Builder\PipelineTestCase;
1112

@@ -32,7 +33,7 @@ public function testQueryingByDataType(): void
3233
zipCode: Query::type(2),
3334
),
3435
Stage::match(
35-
zipCode: Query::type('string'),
36+
zipCode: QueryType::String,
3637
),
3738
Stage::match(
3839
zipCode: Query::type(1),
@@ -41,7 +42,7 @@ public function testQueryingByDataType(): void
4142
zipCode: Query::type('double'),
4243
),
4344
Stage::match(
44-
zipCode: Query::type('number'),
45+
zipCode: QueryType::Number,
4546
),
4647
);
4748

0 commit comments

Comments
 (0)