@@ -1154,27 +1154,10 @@ static function (self $test, ClientEncryption $clientEncryptionNoClientCert, Cli
1154
1154
];
1155
1155
}
1156
1156
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
1168
1158
{
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 );
1178
1161
1179
1162
return unserialize ($ int64 );
1180
1163
}
@@ -1233,7 +1216,13 @@ private function encryptCorpusValue(string $fieldName, stdClass $data, ClientEnc
1233
1216
1234
1217
if ($ data ->allowed ) {
1235
1218
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 );
1237
1226
} catch (EncryptionException $ e ) {
1238
1227
$ this ->fail ('Could not encrypt value for field ' . $ fieldName . ': ' . $ e ->getMessage ());
1239
1228
}
@@ -1279,7 +1268,11 @@ private function insertKeyVaultData(?array $keyVaultData = null): void
1279
1268
private function prepareCorpusData (string $ fieldName , stdClass $ data , ClientEncryption $ clientEncryption )
1280
1269
{
1281
1270
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
+ }
1283
1276
1284
1277
return $ data ;
1285
1278
}
0 commit comments