Skip to content

Commit 6d0274a

Browse files
committed
Add Int64 type
1 parent dad3800 commit 6d0274a

File tree

9 files changed

+89
-83
lines changed

9 files changed

+89
-83
lines changed

docs/en/reference/basic-mapping.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Here is a quick overview of the built-in mapping types:
145145
- ``hash``
146146
- ``id``
147147
- ``int``
148+
- ``int64``
148149
- ``key``
149150
- ``object_id``
150151
- ``raw``

lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1010
use Doctrine\ODM\MongoDB\Mapping\MappingException;
1111
use Doctrine\ODM\MongoDB\Mapping\TimeSeries\Granularity;
12-
use Doctrine\ODM\MongoDB\Types\Type;
1312
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
1413
use Doctrine\Persistence\Mapping\Driver\FileDriver;
1514
use DOMDocument;
@@ -941,7 +940,7 @@ private function addEncryptionMapping(SimpleXMLElement $encrypt, string $type):
941940
foreach ($encrypt->attributes() as $key => $value) {
942941
$encryptMapping[$key] = match ($key) {
943942
'queryType' => EncryptQuery::from((string) $value),
944-
'min', 'max' => Type::getType($type)->convertToDatabaseValue((string) $value),
943+
'min', 'max' => $value,
945944
'sparsity', 'precision', 'trimFactor', 'contention' => (int) $value,
946945
};
947946
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ODM\MongoDB\Types;
6+
7+
use MongoDB\BSON\Int64;
8+
9+
/**
10+
* The Int64 type (long)
11+
*/
12+
class Int64Type extends IntType implements Incrementable, Versionable
13+
{
14+
public function convertToDatabaseValue($value)
15+
{
16+
return $value !== null ? new Int64($value) : null;
17+
}
18+
19+
public function closureToMongo(): string
20+
{
21+
return '$return = new \MongoDB\BSON\Int64($value);';
22+
}
23+
}

lib/Doctrine/ODM/MongoDB/Types/Type.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ abstract class Type
2626
public const CUSTOMID = 'custom_id';
2727
public const BOOL = 'bool';
2828
public const INT = 'int';
29+
public const INT64 = 'int64';
2930
public const FLOAT = 'float';
3031
public const STRING = 'string';
3132
public const DATE = 'date';
@@ -66,6 +67,7 @@ abstract class Type
6667
self::BOOLEAN => Types\BooleanType::class,
6768
self::INT => Types\IntType::class,
6869
self::INTEGER => Types\IntType::class,
70+
self::INT64 => Types\Int64Type::class,
6971
self::FLOAT => Types\FloatType::class,
7072
self::STRING => Types\StringType::class,
7173
self::DATE => Types\DateType::class,

lib/Doctrine/ODM/MongoDB/Utility/EncryptedFieldsMapGenerator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ private function createEncryptedFieldsMapForClass(
122122
ClassMetadata::ONE, Type::HASH => 'object',
123123
ClassMetadata::MANY, Type::COLLECTION => 'array',
124124
Type::INT, Type::INTEGER => 'int',
125+
Type::INT64 => 'long',
125126
Type::FLOAT => 'double',
126127
Type::DECIMAL128 => 'decimal',
127128
Type::DATE, Type::DATE_IMMUTABLE => 'date',
@@ -139,6 +140,12 @@ private function createEncryptedFieldsMapForClass(
139140
if (isset($mapping['encrypt']['queryType'])) {
140141
$field['queries'] = array_filter($mapping['encrypt'], static fn ($v) => $v !== null);
141142
$field['queries']['queryType'] = $field['queries']['queryType']->value;
143+
144+
foreach (['min', 'max'] as $option) {
145+
if (isset($field['queries'][$option])) {
146+
$field['queries'][$option] = Type::getType($mapping['type'])->convertToDatabaseValue($field['queries'][$option]);
147+
}
148+
}
142149
}
143150

144151
yield $field;

phpstan-baseline.neon

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: '#^Call to an undefined method MongoDB\\Driver\\Monitoring\\CommandFailedEvent\|MongoDB\\Driver\\Monitoring\\CommandSucceededEvent\:\:getServer\(\)\.$#'
5-
identifier: method.notFound
6-
count: 1
7-
path: lib/Doctrine/ODM/MongoDB/APM/Command.php
8-
9-
-
10-
message: '#^Call to function assert\(\) with true will always evaluate to true\.$#'
11-
identifier: function.alreadyNarrowedType
12-
count: 2
13-
path: lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php
14-
15-
-
16-
message: '#^Instanceof between MongoDB\\Driver\\CursorInterface and Iterator will always evaluate to true\.$#'
17-
identifier: instanceof.alwaysTrue
18-
count: 1
19-
path: lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php
20-
21-
-
22-
message: '#^Instanceof between MongoDB\\Driver\\CursorInterface and MongoDB\\Driver\\CursorInterface will always evaluate to true\.$#'
23-
identifier: instanceof.alwaysTrue
24-
count: 1
25-
path: lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php
26-
273
-
284
message: '#^Method Doctrine\\ODM\\MongoDB\\Aggregation\\Aggregation\:\:__construct\(\) has parameter \$classMetadata with generic class Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata but does not specify its types\: T$#'
295
identifier: missingType.generics
@@ -1116,36 +1092,12 @@ parameters:
11161092
count: 1
11171093
path: lib/Doctrine/ODM/MongoDB/Persisters/CollectionPersister.php
11181094

1119-
-
1120-
message: '#^Call to function assert\(\) with true will always evaluate to true\.$#'
1121-
identifier: function.alreadyNarrowedType
1122-
count: 2
1123-
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
1124-
11251095
-
11261096
message: '#^Call to function is_array\(\) with array will always evaluate to true\.$#'
11271097
identifier: function.alreadyNarrowedType
11281098
count: 1
11291099
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
11301100

1131-
-
1132-
message: '#^Instanceof between MongoDB\\Collection and MongoDB\\Collection will always evaluate to true\.$#'
1133-
identifier: instanceof.alwaysTrue
1134-
count: 1
1135-
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
1136-
1137-
-
1138-
message: '#^Instanceof between MongoDB\\Driver\\CursorInterface and Iterator will always evaluate to true\.$#'
1139-
identifier: instanceof.alwaysTrue
1140-
count: 1
1141-
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
1142-
1143-
-
1144-
message: '#^Instanceof between MongoDB\\Driver\\CursorInterface and MongoDB\\Driver\\CursorInterface will always evaluate to true\.$#'
1145-
identifier: instanceof.alwaysTrue
1146-
count: 1
1147-
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
1148-
11491101
-
11501102
message: '#^Method Doctrine\\ODM\\MongoDB\\Persisters\\DocumentPersister\:\:createReferenceManyInverseSideQuery\(\) has parameter \$collection with generic interface Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionInterface but does not specify its types\: TKey, T$#'
11511103
identifier: missingType.generics
@@ -1236,12 +1188,6 @@ parameters:
12361188
count: 1
12371189
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
12381190

1239-
-
1240-
message: '#^Result of && is always true\.$#'
1241-
identifier: booleanAnd.alwaysTrue
1242-
count: 1
1243-
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
1244-
12451191
-
12461192
message: '#^Call to an undefined static method Doctrine\\ODM\\MongoDB\\Proxy\\Factory\\LazyGhostProxyFactory\:\:createLazyGhost\(\)\.$#'
12471193
identifier: staticMethod.notFound

tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
use Documents\Encryption\PatientRecord;
2424
use Documents\Encryption\RangeTypes;
2525
use MongoDB\BSON\Decimal128;
26+
use MongoDB\BSON\Int64;
2627
use MongoDB\BSON\UTCDateTime;
2728

2829
use function array_map;
2930

31+
use const PHP_INT_MAX;
32+
3033
class EncryptedFieldsMapGeneratorTest extends BaseTestCase
3134
{
3235
public function testGetEncryptionFieldsMapForClass(): void
@@ -90,6 +93,16 @@ public function testVariousRangeTypes(): void
9093
'keyId' => null,
9194
'queries' => ['queryType' => 'range', 'min' => 5, 'max' => 10],
9295
],
96+
[
97+
'path' => 'int64Field',
98+
'bsonType' => 'long',
99+
'keyId' => null,
100+
'queries' => [
101+
'queryType' => 'range',
102+
'min' => new Int64(5),
103+
'max' => new Int64(PHP_INT_MAX - 5),
104+
],
105+
],
93106
[
94107
'path' => 'floatField',
95108
'bsonType' => 'double',

tests/Doctrine/ODM/MongoDB/Tests/Types/TypeTest.php

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Doctrine\ODM\MongoDB\Types\Type;
1111
use MongoDB\BSON\Binary;
1212
use MongoDB\BSON\Decimal128;
13+
use MongoDB\BSON\Int64;
14+
use MongoDB\BSON\MaxKey;
15+
use MongoDB\BSON\MinKey;
1316
use MongoDB\BSON\ObjectId;
1417
use MongoDB\BSON\Timestamp;
1518
use MongoDB\BSON\UTCDateTime;
@@ -24,40 +27,46 @@
2427

2528
class TypeTest extends BaseTestCase
2629
{
27-
/** @param mixed $test */
2830
#[DataProvider('provideTypes')]
29-
public function testConversion(Type $type, $test): void
31+
public function testConversion(string $typeName, mixed $phpValue, mixed $bsonValue = null): void
3032
{
31-
self::assertEquals($test, $type->convertToPHPValue($type->convertToDatabaseValue($test)));
33+
$bsonValue ??= $phpValue;
34+
$type = Type::getType($typeName);
35+
36+
self::assertEquals($phpValue, $type->convertToPHPValue($bsonValue));
37+
self::assertEquals($bsonValue, $type->convertToDatabaseValue($phpValue));
3238
}
3339

3440
public static function provideTypes(): array
3541
{
42+
$array = ['foo' => 'bar'];
43+
3644
return [
37-
'id' => [Type::getType(Type::ID), '507f1f77bcf86cd799439011'],
38-
'intId' => [Type::getType(Type::INTID), 1],
39-
'customId' => [Type::getType(Type::CUSTOMID), (object) ['foo' => 'bar']],
40-
'bool' => [Type::getType(Type::BOOL), true],
41-
'boolean' => [Type::getType(Type::BOOLEAN), false],
42-
'int' => [Type::getType(Type::INT), 69],
43-
'integer' => [Type::getType(Type::INTEGER), 42],
44-
'float' => [Type::getType(Type::FLOAT), 3.14],
45-
'string' => [Type::getType(Type::STRING), 'ohai'],
46-
'minKey' => [Type::getType(Type::KEY), 0],
47-
'maxKey' => [Type::getType(Type::KEY), 1],
48-
'timestamp' => [Type::getType(Type::TIMESTAMP), time()],
49-
'binData' => [Type::getType(Type::BINDATA), 'foobarbaz'],
50-
'binDataFunc' => [Type::getType(Type::BINDATAFUNC), 'foobarbaz'],
51-
'binDataByteArray' => [Type::getType(Type::BINDATABYTEARRAY), 'foobarbaz'],
52-
'binDataUuid' => [Type::getType(Type::BINDATAUUID), 'testtesttesttest'],
53-
'binDataUuidRFC4122' => [Type::getType(Type::BINDATAUUIDRFC4122), str_repeat('a', 16)],
54-
'binDataMD5' => [Type::getType(Type::BINDATAMD5), md5('ODM')],
55-
'binDataCustom' => [Type::getType(Type::BINDATACUSTOM), 'foobarbaz'],
56-
'hash' => [Type::getType(Type::HASH), ['foo' => 'bar']],
57-
'collection' => [Type::getType(Type::COLLECTION), ['foo', 'bar']],
58-
'objectId' => [Type::getType(Type::OBJECTID), '507f1f77bcf86cd799439011'],
59-
'raw' => [Type::getType(Type::RAW), (object) ['foo' => 'bar']],
60-
'decimal128' => [Type::getType(Type::DECIMAL128), '4.20'],
45+
'id' => [Type::ID, '507f1f77bcf86cd799439011', new ObjectId('507f1f77bcf86cd799439011')],
46+
'intId' => [Type::INTID, 1],
47+
'customId' => [Type::CUSTOMID, (object) ['foo' => 'bar']],
48+
'bool' => [Type::BOOL, true],
49+
'boolean' => [Type::BOOLEAN, false],
50+
'int' => [Type::INT, 69],
51+
'integer' => [Type::INTEGER, 42],
52+
'int64' => [Type::INT64, 9223372036854775807, new Int64(9223372036854775807)],
53+
'float' => [Type::FLOAT, 3.14],
54+
'string' => [Type::STRING, 'ohai'],
55+
'minKey' => [Type::KEY, 0, new MinKey()],
56+
'maxKey' => [Type::KEY, 1, new MaxKey()],
57+
'timestamp' => [Type::TIMESTAMP, $t = time(), new Timestamp(0, $t)],
58+
'binData' => [Type::BINDATA, 'foobarbaz'],
59+
'binDataFunc' => [Type::BINDATAFUNC, 'foobarbaz'],
60+
'binDataByteArray' => [Type::BINDATABYTEARRAY, 'foobarbaz'],
61+
'binDataUuid' => [Type::BINDATAUUID, 'testtesttesttest'],
62+
'binDataUuidRFC4122' => [Type::BINDATAUUIDRFC4122, str_repeat('a', 16)],
63+
'binDataMD5' => [Type::BINDATAMD5, md5('ODM')],
64+
'binDataCustom' => [Type::BINDATACUSTOM, 'foobarbaz'],
65+
'hash' => [Type::HASH, ['foo' => 'bar'], (object) ['foo' => 'bar']],
66+
'collection' => [Type::COLLECTION, ['foo', 'bar']],
67+
'objectId' => [Type::OBJECTID, '507f1f77bcf86cd799439011'],
68+
'raw' => [Type::RAW, (object) ['foo' => 'bar']],
69+
'decimal128' => [Type::DECIMAL128, '4.20'],
6170
];
6271
}
6372

tests/Documents/Encryption/RangeTypes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Doctrine\ODM\MongoDB\Types\Type;
1414
use MongoDB\BSON\Decimal128;
1515

16+
use const PHP_INT_MAX;
17+
1618
/**
1719
* Test all supported types for range encrypted queries.
1820
*
@@ -28,6 +30,10 @@ class RangeTypes
2830
#[Encrypt(EncryptQuery::Range, min: 5, max: 10)]
2931
public int $intField;
3032

33+
#[Field(type: Type::INT64)]
34+
#[Encrypt(EncryptQuery::Range, min: 5, max: PHP_INT_MAX - 5)]
35+
public int $int64Field;
36+
3137
#[Field(type: Type::FLOAT)]
3238
#[Encrypt(EncryptQuery::Range, min: 5.5, max: 10.5, precision: 1)]
3339
public float $floatField;

0 commit comments

Comments
 (0)