Skip to content

Commit ac89d43

Browse files
committed
Extract object initialisation from document visitor
1 parent 548c183 commit ac89d43

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

src/phongo_bson.c

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,45 @@ static zval* resolve_enum_case(zend_class_entry* ce, const char* case_name)
817817
}
818818
#endif /* PHP_VERSION_ID >= 80100 */
819819

820+
static bool php_phongo_bson_init_document_object(zval* src, zend_class_entry* obj_ce, zval* obj)
821+
{
822+
#if PHP_VERSION_ID >= 80100
823+
/* Enums require special handling for instantiation */
824+
if (obj_ce->ce_flags & ZEND_ACC_ENUM) {
825+
int plen;
826+
zend_bool pfree;
827+
char* case_name;
828+
zval* enum_case;
829+
830+
case_name = php_array_fetchc_string(src, "name", &plen, &pfree);
831+
832+
if (!case_name) {
833+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Missing 'name' field to infer enum case for %s", ZSTR_VAL(obj_ce->name));
834+
835+
return false;
836+
}
837+
838+
enum_case = resolve_enum_case(obj_ce, case_name);
839+
840+
if (pfree) {
841+
efree(case_name);
842+
}
843+
844+
if (!enum_case) {
845+
return false;
846+
}
847+
848+
ZVAL_COPY(obj, enum_case);
849+
} else {
850+
object_init_ex(obj, obj_ce);
851+
}
852+
#else /* PHP_VERSION_ID < 80100 */
853+
object_init_ex(obj, obj_ce);
854+
#endif /* PHP_VERSION_ID */
855+
856+
return true;
857+
}
858+
820859
static bool php_phongo_bson_visit_document(const bson_iter_t* iter ARG_UNUSED, const char* key, const bson_t* v_document, void* data)
821860
{
822861
zval* retval = PHONGO_BSON_STATE_ZCHILD(data);
@@ -857,47 +896,13 @@ static bool php_phongo_bson_visit_document(const bson_iter_t* iter ARG_UNUSED, c
857896
zval obj;
858897
zend_class_entry* obj_ce = state.odm ? state.odm : state.map.document.class;
859898

860-
#if PHP_VERSION_ID >= 80100
861-
/* Enums require special handling for instantiation */
862-
if (obj_ce->ce_flags & ZEND_ACC_ENUM) {
863-
int plen;
864-
zend_bool pfree;
865-
char* case_name;
866-
zval* enum_case;
867-
868-
case_name = php_array_fetchc_string(&state.zchild, "name", &plen, &pfree);
869-
870-
if (!case_name) {
871-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Missing 'name' field to infer enum case for %s", ZSTR_VAL(obj_ce->name));
872-
873-
/* Clean up and return true to stop iteration for
874-
* our parent context. */
875-
zval_ptr_dtor(&state.zchild);
876-
php_phongo_bson_state_dtor(&state);
877-
return true;
878-
}
879-
880-
enum_case = resolve_enum_case(obj_ce, case_name);
881-
882-
if (pfree) {
883-
efree(case_name);
884-
}
885-
886-
if (!enum_case) {
887-
/* Exception already thrown. Clean up and return
888-
* true to stop iteration for our parent context. */
889-
zval_ptr_dtor(&state.zchild);
890-
php_phongo_bson_state_dtor(&state);
891-
return true;
892-
}
893-
894-
ZVAL_COPY(&obj, enum_case);
895-
} else {
896-
object_init_ex(&obj, obj_ce);
899+
if (!php_phongo_bson_init_document_object(&state.zchild, obj_ce, &obj)) {
900+
/* Exception already thrown. Clean up and return
901+
* true to stop iteration for our parent context. */
902+
zval_ptr_dtor(&state.zchild);
903+
php_phongo_bson_state_dtor(&state);
904+
return true;
897905
}
898-
#else /* PHP_VERSION_ID < 80100 */
899-
object_init_ex(&obj, obj_ce);
900-
#endif /* PHP_VERSION_ID */
901906

902907
zend_call_method_with_1_params(PHONGO_COMPAT_OBJ_P(&obj), NULL, NULL, BSON_UNSERIALIZE_FUNC_NAME, NULL, &state.zchild);
903908
if (((php_phongo_bson_state*) data)->is_visiting_array) {

0 commit comments

Comments
 (0)