Skip to content

Commit

Permalink
PHPC-1499: Add functions to convert zval to bson_value_t and back
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jan 22, 2020
1 parent 3847154 commit f167da9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions php_bson.h
Expand Up @@ -101,6 +101,8 @@ bool php_phongo_bson_to_zval(const unsigned char* data, int data_len, zval* out)
#else
bool php_phongo_bson_to_zval(const unsigned char* data, int data_len, zval** out);
#endif
bool php_phongo_bson_value_to_zval(const bson_value_t* value, zval* zv);
void php_phongo_zval_to_bson_value(zval* data, php_phongo_bson_flags_t flags, bson_value_t* value TSRMLS_DC);
bool php_phongo_bson_typemap_to_state(zval* typemap, php_phongo_bson_typemap* map TSRMLS_DC);
void php_phongo_bson_state_ctor(php_phongo_bson_state* state);
void php_phongo_bson_state_dtor(php_phongo_bson_state* state);
Expand Down
38 changes: 38 additions & 0 deletions src/bson-encode.c
Expand Up @@ -648,6 +648,44 @@ void php_phongo_zval_to_bson(zval* data, php_phongo_bson_flags_t flags, bson_t*
php_phongo_field_path_free(field_path);
} /* }}} */

/* Converts the argument to a bson_value_t. If the object is an instance of
* MongoDB\BSON\Serializable, the return value of bsonSerialize() will be
* used. */
void php_phongo_zval_to_bson_value(zval* data, php_phongo_bson_flags_t flags, bson_value_t* value TSRMLS_DC) /* {{{ */
{
bson_iter_t iter;
bson_t bson = BSON_INITIALIZER;

#if PHP_VERSION_ID >= 70000
zval* data_object = ecalloc(sizeof(zval), 1);
#else
zval* data_object = NULL;
ALLOC_INIT_ZVAL(data_object);
#endif

array_init_size(data_object, 1);
add_assoc_zval(data_object, "data", data);

#if PHP_VERSION_ID >= 70000
Z_TRY_ADDREF_P(data);
#else
Z_ADDREF_P(data);
#endif

php_phongo_zval_to_bson(data_object, flags, &bson, NULL TSRMLS_CC);

if (bson_iter_init_find(&iter, &bson, "data")) {
bson_value_copy(bson_iter_value(&iter), value);
}

#if PHP_VERSION_ID >= 70000
zval_ptr_dtor(data_object);
efree(data_object);
#else
zval_ptr_dtor(&data_object);
#endif
} /* }}} */

/*
* Local variables:
* tab-width: 4
Expand Down
34 changes: 34 additions & 0 deletions src/bson.c
Expand Up @@ -1221,6 +1221,40 @@ bool php_phongo_bson_to_zval(const unsigned char* data, int data_len, zval** zv)
return retval;
} /* }}} */

/* Converts a BSON value to a ZVAL. */
bool php_phongo_bson_value_to_zval(const bson_value_t* value, zval* zv) /* {{{ */
{
bson_t bson = BSON_INITIALIZER;
php_phongo_bson_state state;
zval* return_value;
bool retval = false;

PHONGO_BSON_INIT_STATE(state);
state.map.root_type = PHONGO_TYPEMAP_NATIVE_ARRAY;

bson_append_value(&bson, "data", 4, value);
if (!php_phongo_bson_to_zval_ex(bson_get_data(&bson), bson.len, &state)) {
/* Exception already thrown */
goto cleanup;
}

retval = true;

#if PHP_VERSION_ID >= 70000
return_value = php_array_fetchc(&state.zchild, "data");
#else
return_value = php_array_fetchc(state.zchild, "data");
#endif

if (return_value) {
ZVAL_ZVAL(zv, return_value, 1, 0);
}

cleanup:
zval_ptr_dtor(&state.zchild);
return retval;
} /* }}} */

/* Converts a BSON document to a PHP value according to the typemap specified in
* the state argument.
*
Expand Down

0 comments on commit f167da9

Please sign in to comment.