4040
4141zend_class_entry * php_phongo_bulkwritecommandresult_ce ;
4242
43- static bool php_phongo_bulkwritecommandresult_get_writeconcernerrors (php_phongo_bulkwritecommandresult_t * intern , zval * return_value )
43+ /* Populates return_value with a list of WriteConcernError objects. Returns true
44+ * on success; otherwise, false is returned and an exception is thrown. */
45+ static bool phongo_bulkwritecommandresult_get_writeconcernerrors (php_phongo_bulkwritecommandresult_t * intern , zval * return_value )
4446{
4547 bson_iter_t iter ;
4648
4749 array_init (return_value );
4850
49- if (!intern -> write_concern_errors ) {
50- return true;
51- }
51+ if (intern -> write_concern_errors && bson_iter_init (& iter , intern -> write_concern_errors )) {
52+ while (bson_iter_next (& iter )) {
53+ bson_t bson ;
54+ uint32_t len ;
55+ const uint8_t * data ;
56+ zval write_concern_error ;
5257
53- for (bson_iter_init (& iter , intern -> write_concern_errors ); bson_iter_next (& iter );) {
54- bson_t bson ;
55- uint32_t len ;
56- const uint8_t * data ;
57- zval write_concern_error ;
58+ if (!BSON_ITER_HOLDS_DOCUMENT (& iter )) {
59+ continue ;
60+ }
5861
59- if (!BSON_ITER_HOLDS_DOCUMENT (& iter )) {
60- continue ;
61- }
62+ bson_iter_document (& iter , & len , & data );
6263
63- bson_iter_document (& iter , & len , & data );
64+ if (!bson_init_static (& bson , data , len )) {
65+ continue ;
66+ }
6467
65- if (!bson_init_static (& bson , data , len )) {
66- continue ;
67- }
68+ if (!phongo_writeconcernerror_init (& write_concern_error , & bson )) {
69+ /* Exception already thrown */
70+ zval_ptr_dtor (& write_concern_error );
71+ return false;
72+ }
6873
69- if (!phongo_writeconcernerror_init (& write_concern_error , & bson )) {
70- zval_ptr_dtor (& write_concern_error );
71- continue ;
74+ add_next_index_zval (return_value , & write_concern_error );
7275 }
73-
74- add_next_index_zval (return_value , & write_concern_error );
7576 }
7677
7778 return true;
7879}
7980
80- static bool php_phongo_bulkwritecommandresult_get_writeerrors (php_phongo_bulkwritecommandresult_t * intern , zval * return_value )
81+ /* Populates return_value with a map of WriteError objects indexed by the offset
82+ * of the corresponding operation. Returns true on success; otherwise, false is
83+ * returned and an exception is thrown. */
84+ static bool phongo_bulkwritecommandresult_get_writeerrors (php_phongo_bulkwritecommandresult_t * intern , zval * return_value )
8185{
8286 bson_iter_t iter ;
8387
8488 array_init (return_value );
8589
86- if (!intern -> write_errors ) {
87- return true;
88- }
90+ if (intern -> write_errors && bson_iter_init (& iter , intern -> write_errors )) {
91+ while (bson_iter_next (& iter )) {
92+ bson_t bson ;
93+ uint32_t len ;
94+ const uint8_t * data ;
95+ zval write_error ;
96+ zend_ulong index ;
8997
90- for (bson_iter_init (& iter , intern -> write_errors ); bson_iter_next (& iter );) {
91- bson_t bson ;
92- uint32_t len ;
93- const uint8_t * data ;
94- zval write_error ;
95- zend_ulong index ;
98+ if (!BSON_ITER_HOLDS_DOCUMENT (& iter )) {
99+ continue ;
100+ }
96101
97- if (!BSON_ITER_HOLDS_DOCUMENT (& iter )) {
98- continue ;
99- }
102+ bson_iter_document (& iter , & len , & data );
100103
101- bson_iter_document (& iter , & len , & data );
104+ if (!bson_init_static (& bson , data , len )) {
105+ continue ;
106+ }
102107
103- if (!bson_init_static (& bson , data , len )) {
104- continue ;
105- }
108+ index = (zend_ulong ) ZEND_STRTOUL (bson_iter_key (& iter ), NULL , 10 );
106109
107- index = (zend_ulong ) ZEND_STRTOUL (bson_iter_key (& iter ), NULL , 10 );
110+ if (!phongo_writeerror_init_ex (& write_error , & bson , (int32_t ) index )) {
111+ /* Exception already thrown */
112+ zval_ptr_dtor (& write_error );
113+ return false;
114+ }
108115
109- if (!phongo_writeerror_init_ex (& write_error , & bson , (int32_t ) index )) {
110- zval_ptr_dtor (& write_error );
111- continue ;
116+ add_index_zval (return_value , index , & write_error );
112117 }
113-
114- add_index_zval (return_value , index , & write_error );
115118 }
116119
117120 return true;
@@ -258,7 +261,7 @@ static PHP_METHOD(MongoDB_Driver_BulkWriteCommandResult, getWriteConcernErrors)
258261
259262 PHONGO_PARSE_PARAMETERS_NONE ();
260263
261- php_phongo_bulkwritecommandresult_get_writeconcernerrors (intern , return_value );
264+ phongo_bulkwritecommandresult_get_writeconcernerrors (intern , return_value );
262265}
263266
264267/* Returns any write errors that occurred */
@@ -270,7 +273,7 @@ static PHP_METHOD(MongoDB_Driver_BulkWriteCommandResult, getWriteErrors)
270273
271274 PHONGO_PARSE_PARAMETERS_NONE ();
272275
273- php_phongo_bulkwritecommandresult_get_writeerrors (intern , return_value );
276+ phongo_bulkwritecommandresult_get_writeerrors (intern , return_value );
274277}
275278
276279static PHP_METHOD (MongoDB_Driver_BulkWriteCommandResult , getErrorReply )
@@ -378,14 +381,14 @@ static HashTable* php_phongo_bulkwritecommandresult_get_debug_info(zend_object*
378381 {
379382 zval writeerrors ;
380383
381- php_phongo_bulkwritecommandresult_get_writeerrors (intern , & writeerrors );
384+ phongo_bulkwritecommandresult_get_writeerrors (intern , & writeerrors );
382385 ADD_ASSOC_ZVAL_EX (& retval , "writeErrors" , & writeerrors );
383386 }
384387
385388 {
386389 zval writeconcernerrors ;
387390
388- php_phongo_bulkwritecommandresult_get_writeconcernerrors (intern , & writeconcernerrors );
391+ phongo_bulkwritecommandresult_get_writeconcernerrors (intern , & writeconcernerrors );
389392 ADD_ASSOC_ZVAL_EX (& retval , "writeConcernErrors" , & writeconcernerrors );
390393 }
391394
0 commit comments