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

Commit 7b15fe0

Browse files
committed
Query::type factory supports QueryType enum values
1 parent e29fbb1 commit 7b15fe0

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/Builder/Query.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MongoDB\BSON\Regex;
88
use MongoDB\BSON\Type;
99
use MongoDB\Builder\Query\RegexOperator;
10+
use MongoDB\Builder\Query\TypeOperator;
1011
use MongoDB\Builder\Type\CombinedFieldQuery;
1112
use MongoDB\Builder\Type\FieldQueryInterface;
1213
use MongoDB\Builder\Type\QueryInterface;
@@ -25,6 +26,27 @@ final class Query
2526
{
2627
use Query\FactoryTrait {
2728
regex as private generatedRegex;
29+
type as private generatedType;
30+
}
31+
32+
/**
33+
* Selects documents if a field is of the specified type.
34+
*
35+
* @see https://www.mongodb.com/docs/manual/reference/operator/query/type/
36+
*
37+
* @param int|non-empty-string|QueryType ...$type
38+
*
39+
* @no-named-arguments
40+
*/
41+
public static function type(string|int|QueryType ...$type): TypeOperator
42+
{
43+
foreach ($type as &$value) {
44+
if ($value instanceof QueryType) {
45+
$value = $value->value;
46+
}
47+
}
48+
49+
return self::generatedType(...$type);
2850
}
2951

3052
/**

tests/Builder/Query/TypeOperatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testQueryingByDataType(): void
3939
zipCode: Query::type(1),
4040
),
4141
Stage::match(
42-
zipCode: Query::type('double'),
42+
zipCode: Query::type(QueryType::Double),
4343
),
4444
Stage::match(
4545
zipCode: QueryType::Number,
@@ -70,7 +70,7 @@ public function testQueryingByMultipleDataType(): void
7070
zipCode: Query::type(2, 1),
7171
),
7272
Stage::match(
73-
zipCode: Query::type('string', 'double'),
73+
zipCode: Query::type(QueryType::String, 'double'),
7474
),
7575
);
7676

0 commit comments

Comments
 (0)