-
Type: Task
-
Resolution: Unresolved
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
php_phongo_zval_to_bson_internal is the common function for converting a zval to a bson_t and is called in several contexts:
- php_phongo_bson_append uses it to encode an indexed PHP array
- php_phongo_bson_append_object uses it for encoding the return value of bsonSerialize() (both Serializable and Persistable) and also for generic objects (i.e. not instances of MongoDB\BSON\Type)
- php_phongo_zval_to_bson and php_phongo_zval_to_bson_value use it to encode root documents
If we contrast php_phongo_zval_to_bson_internal with php_phongo_bson_append_object, we can see that both functions include handling for Serializable instances and invoking bsonSerialize(). I believe this behavior dates back to very early (pre-1.0) versions of the driver and may have been introduced by PHPC-274. Note that zval_to_bson and object_to_bson were early counterparts to our current php_phongo_zval_to_bson_internal and php_phongo_bson_append_object functions, respectively. In 9e59cee for PHPC-274, Serializable handling was added to zval_to_bson. At the time, object_to_bson delegated to zval_to_bson (as their modern counterparts still do today).
This seems like a refactoring opportunity.
Note: I originally assumed that there was a bug here with bsonSerialize() being invoked twice for embedded documents. That is not the case, as the first invocation in php_phongo_bson_append_object ends up passing bsonSerialize()'s return value to php_phongo_zval_to_bson. It also appears that the handling in php_phongo_bson_append_object is not entirely redundant, (and thus a candidate for simple removal), due to a subtle difference between both functions: php_phongo_bson_append_object determines whether to append the bson_t as an array or object while php_phongo_zval_to_bson_internal only concerns itself with populating a bson_t.
- is related to
-
PHPC-274 zval_to_bson() ignores BSON\Serializable interface
- Closed