Skip to content

Commit 4549f8c

Browse files
committed
PHPC-1622: Use get_properties_for handler in ObjectId (prototype)
Replace the get_debug_info and get_properties handlers with a single get_properties_for handler that dispatches by purpose: - ZEND_PROP_PURPOSE_DEBUG, ARRAY_CAST, GET_OBJECT_VARS, VAR_EXPORT: return a fresh temporary HashTable (no caching) - ZEND_PROP_PURPOSE_SERIALIZE, JSON: return NULL, deferring to the class's __serialize/__unserialize and JsonSerializable implementations The get_properties handler is retained since foreach on non-Traversable objects calls it directly, bypassing get_properties_for on all PHP versions. ZEND_PROP_PURPOSE_GET_OBJECT_VARS (added in PHP 8.4 as part of the Property Hooks RFC) is guarded with #if PHP_VERSION_ID >= 80400; on PHP 8.1-8.3 get_object_vars() reaches get_properties directly. Add tests for the two previously untested purposes: - bson-objectid-array_cast-001.phpt (ZEND_PROP_PURPOSE_ARRAY_CAST) - bson-objectid-var_export-001.phpt (ZEND_PROP_PURPOSE_VAR_EXPORT)
1 parent bb72111 commit 4549f8c

3 files changed

Lines changed: 50 additions & 5 deletions

File tree

src/BSON/ObjectId.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,19 @@ static int phongo_objectid_compare_objects(zval* o1, zval* o2)
252252
return strcmp(intern1->oid, intern2->oid);
253253
}
254254

255-
static HashTable* phongo_objectid_get_debug_info(zend_object* object, int* is_temp)
255+
static HashTable* phongo_objectid_get_properties_for(zend_object* object, zend_prop_purpose purpose)
256256
{
257-
*is_temp = 1;
258-
return phongo_objectid_get_properties_hash(object, true);
257+
switch (purpose) {
258+
case ZEND_PROP_PURPOSE_DEBUG:
259+
case ZEND_PROP_PURPOSE_ARRAY_CAST:
260+
#if PHP_VERSION_ID >= 80400
261+
case ZEND_PROP_PURPOSE_GET_OBJECT_VARS:
262+
#endif
263+
case ZEND_PROP_PURPOSE_VAR_EXPORT:
264+
return phongo_objectid_get_properties_hash(object, true);
265+
default:
266+
return NULL;
267+
}
259268
}
260269

261270
static HashTable* phongo_objectid_get_properties(zend_object* object)
@@ -271,8 +280,8 @@ void phongo_objectid_init_ce(INIT_FUNC_ARGS)
271280
memcpy(&phongo_handler_objectid, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
272281
phongo_handler_objectid.compare = phongo_objectid_compare_objects;
273282
phongo_handler_objectid.clone_obj = phongo_objectid_clone_object;
274-
phongo_handler_objectid.get_debug_info = phongo_objectid_get_debug_info;
275-
phongo_handler_objectid.get_properties = phongo_objectid_get_properties;
283+
phongo_handler_objectid.get_properties_for = phongo_objectid_get_properties_for;
284+
phongo_handler_objectid.get_properties = phongo_objectid_get_properties;
276285
phongo_handler_objectid.free_obj = phongo_objectid_free_object;
277286
phongo_handler_objectid.offset = XtOffsetOf(phongo_objectid_t, std);
278287
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\BSON\ObjectId array cast (ZEND_PROP_PURPOSE_ARRAY_CAST)
3+
--FILE--
4+
<?php
5+
6+
$oid = new MongoDB\BSON\ObjectId('53e2a1c40640fd72175d4603');
7+
8+
var_dump((array) $oid);
9+
10+
?>
11+
===DONE===
12+
<?php exit(0); ?>
13+
--EXPECT--
14+
array(1) {
15+
["oid"]=>
16+
string(24) "53e2a1c40640fd72175d4603"
17+
}
18+
===DONE===
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\BSON\ObjectId var_export() (ZEND_PROP_PURPOSE_VAR_EXPORT)
3+
--FILE--
4+
<?php
5+
6+
$oid = new MongoDB\BSON\ObjectId('53e2a1c40640fd72175d4603');
7+
8+
var_export($oid);
9+
echo "\n";
10+
11+
?>
12+
===DONE===
13+
<?php exit(0); ?>
14+
--EXPECTF--
15+
%r\\?%rMongoDB\BSON\ObjectId::__set_state(array(
16+
'oid' => '53e2a1c40640fd72175d4603',
17+
))
18+
===DONE===

0 commit comments

Comments
 (0)