Skip to content

Commit 96265de

Browse files
committed
Convert min/max options in the metadata class
1 parent 6d0274a commit 96265de

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,16 @@ public function mapField(array $mapping): array
23862386
$mapping['nullable'] = false;
23872387
}
23882388

2389+
if (isset($mapping['encrypt']['queryType'])) {
2390+
// The encrypted range query options min and max must be converted to the database type
2391+
$type = Type::getType($mapping['type']);
2392+
foreach (['min', 'max'] as $option) {
2393+
if (isset($mapping['encrypt'][$option])) {
2394+
$mapping['encrypt'][$option] = $type->convertToDatabaseValue($mapping['encrypt'][$option]);
2395+
}
2396+
}
2397+
}
2398+
23892399
if (
23902400
isset($mapping['reference'])
23912401
&& isset($mapping['storeAs'])

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ private function addEncryptionMapping(SimpleXMLElement $encrypt, string $type):
940940
foreach ($encrypt->attributes() as $key => $value) {
941941
$encryptMapping[$key] = match ($key) {
942942
'queryType' => EncryptQuery::from((string) $value),
943-
'min', 'max' => $value,
943+
'min', 'max' => (string) $value,
944944
'sparsity', 'precision', 'trimFactor', 'contention' => (int) $value,
945945
};
946946
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ private function createEncryptedFieldsMapForClass(
140140
if (isset($mapping['encrypt']['queryType'])) {
141141
$field['queries'] = array_filter($mapping['encrypt'], static fn ($v) => $v !== null);
142142
$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-
}
149143
}
150144

151145
yield $field;

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/AbstractDriverTestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Documents\Phonenumber;
1818
use Documents\Profile;
1919
use MongoDB\BSON\Decimal128;
20+
use MongoDB\BSON\Int64;
2021
use MongoDB\BSON\UTCDateTime;
2122
use PHPUnit\Framework\TestCase;
2223
use TestDocuments\EmbeddedDocument;
@@ -26,6 +27,8 @@
2627
use TestDocuments\QueryResultDocument;
2728
use TestDocuments\User;
2829

30+
use const PHP_INT_MAX;
31+
2932
abstract class AbstractDriverTestCase extends TestCase
3033
{
3134
protected MappingDriver|null $driver;
@@ -573,6 +576,12 @@ public function testEncryptQueryRangeTypes(): void
573576
'max' => 10,
574577
], $classMetadata->fieldMappings['intField']['encrypt']);
575578

579+
self::assertEquals([
580+
'queryType' => EncryptQuery::Range,
581+
'min' => new Int64(5),
582+
'max' => new Int64(PHP_INT_MAX - 5),
583+
], $classMetadata->fieldMappings['int64Field']['encrypt']);
584+
576585
self::assertEquals([
577586
'queryType' => EncryptQuery::Range,
578587
'min' => 5.5,

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/Documents.Encryption.RangeTypes.dcm.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<field name="intField" type="int">
1111
<encrypt queryType="range" min="5" max="10"/>
1212
</field>
13+
<field name="int64Field" type="int64">
14+
<encrypt queryType="range" min="5" max="9223372036854775802"/>
15+
</field>
1316
<field name="floatField" type="float">
1417
<encrypt queryType="range" min="5.5" max="10.5"/>
1518
</field>

0 commit comments

Comments
 (0)