Skip to content

Commit 6f260f9

Browse files
authored
Expand accepted types for field queries (mongodb#11)
1 parent 9a8ad5b commit 6f260f9

File tree

8 files changed

+29
-33
lines changed

8 files changed

+29
-33
lines changed

generator/config/expressions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
],
8484
'fieldQuery' => [
8585
'returnType' => Type\FieldQueryInterface::class,
86-
'acceptedTypes' => [Type\FieldQueryInterface::class, BSON\Regex::class, 'bool', 'string', 'array', 'null', stdClass::class, ...$bsonTypes['number']],
86+
'acceptedTypes' => [Type\FieldQueryInterface::class, ...$bsonTypes['any']],
8787
],
8888
'query' => [
8989
'returnType' => Type\QueryInterface::class,

src/Builder/Query.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace MongoDB\Builder;
66

7-
use MongoDB\BSON\Decimal128;
8-
use MongoDB\BSON\Int64;
97
use MongoDB\BSON\Regex;
8+
use MongoDB\BSON\Type;
109
use MongoDB\Builder\Query\RegexOperator;
1110
use MongoDB\Builder\Type\FieldQueryInterface;
1211
use MongoDB\Builder\Type\QueryInterface;
@@ -41,7 +40,7 @@ public static function regex(Regex|string $regex, string|null $flags = null): Re
4140
return self::generatedRegex($regex);
4241
}
4342

44-
public static function query(QueryInterface|FieldQueryInterface|Decimal128|Int64|Regex|stdClass|array|bool|float|int|string|null ...$query): QueryInterface
43+
public static function query(QueryInterface|FieldQueryInterface|Type|stdClass|array|bool|float|int|string|null ...$query): QueryInterface
4544
{
4645
return QueryObject::create($query);
4746
}

src/Builder/Query/FactoryTrait.php

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Query/NotOperator.php

Lines changed: 6 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Stage.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace MongoDB\Builder;
66

