This repository was archived by the owner on Feb 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +67
-3
lines changed Expand file tree Collapse file tree 4 files changed +67
-3
lines changed Original file line number Diff line number Diff line change 4
4
5
5
namespace MongoDB \Builder ;
6
6
7
+ use BackedEnum ;
7
8
use LogicException ;
8
9
use MongoDB \Builder \Expression \Variable ;
9
10
use MongoDB \Builder \Stage \GroupStage ;
@@ -101,6 +102,10 @@ public function encode($value): stdClass|array|string
101
102
throw new LogicException (sprintf ('Class "%s" does not implement OperatorInterface. ' , $ value ::class));
102
103
}
103
104
105
+ if ($ value instanceof BackedEnum) {
106
+ return $ this ->encodeAsEnum ($ value );
107
+ }
108
+
104
109
// The generic but incomplete encoding code
105
110
switch ($ value ::ENCODE ) {
106
111
case Encode::Single:
@@ -124,6 +129,16 @@ public function encode($value): stdClass|array|string
124
129
throw new LogicException (sprintf ('Class "%s" does not have a valid ENCODE constant. ' , $ value ::class));
125
130
}
126
131
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
+
127
142
/**
128
143
* Encode the value as an array of properties, in the order they are defined in the class.
129
144
*/
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ final class Stage
21
21
*
22
22
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/
23
23
*
24
- * @param QueryInterface|FieldQueryInterface|Type |stdClass|array<array-key,mixed>|bool|float|int|string|null ...$queries The query predicates to match
24
+ * @param QueryInterface|FieldQueryInterface|QueryType |stdClass|array<array-key,mixed>|bool|float|int|string|null ...$queries The query predicates to match
25
25
*/
26
26
public static function match (QueryInterface |FieldQueryInterface |Type |stdClass |array |bool |float |int |string |null ...$ queries ): MatchStage
27
27
{
Original file line number Diff line number Diff line change 6
6
7
7
use MongoDB \Builder \Pipeline ;
8
8
use MongoDB \Builder \Query ;
9
+ use MongoDB \Builder \QueryType ;
9
10
use MongoDB \Builder \Stage ;
10
11
use MongoDB \Tests \Builder \PipelineTestCase ;
11
12
@@ -32,7 +33,7 @@ public function testQueryingByDataType(): void
32
33
zipCode: Query::type (2 ),
33
34
),
34
35
Stage::match (
35
- zipCode: Query:: type ( ' string ' ) ,
36
+ zipCode: QueryType::String ,
36
37
),
37
38
Stage::match (
38
39
zipCode: Query::type (1 ),
@@ -41,7 +42,7 @@ public function testQueryingByDataType(): void
41
42
zipCode: Query::type ('double ' ),
42
43
),
43
44
Stage::match (
44
- zipCode: Query:: type ( ' number ' ) ,
45
+ zipCode: QueryType::Number ,
45
46
),
46
47
);
47
48
You can’t perform that action at this time.
0 commit comments