4242#include "php_phongo.h"
4343#include "php_bson.h"
4444
45+ #define RETURN_LONG_FROM_BSON_INT32 (iter , bson , key ) \
46+ if (bson_iter_init_find((iter), (bson), (key)) && BSON_ITER_HOLDS_INT32((iter))) { \
47+ RETURN_LONG(bson_iter_int32((iter))); \
48+ }
4549
4650PHONGO_API zend_class_entry * php_phongo_writeresult_ce ;
4751
@@ -62,9 +66,7 @@ PHP_METHOD(WriteResult, getInsertedCount)
6266 return ;
6367 }
6468
65- if (bson_iter_init_find (& iter , intern -> reply , "nInserted" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
66- RETURN_LONG (bson_iter_int32 (& iter ));
67- }
69+ RETURN_LONG_FROM_BSON_INT32 (& iter , intern -> reply , "nInserted" );
6870}
6971/* }}} */
7072/* {{{ proto integer WriteResult::getMatchedCount()
@@ -82,9 +84,7 @@ PHP_METHOD(WriteResult, getMatchedCount)
8284 return ;
8385 }
8486
85- if (bson_iter_init_find (& iter , intern -> reply , "nMatched" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
86- RETURN_LONG (bson_iter_int32 (& iter ));
87- }
87+ RETURN_LONG_FROM_BSON_INT32 (& iter , intern -> reply , "nMatched" );
8888}
8989/* }}} */
9090/* {{{ proto integer|null WriteResult::getModifiedCount()
@@ -102,9 +102,7 @@ PHP_METHOD(WriteResult, getModifiedCount)
102102 return ;
103103 }
104104
105- if (bson_iter_init_find (& iter , intern -> reply , "nModified" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
106- RETURN_LONG (bson_iter_int32 (& iter ));
107- }
105+ RETURN_LONG_FROM_BSON_INT32 (& iter , intern -> reply , "nModified" );
108106}
109107/* }}} */
110108/* {{{ proto integer WriteResult::getDeletedCount()
@@ -122,9 +120,7 @@ PHP_METHOD(WriteResult, getDeletedCount)
122120 return ;
123121 }
124122
125- if (bson_iter_init_find (& iter , intern -> reply , "nRemoved" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
126- RETURN_LONG (bson_iter_int32 (& iter ));
127- }
123+ RETURN_LONG_FROM_BSON_INT32 (& iter , intern -> reply , "nRemoved" );
128124}
129125/* }}} */
130126/* {{{ proto integer WriteResult::getUpsertedCount()
@@ -142,9 +138,7 @@ PHP_METHOD(WriteResult, getUpsertedCount)
142138 return ;
143139 }
144140
145- if (bson_iter_init_find (& iter , intern -> reply , "nUpserted" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
146- RETURN_LONG (bson_iter_int32 (& iter ));
147- }
141+ RETURN_LONG_FROM_BSON_INT32 (& iter , intern -> reply , "nUpserted" );
148142}
149143/* }}} */
150144/* {{{ proto MongoDB\Driver\Server WriteResult::getServer()
@@ -471,35 +465,19 @@ HashTable *php_phongo_writeresult_get_debug_info(zval *object, int *is_temp TSRM
471465 * is_temp = 1 ;
472466 array_init_size (& retval , 9 );
473467
474- if (bson_iter_init_find (& iter , intern -> reply , "nInserted" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
475- ADD_ASSOC_LONG_EX (& retval , "nInserted" , bson_iter_int32 (& iter ));
476- } else {
477- ADD_ASSOC_NULL_EX (& retval , "nInserted" );
478- }
479-
480- if (bson_iter_init_find (& iter , intern -> reply , "nMatched" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
481- ADD_ASSOC_LONG_EX (& retval , "nMatched" , bson_iter_int32 (& iter ));
482- } else {
483- ADD_ASSOC_NULL_EX (& retval , "nMatched" );
468+ #define SCP (field ) \
469+ if (bson_iter_init_find(&iter, intern->reply, (field)) && BSON_ITER_HOLDS_INT32(&iter)) { \
470+ ADD_ASSOC_LONG_EX(&retval, (field), bson_iter_int32(&iter)); \
471+ } else { \
472+ ADD_ASSOC_NULL_EX(&retval, (field)); \
484473 }
485474
486- if (bson_iter_init_find (& iter , intern -> reply , "nModified" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
487- ADD_ASSOC_LONG_EX (& retval , "nModified" , bson_iter_int32 (& iter ));
488- } else {
489- ADD_ASSOC_NULL_EX (& retval , "nModified" );
490- }
491-
492- if (bson_iter_init_find (& iter , intern -> reply , "nRemoved" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
493- ADD_ASSOC_LONG_EX (& retval , "nRemoved" , bson_iter_int32 (& iter ));
494- } else {
495- ADD_ASSOC_NULL_EX (& retval , "nRemoved" );
496- }
497-
498- if (bson_iter_init_find (& iter , intern -> reply , "nUpserted" ) && BSON_ITER_HOLDS_INT32 (& iter )) {
499- ADD_ASSOC_LONG_EX (& retval , "nUpserted" , bson_iter_int32 (& iter ));
500- } else {
501- ADD_ASSOC_NULL_EX (& retval , "nUpserted" );
502- }
475+ SCP ("nInserted" );
476+ SCP ("nMatched" );
477+ SCP ("nModified" );
478+ SCP ("nRemoved" );
479+ SCP ("nUpserted" );
480+ #undef SCP
503481
504482 if (bson_iter_init_find (& iter , intern -> reply , "upserted" ) && BSON_ITER_HOLDS_ARRAY (& iter )) {
505483 uint32_t len ;
0 commit comments