Skip to content

Commit f267ede

Browse files
bjoriderickr
authored andcommitted
Simpilfy iterating over a zval
1 parent f26b44f commit f267ede

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/bson.c

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,45 +1068,56 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
10681068
return;
10691069
}
10701070

1071+
#if PHP_VERSION_ID >= 70000
1072+
{
1073+
zend_string *key;
1074+
zval *value;
1075+
1076+
ZEND_HASH_FOREACH_STR_KEY_VAL(ht_data, key, value) {
1077+
if (key) {
1078+
if (Z_TYPE_P(data) == IS_OBJECT) {
1079+
const char *skey;
1080+
unsigned int skey_len = 0;
1081+
const char *class_name;
1082+
zend_unmangle_property_name(key, &class_name, &skey);
1083+
1084+
if (flags & PHONGO_BSON_ADD_ID) {
1085+
if (!strncmp(skey, "_id", sizeof("_id")-1)) {
1086+
flags &= ~PHONGO_BSON_ADD_ID;
1087+
}
1088+
}
1089+
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, skey, skey_len, Z_TYPE_P(value), value TSRMLS_CC);
1090+
} else {
1091+
/* Chop off the \0 from string lengths */
1092+
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, key->val, key->len-1, Z_TYPE_P(value), value TSRMLS_CC);
1093+
}
1094+
}
1095+
} ZEND_HASH_FOREACH_END();
1096+
}
1097+
#else
10711098
zend_hash_internal_pointer_reset_ex(ht_data, &pos);
10721099
for (;; zend_hash_move_forward_ex(ht_data, &pos)) {
10731100
unsigned int key_len = 0;
10741101
uint64_t index = 0;
10751102
char numbuf[32];
10761103
char *key = NULL;
1077-
#if PHP_VERSION_ID >= 70000
1078-
zend_string *zs_key;
1079-
zval *entry;
1080-
#else
10811104
zval **entry;
1082-
#endif
10831105
int hash_type = HASH_KEY_NON_EXISTENT;
1084-
#if PHP_VERSION_ID >= 70000
1085-
#else
1086-
hash_type = zend_hash_get_current_key_ex(ht_data, &zs_key, &index, 0, &pos);
1087-
#endif
1106+
1107+
hash_type = zend_hash_get_current_key_ex(ht_data, &key, &key_len, &index, 0, &pos);
10881108

10891109
if (hash_type == HASH_KEY_NON_EXISTENT) {
10901110
break;
10911111
}
10921112

1093-
#if PHP_VERSION_ID >= 70000
1094-
if ((entry = zend_hash_get_current_data_ex(ht_data, &pos)) != NULL) {
1095-
#else
10961113
if (zend_hash_get_current_data_ex(ht_data, (void **) &entry, &pos) == FAILURE) {
1097-
#endif
10981114
break;
10991115
}
11001116

11011117
if (hash_type == HASH_KEY_IS_STRING) {
11021118
if (ht_data_from_properties) {
11031119
const char *class_name;
1104-
#if PHP_VERSION_ID >= 70000
1105-
zend_unmangle_property_name(zs_key, &class_name, (const char **)&key);
1106-
zend_string_free(zs_key);
1107-
#else
11081120
zend_unmangle_property_name(key, key_len-1, &class_name, (const char **)&key);
1109-
#endif
11101121
key_len = strlen(key);
11111122

11121123
/* Ignore non-public properties */
@@ -1126,12 +1137,9 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
11261137
} else {
11271138
key_len = bson_uint32_to_string(index, (const char **)&key, numbuf, sizeof(numbuf));
11281139
}
1129-
#if PHP_VERSION_ID >= 70000
1130-
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, key, key_len, Z_TYPE_P(entry), entry TSRMLS_CC);
1131-
#else
11321140
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, key, key_len, Z_TYPE_PP(entry), *entry TSRMLS_CC);
1133-
#endif
11341141
}
1142+
#endif
11351143

11361144
if (flags & PHONGO_BSON_ADD_ID) {
11371145
bson_oid_t oid;

0 commit comments

Comments
 (0)