@@ -45,6 +45,10 @@ static bool php_phongo_bulkwritecommandresult_get_writeconcernerrors(php_phongo_
4545
4646 array_init (return_value );
4747
48+ if (!intern -> write_concern_errors ) {
49+ return true;
50+ }
51+
4852 for (bson_iter_init (& iter , intern -> write_concern_errors ); bson_iter_next (& iter );) {
4953 bson_t bson ;
5054 uint32_t len ;
@@ -78,6 +82,10 @@ static bool php_phongo_bulkwritecommandresult_get_writeerrors(php_phongo_bulkwri
7882
7983 array_init (return_value );
8084
85+ if (!intern -> write_errors ) {
86+ return true;
87+ }
88+
8189 for (bson_iter_init (& iter , intern -> write_errors ); bson_iter_next (& iter );) {
8290 bson_t bson ;
8391 uint32_t len ;
@@ -246,7 +254,6 @@ static PHP_METHOD(MongoDB_Driver_BulkWriteCommandResult, getWriteConcernErrors)
246254
247255 PHONGO_PARSE_PARAMETERS_NONE ();
248256
249- // TODO: null handling
250257 php_phongo_bulkwritecommandresult_get_writeconcernerrors (intern , return_value );
251258}
252259
@@ -259,7 +266,6 @@ static PHP_METHOD(MongoDB_Driver_BulkWriteCommandResult, getWriteErrors)
259266
260267 PHONGO_PARSE_PARAMETERS_NONE ();
261268
262- // TODO: null handling
263269 php_phongo_bulkwritecommandresult_get_writeerrors (intern , return_value );
264270}
265271
@@ -271,8 +277,9 @@ static PHP_METHOD(MongoDB_Driver_BulkWriteCommandResult, getErrorReply)
271277
272278 PHONGO_PARSE_PARAMETERS_NONE ();
273279
274- // TODO: null handling
275- phongo_document_new (return_value , intern -> error_reply , true);
280+ if (intern -> error_reply ) {
281+ phongo_document_new (return_value , intern -> error_reply , true);
282+ }
276283}
277284
278285/* Returns whether the write operation was acknowledged (based on the write
@@ -381,22 +388,18 @@ static HashTable* php_phongo_bulkwritecommandresult_get_debug_info(zend_object*
381388 ADD_ASSOC_NULL_EX (& retval , "deleteResults" );
382389 }
383390
384- if ( intern -> write_errors ) {
391+ {
385392 zval writeerrors ;
386393
387394 php_phongo_bulkwritecommandresult_get_writeerrors (intern , & writeerrors );
388395 ADD_ASSOC_ZVAL_EX (& retval , "writeErrors" , & writeerrors );
389- } else {
390- ADD_ASSOC_NULL_EX (& retval , "writeErrors" );
391396 }
392397
393- if ( intern -> write_concern_errors ) {
398+ {
394399 zval writeconcernerrors ;
395400
396401 php_phongo_bulkwritecommandresult_get_writeconcernerrors (intern , & writeconcernerrors );
397402 ADD_ASSOC_ZVAL_EX (& retval , "writeConcernErrors" , & writeconcernerrors );
398- } else {
399- ADD_ASSOC_NULL_EX (& retval , "writeConcernErrors" );
400403 }
401404
402405 if (intern -> error_reply ) {
@@ -433,6 +436,11 @@ void php_phongo_bulkwritecommandresult_init_ce(INIT_FUNC_ARGS)
433436 php_phongo_handler_bulkwritecommandresult .offset = XtOffsetOf (php_phongo_bulkwritecommandresult_t , std );
434437}
435438
439+ static inline bson_t * _bson_copy_or_null (const bson_t * bson )
440+ {
441+ return bson_empty0 (bson ) ? NULL : bson_copy (bson );
442+ }
443+
436444php_phongo_bulkwritecommandresult_t * phongo_bulkwritecommandresult_init (zval * return_value , mongoc_bulkwritereturn_t * bw_ret , zval * manager )
437445{
438446 php_phongo_bulkwritecommandresult_t * bwcr ;
@@ -450,20 +458,18 @@ php_phongo_bulkwritecommandresult_t* phongo_bulkwritecommandresult_init(zval* re
450458 bwcr -> modified_count = mongoc_bulkwriteresult_modifiedcount (bw_ret -> res );
451459 bwcr -> deleted_count = mongoc_bulkwriteresult_deletedcount (bw_ret -> res );
452460
453- #define BSON_COPY_OR_NULL (bson ) ((bson) ? bson_copy(bson) : NULL)
454- bwcr -> insert_results = BSON_COPY_OR_NULL (mongoc_bulkwriteresult_insertresults (bw_ret -> res ));
455- bwcr -> update_results = BSON_COPY_OR_NULL (mongoc_bulkwriteresult_updateresults (bw_ret -> res ));
456- bwcr -> delete_results = BSON_COPY_OR_NULL (mongoc_bulkwriteresult_deleteresults (bw_ret -> res ));
457- #undef BSON_COPY_OR_NULL
461+ bwcr -> insert_results = _bson_copy_or_null (mongoc_bulkwriteresult_insertresults (bw_ret -> res ));
462+ bwcr -> update_results = _bson_copy_or_null (mongoc_bulkwriteresult_updateresults (bw_ret -> res ));
463+ bwcr -> delete_results = _bson_copy_or_null (mongoc_bulkwriteresult_deleteresults (bw_ret -> res ));
458464
459465 bwcr -> server_id = mongoc_bulkwriteresult_serverid (bw_ret -> res );
460466 }
461467
462468 // Copy mongoc_bulkwriteexception_t fields
463469 if (bw_ret -> exc ) {
464- bwcr -> error_reply = bson_copy (mongoc_bulkwriteexception_errorreply (bw_ret -> exc ));
465- bwcr -> write_errors = bson_copy (mongoc_bulkwriteexception_writeerrors (bw_ret -> exc ));
466- bwcr -> write_concern_errors = bson_copy (mongoc_bulkwriteexception_writeconcernerrors (bw_ret -> exc ));
470+ bwcr -> error_reply = _bson_copy_or_null (mongoc_bulkwriteexception_errorreply (bw_ret -> exc ));
471+ bwcr -> write_errors = _bson_copy_or_null (mongoc_bulkwriteexception_writeerrors (bw_ret -> exc ));
472+ bwcr -> write_concern_errors = _bson_copy_or_null (mongoc_bulkwriteexception_writeconcernerrors (bw_ret -> exc ));
467473 }
468474
469475 ZVAL_ZVAL (& bwcr -> manager , manager , 1 , 0 );
0 commit comments