Skip to content

Commit 7c4eac6

Browse files
committed
PHPLIB-1001: Support "number" alias for legacy $$type operator
Also add a default case to raise an exception for unsupported types, which were previously ignored.
1 parent 61c17de commit 7c4eac6

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

tests/SpecTests/DocumentsMatchConstraint.php

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

55
use ArrayObject;
66
use InvalidArgumentException;
7+
use LogicException;
78
use MongoDB\BSON\BinaryInterface;
89
use MongoDB\BSON\DBPointer;
910
use MongoDB\BSON\Decimal128;
@@ -243,6 +244,19 @@ private function assertBSONType(string $expectedType, $actualValue): void
243244
(new IsInstanceOf(MaxKey::class))->evaluate($actualValue);
244245

245246
return;
247+
248+
case 'number':
249+
LogicalOr::fromConstraints(
250+
new IsType('int'),
251+
new IsInstanceOf(Int64::class),
252+
new IsType('float'),
253+
new IsInstanceOf(Decimal128::class)
254+
)->evaluate($actualValue);
255+
256+
return;
257+
258+
default:
259+
throw new LogicException('Unsupported type: ' . $expectedType);
246260
}
247261
}
248262

tests/SpecTests/DocumentsMatchConstraintTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ public function provideBSONTypes()
112112
'decimal' => ['decimal', new Decimal128('18446744073709551616')],
113113
'minKey' => ['minKey', new MinKey()],
114114
'maxKey' => ['maxKey', new MaxKey()],
115+
'number(double)' => ['number', 1.4],
116+
'number(decimal)' => ['number', new Decimal128('18446744073709551616')],
117+
'number(int)' => ['number', 1],
118+
'number(int64)' => ['number', $int64],
119+
'number(long)' => ['number', $long],
115120
];
116121
}
117122

0 commit comments

Comments
 (0)