7-
use MongoDB\BSON\Decimal128;
8-
use MongoDB\BSON\Int64;
9-
use MongoDB\BSON\Regex;
7+
use MongoDB\BSON\Type;
108
use MongoDB\Builder\Stage\MatchStage;
119
use MongoDB\Builder\Type\FieldQueryInterface;
1210
use MongoDB\Builder\Type\QueryInterface;
@@ -23,9 +21,9 @@ final class Stage
2321
*
2422
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/
2523
*
26-
* @param QueryInterface|FieldQueryInterface|Decimal128|Int64|Regex|stdClass|array<array-key,mixed>|bool|float|int|string|null ...$queries The query predicates to match
24+
* @param QueryInterface|FieldQueryInterface|Type|stdClass|array<array-key,mixed>|bool|float|int|string|null ...$queries The query predicates to match
2725
*/
28-
public static function match(QueryInterface|FieldQueryInterface|Decimal128|Int64|Regex|stdClass|array|bool|float|int|string|null ...$queries): MatchStage
26+
public static function match(QueryInterface|FieldQueryInterface|Type|stdClass|array|bool|float|int|string|null ...$queries): MatchStage
2927
{
3028
// Override the generated method to allow variadic arguments
3129
return self::generatedMatch($queries);

src/Builder/Type/CombinedFieldQuery.php

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

55
namespace MongoDB\Builder\Type;
66

7-
use MongoDB\BSON\Decimal128;
8-
use MongoDB\BSON\Int64;
9-
use MongoDB\BSON\Regex;
7+
use MongoDB\BSON\Type;
108
use MongoDB\Exception\InvalidArgumentException;
119
use stdClass;
1210

@@ -28,18 +26,18 @@
2826
*/
2927
class CombinedFieldQuery implements FieldQueryInterface
3028
{
31-
/** @var list<QueryInterface|FieldQueryInterface|Decimal128|Int64|Regex|stdClass|array|bool|float|int|string|null> $fieldQueries */
29+
/** @var list<QueryInterface|FieldQueryInterface|Type|stdClass|array|bool|float|int|string|null> $fieldQueries */
3230
public readonly array $fieldQueries;
3331

34-
/** @param list<QueryInterface|FieldQueryInterface|Decimal128|Int64|Regex|stdClass|array|bool|float|int|string|null> $fieldQueries */
32+
/** @param list<QueryInterface|FieldQueryInterface|Type|stdClass|array|bool|float|int|string|null> $fieldQueries */
3533
public function __construct(array $fieldQueries)
3634
{
3735
if (! array_is_list($fieldQueries)) {
3836
throw new InvalidArgumentException('Expected filters to be a list, invalid array given.');
3937
}
4038

4139
// Flatten nested CombinedFieldQuery
42-
$this->fieldQueries = array_reduce($fieldQueries, static function (array $fieldQueries, QueryInterface|FieldQueryInterface|Decimal128|Int64|Regex|stdClass|array|bool|float|int|string|null $fieldQuery): array {
40+
$this->fieldQueries = array_reduce($fieldQueries, static function (array $fieldQueries, QueryInterface|FieldQueryInterface|Type|stdClass|array|bool|float|int|string|null $fieldQuery): array {
4341
if ($fieldQuery instanceof CombinedFieldQuery) {
4442
return array_merge($fieldQueries, $fieldQuery->fieldQueries);
4543
}

src/Builder/Type/QueryObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private function __construct(array $queriesOrArrayOfQueries)
8484
$this->queries = $queries;
8585
}
8686

87-
/** @psalm-assert-if-true list<mixed> $values */
87+
/** @psalm-assert-if-true list<FieldQueryInterface> $values */
8888
private static function isListOfFilters(mixed $values): bool
8989
{
9090
if (! is_array($values) || ! array_is_list($values)) {

tests/Builder/Type/QueryObjectTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
namespace MongoDB\Tests\Builder\Type;
66

77
use Generator;
8-
use MongoDB\BSON\Decimal128;
9-
use MongoDB\BSON\Int64;
10-
use MongoDB\BSON\Regex;
8+
use MongoDB\BSON;
119
use MongoDB\Builder\Query\CommentOperator;
1210
use MongoDB\Builder\Query\EqOperator;
1311
use MongoDB\Builder\Query\GtOperator;
@@ -66,9 +64,15 @@ public function provideQueryObjectValue(): Generator
6664
yield 'string' => [['foo' => 'bar']];
6765
yield 'bool' => [['foo' => true]];
6866
yield 'null' => [['foo' => null]];
69-
yield 'decimal128' => [['foo' => new Decimal128('1.1')]];
70-
yield 'int64' => [[1 => new Int64(1)]];
71-
yield 'regex' => [['foo' => new Regex('foo')]];
67+
yield 'decimal128' => [['foo' => new BSON\Decimal128('1.1')]];
68+
yield 'int64' => [[1 => new BSON\Int64(1)]];
69+
yield 'objectId' => [['foo' => new BSON\ObjectId()]];
70+
yield 'binary' => [['foo' => new BSON\Binary('foo')]];
71+
yield 'regex' => [['foo' => new BSON\Regex('foo')]];
72+
yield 'datetime' => [['foo' => new BSON\UTCDateTime()]];
73+
yield 'timestamp' => [['foo' => new BSON\Timestamp(1234, 5678)]];
74+
yield 'bson document' => [['foo' => BSON\Document::fromPHP(['bar' => 'baz'])]];
75+
yield 'bson array' => [['foo' => BSON\PackedArray::fromPHP(['bar', 'baz'])]];
7276
yield 'object' => [['foo' => (object) ['bar' => 'baz']]];
7377
yield 'list' => [['foo' => ['bar', 'baz']]];
7478
yield 'operator as array' => [['foo' => ['$eq' => 1]]];

0 commit comments

Comments
 (0)