Skip to content

Commit e1c09b0

Browse files
authored
PHPC-1000: Allow bsonSerialize to return BSON structure (#1449)
* Document and PackedArray classes implement Type interface * Narrow return type for Serializable::bsonSerialize The previously indicated return type was incorrect, as returning an object other than stdClass would result in an error. * Add compat macro for ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_TYPE_MASK_EX * PHPC-1000: Allow bsonSerialize to return BSON structures * Narrow return type of bsonSerialize implementations * Split BSON structure checks for legibility * Added todo to remove obsolete type check * Simplify handling of arrays when encoding BSON * Prevent Persistable implementations to return PackedArray instances * Extract utility function to check return types of bsonSerialize()
1 parent d2a600b commit e1c09b0

24 files changed

+173
-162
lines changed

src/BSON/Document.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ static HashTable* php_phongo_document_get_properties(phongo_compat_object_handle
473473

474474
void php_phongo_document_init_ce(INIT_FUNC_ARGS)
475475
{
476-
php_phongo_document_ce = register_class_MongoDB_BSON_Document(zend_ce_aggregate, zend_ce_serializable);
476+
php_phongo_document_ce = register_class_MongoDB_BSON_Document(zend_ce_aggregate, zend_ce_serializable, php_phongo_type_ce);
477477
php_phongo_document_ce->create_object = php_phongo_document_create_object;
478478

479479
#if PHP_VERSION_ID >= 80000

src/BSON/Document.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace MongoDB\BSON;
99

10-
final class Document implements \IteratorAggregate, \Serializable
10+
final class Document implements \IteratorAggregate, \Serializable, Type
1111
{
1212
private function __construct() {}
1313

src/BSON/Document_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: e00ccf66afed0f51040527ee5ee24513f0c1f495 */
2+
* Stub hash: 703e15f17b01dd2b6f04cb89c080ba83a5a420d0 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_BSON_Document___construct, 0, 0, 0)
55
ZEND_END_ARG_INFO()
@@ -166,14 +166,14 @@ static const zend_function_entry class_MongoDB_BSON_Document_methods[] = {
166166
ZEND_FE_END
167167
};
168168

169-
static zend_class_entry *register_class_MongoDB_BSON_Document(zend_class_entry *class_entry_IteratorAggregate, zend_class_entry *class_entry_Serializable)
169+
static zend_class_entry *register_class_MongoDB_BSON_Document(zend_class_entry *class_entry_IteratorAggregate, zend_class_entry *class_entry_Serializable, zend_class_entry *class_entry_MongoDB_BSON_Type)
170170
{
171171
zend_class_entry ce, *class_entry;
172172

173173
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\BSON", "Document", class_MongoDB_BSON_Document_methods);
174174
class_entry = zend_register_internal_class_ex(&ce, NULL);
175175
class_entry->ce_flags |= ZEND_ACC_FINAL;
176-
zend_class_implements(class_entry, 2, class_entry_IteratorAggregate, class_entry_Serializable);
176+
zend_class_implements(class_entry, 3, class_entry_IteratorAggregate, class_entry_Serializable, class_entry_MongoDB_BSON_Type);
177177

178178
return class_entry;
179179
}

src/BSON/PackedArray.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ static HashTable* php_phongo_packedarray_get_properties(phongo_compat_object_han
403403

404404
void php_phongo_packedarray_init_ce(INIT_FUNC_ARGS)
405405
{
406-
php_phongo_packedarray_ce = register_class_MongoDB_BSON_PackedArray(zend_ce_aggregate, zend_ce_serializable);
406+
php_phongo_packedarray_ce = register_class_MongoDB_BSON_PackedArray(zend_ce_aggregate, zend_ce_serializable, php_phongo_type_ce);
407407
php_phongo_packedarray_ce->create_object = php_phongo_packedarray_create_object;
408408

409409
#if PHP_VERSION_ID >= 80000

src/BSON/PackedArray.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace MongoDB\BSON;
99

10-
final class PackedArray implements \IteratorAggregate, \Serializable
10+
final class PackedArray implements \IteratorAggregate, \Serializable, Type
1111
{
1212
private function __construct() {}
1313

src/BSON/PackedArray_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: e9b57c4051440170531560cc355e0af1b43f3424 */
2+
* Stub hash: f9813432d7811e068f62a4b34d86919a52f63984 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_BSON_PackedArray___construct, 0, 0, 0)
55
ZEND_END_ARG_INFO()
@@ -128,14 +128,14 @@ static const zend_function_entry class_MongoDB_BSON_PackedArray_methods[] = {
128128
ZEND_FE_END
129129
};
130130

131-
static zend_class_entry *register_class_MongoDB_BSON_PackedArray(zend_class_entry *class_entry_IteratorAggregate, zend_class_entry *class_entry_Serializable)
131+
static zend_class_entry *register_class_MongoDB_BSON_PackedArray(zend_class_entry *class_entry_IteratorAggregate, zend_class_entry *class_entry_Serializable, zend_class_entry *class_entry_MongoDB_BSON_Type)
132132
{
133133
zend_class_entry ce, *class_entry;
134134

135135
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\BSON", "PackedArray", class_MongoDB_BSON_PackedArray_methods);
136136
class_entry = zend_register_internal_class_ex(&ce, NULL);
137137
class_entry->ce_flags |= ZEND_ACC_FINAL;
138-
zend_class_implements(class_entry, 2, class_entry_IteratorAggregate, class_entry_Serializable);
138+
zend_class_implements(class_entry, 3, class_entry_IteratorAggregate, class_entry_Serializable, class_entry_MongoDB_BSON_Type);
139139

140140
return class_entry;
141141
}

src/BSON/Persistable.stub.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@
99

1010
interface Persistable extends Serializable, Unserializable
1111
{
12+
#if PHP_VERSION_ID >= 80000
13+
/** @tentative-return-type */
14+
public function bsonSerialize(): array|\stdClass|Document;
15+
#else
16+
/** @return array|\stdClass|Document */
17+
public function bsonSerialize();
18+
#endif
1219
}

src/BSON/Persistable_arginfo.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: babd07f95f47c3b66228ef23b66ab0446cd5c308 */
2+
* Stub hash: e61c06a90093af5468c6c29f6cbf16c5db8d54d1 */
33

4+
#if PHP_VERSION_ID >= 80000
5+
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_MongoDB_BSON_Persistable_bsonSerialize, 0, 0, stdClass|MongoDB\\BSON\\Document, MAY_BE_ARRAY)
6+
ZEND_END_ARG_INFO()
7+
#endif
48

9+
#if !(PHP_VERSION_ID >= 80000)
10+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_BSON_Persistable_bsonSerialize, 0, 0, 0)
11+
ZEND_END_ARG_INFO()
12+
#endif
13+
14+
15+
#if PHP_VERSION_ID >= 80000
16+
#endif
17+
#if !(PHP_VERSION_ID >= 80000)
18+
#endif
519

620

721
static const zend_function_entry class_MongoDB_BSON_Persistable_methods[] = {
22+
#if PHP_VERSION_ID >= 80000
23+
ZEND_ABSTRACT_ME_WITH_FLAGS(MongoDB_BSON_Persistable, bsonSerialize, arginfo_class_MongoDB_BSON_Persistable_bsonSerialize, ZEND_ACC_PUBLIC|ZEND_ACC_ABSTRACT)
24+
#endif
25+
#if !(PHP_VERSION_ID >= 80000)
26+
ZEND_ABSTRACT_ME_WITH_FLAGS(MongoDB_BSON_Persistable, bsonSerialize, arginfo_class_MongoDB_BSON_Persistable_bsonSerialize, ZEND_ACC_PUBLIC|ZEND_ACC_ABSTRACT)
27+
#endif
828
ZEND_FE_END
929
};
1030

src/BSON/Serializable.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ interface Serializable extends Type
1111
{
1212
#if PHP_VERSION_ID >= 80000
1313
/** @tentative-return-type */
14-
public function bsonSerialize(): array|object;
14+
public function bsonSerialize(): array|\stdClass|Document|PackedArray;
1515
#else
16-
/** @return array|object */
16+
/** @return array|\stdClass|Document|PackedArray */
1717
public function bsonSerialize();
1818
#endif
1919
}

src/BSON/Serializable_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: cbb517e1d922625ff18bdc81620686a44a87e85c */
2+
* Stub hash: 92732bafa65af8a12b291f7bcb52b069c45724a7 */
33

44
#if PHP_VERSION_ID >= 80000
5-
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_MASK_EX(arginfo_class_MongoDB_BSON_Serializable_bsonSerialize, 0, 0, MAY_BE_ARRAY|MAY_BE_OBJECT)
5+
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_MongoDB_BSON_Serializable_bsonSerialize, 0, 0, stdClass|MongoDB\\BSON\\Document|MongoDB\\BSON\\PackedArray, MAY_BE_ARRAY)
66
ZEND_END_ARG_INFO()
77
#endif
88

0 commit comments

Comments
 (0)