Skip to content

Commit ee660a0

Browse files
committed
Revise Int64 helper method and apply type hinting
1 parent 09d194a commit ee660a0

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,27 +1154,10 @@ static function (self $test, ClientEncryption $clientEncryptionNoClientCert, Cli
11541154
];
11551155
}
11561156

1157-
/**
1158-
* Casts the value for a BSON corpus structure to int64 if necessary.
1159-
*
1160-
* This is a workaround for an issue in mongocryptd which refuses to encrypt
1161-
* int32 values if the schemaMap defines a "long" bsonType for an object.
1162-
*
1163-
* @param object $data
1164-
*
1165-
* @return Int64|mixed
1166-
*/
1167-
private function craftInt64($data)
1157+
private function createInt64(string $value): Int64
11681158
{
1169-
if ($data->type !== 'long' || $data->value instanceof Int64) {
1170-
return $data->value;
1171-
}
1172-
1173-
$class = Int64::class;
1174-
1175-
$intAsString = sprintf((string) $data->value);
1176-
$array = sprintf('a:1:{s:7:"integer";s:%d:"%s";}', strlen($intAsString), $intAsString);
1177-
$int64 = sprintf('C:%d:"%s":%d:{%s}', strlen($class), $class, strlen($array), $array);
1159+
$array = sprintf('a:1:{s:7:"integer";s:%d:"%s";}', strlen($value), $value);
1160+
$int64 = sprintf('C:%d:"%s":%d:{%s}', strlen(Int64::class), Int64::class, strlen($array), $array);
11781161

11791162
return unserialize($int64);
11801163
}
@@ -1233,7 +1216,13 @@ private function encryptCorpusValue(string $fieldName, stdClass $data, ClientEnc
12331216

12341217
if ($data->allowed) {
12351218
try {
1236-
$encrypted = $clientEncryption->encrypt($this->craftInt64($data), $encryptionOptions);
1219+
/* Note: workaround issue where mongocryptd refuses to encrypt
1220+
* 32-bit integers if schemaMap defines a "long" BSON type. */
1221+
$value = $data->type === 'long' && ! $data->value instanceof Int64
1222+
? $this->createInt64($data->value)
1223+
: $data->value;
1224+
1225+
$encrypted = $clientEncryption->encrypt($value, $encryptionOptions);
12371226
} catch (EncryptionException $e) {
12381227
$this->fail('Could not encrypt value for field ' . $fieldName . ': ' . $e->getMessage());
12391228
}
@@ -1279,7 +1268,11 @@ private function insertKeyVaultData(?array $keyVaultData = null): void
12791268
private function prepareCorpusData(string $fieldName, stdClass $data, ClientEncryption $clientEncryption)
12801269
{
12811270
if ($data->method === 'auto') {
1282-
$data->value = $this->craftInt64($data);
1271+
/* Note: workaround issue where mongocryptd refuses to encrypt
1272+
* 32-bit integers if schemaMap defines a "long" BSON type. */
1273+
if ($data->type === 'long' && ! $data->value instanceof Int64) {
1274+
$data->value = $this->createInt64($data->value);
1275+
}
12831276

12841277
return $data;
12851278
}

0 commit comments

Comments
 (0)