Skip to content

Commit 07ebeeb

Browse files
committed
Simplify handling of arrays when encoding BSON
1 parent 8622a86 commit 07ebeeb

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/phongo_bson_encode.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ static int php_phongo_is_array_or_document(zval* val)
5858
int count;
5959

6060
if (Z_TYPE_P(val) != IS_ARRAY) {
61+
if (Z_TYPE_P(val) == IS_OBJECT && instanceof_function(Z_OBJCE_P(val), php_phongo_packedarray_ce)) {
62+
return IS_ARRAY;
63+
}
64+
6165
return IS_OBJECT;
6266
}
6367

@@ -121,7 +125,6 @@ static void php_phongo_bson_append_object(bson_t* bson, php_phongo_field_path* f
121125
if (instanceof_function(Z_OBJCE_P(object), php_phongo_serializable_ce)) {
122126
zval obj_data;
123127
bson_t child;
124-
bool is_array;
125128

126129
zend_call_method_with_0_params(PHONGO_COMPAT_OBJ_P(object), NULL, NULL, BSON_SERIALIZE_FUNC_NAME, &obj_data);
127130

@@ -146,11 +149,9 @@ static void php_phongo_bson_append_object(bson_t* bson, php_phongo_field_path* f
146149
return;
147150
}
148151

149-
is_array = php_phongo_is_array_or_document(&obj_data) == IS_ARRAY || (Z_TYPE(obj_data) == IS_OBJECT && instanceof_function(Z_OBJCE(obj_data), php_phongo_packedarray_ce));
150-
151152
/* Persistable objects must always be serialized as BSON documents;
152153
* otherwise, infer based on bsonSerialize()'s return value. */
153-
if (instanceof_function(Z_OBJCE_P(object), php_phongo_persistable_ce) || !is_array) {
154+
if (instanceof_function(Z_OBJCE_P(object), php_phongo_persistable_ce) || php_phongo_is_array_or_document(&obj_data) != IS_ARRAY) {
154155
bson_append_document_begin(bson, key, key_len, &child);
155156
if (instanceof_function(Z_OBJCE_P(object), php_phongo_persistable_ce)) {
156157
bson_append_binary(&child, PHONGO_ODM_FIELD_NAME, -1, 0x80, (const uint8_t*) Z_OBJCE_P(object)->name->val, Z_OBJCE_P(object)->name->len);

0 commit comments

Comments
 (0)