diff --git a/ext/spl/config.m4 b/ext/spl/config.m4 index 6e6973646db84..23a2b0ae2a65d 100644 --- a/ext/spl/config.m4 +++ b/ext/spl/config.m4 @@ -2,3 +2,4 @@ PHP_NEW_EXTENSION(spl, php_spl.c spl_functions.c spl_engine.c spl_iterators.c sp PHP_INSTALL_HEADERS([ext/spl], [php_spl.h spl_array.h spl_directory.h spl_engine.h spl_exceptions.h spl_functions.h spl_iterators.h spl_observer.h spl_dllist.h spl_heap.h spl_fixedarray.h]) PHP_ADD_EXTENSION_DEP(spl, pcre, true) PHP_ADD_EXTENSION_DEP(spl, standard, true) +PHP_ADD_EXTENSION_DEP(spl, json) diff --git a/ext/spl/php_spl.c b/ext/spl/php_spl.c index f39da84d50a12..5aac0a4ddabe3 100644 --- a/ext/spl/php_spl.c +++ b/ext/spl/php_spl.c @@ -735,9 +735,14 @@ PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */ return SUCCESS; } /* }}} */ -/* {{{ spl_module_entry */ +static const zend_module_dep spl_deps[] = { + ZEND_MOD_REQUIRED("json") + ZEND_MOD_END +}; + zend_module_entry spl_module_entry = { - STANDARD_MODULE_HEADER, + STANDARD_MODULE_HEADER_EX, NULL, + spl_deps, "SPL", ext_functions, PHP_MINIT(spl), @@ -748,4 +753,3 @@ zend_module_entry spl_module_entry = { PHP_SPL_VERSION, STANDARD_MODULE_PROPERTIES }; -/* }}} */ diff --git a/ext/spl/spl_fixedarray.c b/ext/spl/spl_fixedarray.c index 364f38a9066d8..66e7b16cb832d 100644 --- a/ext/spl/spl_fixedarray.c +++ b/ext/spl/spl_fixedarray.c @@ -31,6 +31,7 @@ #include "spl_fixedarray.h" #include "spl_exceptions.h" #include "spl_iterators.h" +#include "ext/json/php_json.h" zend_object_handlers spl_handler_SplFixedArray; PHPAPI zend_class_entry *spl_ce_SplFixedArray; @@ -758,6 +759,18 @@ PHP_METHOD(SplFixedArray, getIterator) zend_create_internal_iterator_zval(return_value, ZEND_THIS); } +PHP_METHOD(SplFixedArray, jsonSerialize) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS); + array_init_size(return_value, intern->array.size); + for (zend_long i = 0; i < intern->array.size; i++) { + zend_hash_next_index_insert_new(Z_ARR_P(return_value), &intern->array.elements[i]); + Z_TRY_ADDREF(intern->array.elements[i]); + } +} + static void spl_fixedarray_it_dtor(zend_object_iterator *iter) { zval_ptr_dtor(&iter->data); @@ -838,7 +851,8 @@ zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *ob PHP_MINIT_FUNCTION(spl_fixedarray) { - spl_ce_SplFixedArray = register_class_SplFixedArray(zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_countable); + spl_ce_SplFixedArray = register_class_SplFixedArray( + zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_countable, php_json_serializable_ce); spl_ce_SplFixedArray->create_object = spl_fixedarray_new; spl_ce_SplFixedArray->get_iterator = spl_fixedarray_get_iterator; spl_ce_SplFixedArray->ce_flags |= ZEND_ACC_REUSE_GET_ITERATOR; diff --git a/ext/spl/spl_fixedarray.stub.php b/ext/spl/spl_fixedarray.stub.php index 32489d719911a..790a1df3e373a 100644 --- a/ext/spl/spl_fixedarray.stub.php +++ b/ext/spl/spl_fixedarray.stub.php @@ -2,7 +2,7 @@ /** @generate-class-entries */ -class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable +class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable, JsonSerializable { public function __construct(int $size = 0) {} @@ -49,4 +49,6 @@ public function offsetSet($index, mixed $value) {} public function offsetUnset($index) {} public function getIterator(): Iterator {} + + public function jsonSerialize(): array {} } diff --git a/ext/spl/spl_fixedarray_arginfo.h b/ext/spl/spl_fixedarray_arginfo.h index 627400cb4dd0b..eaec9f1fc33e2 100644 --- a/ext/spl/spl_fixedarray_arginfo.h +++ b/ext/spl/spl_fixedarray_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: aeac254f38638c19a11f7d79ac2e5c2d40924e58 */ + * Stub hash: 115b2d974b18287654be925c4fdc2236674423eb */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SplFixedArray___construct, 0, 0, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, size, IS_LONG, 0, "0") @@ -39,6 +39,9 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_SplFixedArray_getIterator, 0, 0, Iterator, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_SplFixedArray_jsonSerialize, 0, 0, IS_ARRAY, 0) +ZEND_END_ARG_INFO() + ZEND_METHOD(SplFixedArray, __construct); ZEND_METHOD(SplFixedArray, __wakeup); @@ -52,6 +55,7 @@ ZEND_METHOD(SplFixedArray, offsetGet); ZEND_METHOD(SplFixedArray, offsetSet); ZEND_METHOD(SplFixedArray, offsetUnset); ZEND_METHOD(SplFixedArray, getIterator); +ZEND_METHOD(SplFixedArray, jsonSerialize); static const zend_function_entry class_SplFixedArray_methods[] = { @@ -67,16 +71,17 @@ static const zend_function_entry class_SplFixedArray_methods[] = { ZEND_ME(SplFixedArray, offsetSet, arginfo_class_SplFixedArray_offsetSet, ZEND_ACC_PUBLIC) ZEND_ME(SplFixedArray, offsetUnset, arginfo_class_SplFixedArray_offsetUnset, ZEND_ACC_PUBLIC) ZEND_ME(SplFixedArray, getIterator, arginfo_class_SplFixedArray_getIterator, ZEND_ACC_PUBLIC) + ZEND_ME(SplFixedArray, jsonSerialize, arginfo_class_SplFixedArray_jsonSerialize, ZEND_ACC_PUBLIC) ZEND_FE_END }; -static zend_class_entry *register_class_SplFixedArray(zend_class_entry *class_entry_IteratorAggregate, zend_class_entry *class_entry_ArrayAccess, zend_class_entry *class_entry_Countable) +static zend_class_entry *register_class_SplFixedArray(zend_class_entry *class_entry_IteratorAggregate, zend_class_entry *class_entry_ArrayAccess, zend_class_entry *class_entry_Countable, zend_class_entry *class_entry_JsonSerializable) { zend_class_entry ce, *class_entry; INIT_CLASS_ENTRY(ce, "SplFixedArray", class_SplFixedArray_methods); class_entry = zend_register_internal_class_ex(&ce, NULL); - zend_class_implements(class_entry, 3, class_entry_IteratorAggregate, class_entry_ArrayAccess, class_entry_Countable); + zend_class_implements(class_entry, 4, class_entry_IteratorAggregate, class_entry_ArrayAccess, class_entry_Countable, class_entry_JsonSerializable); return class_entry; } diff --git a/ext/spl/tests/splfixedarray_json_encode.phpt b/ext/spl/tests/splfixedarray_json_encode.phpt new file mode 100644 index 0000000000000..5f53b082abe89 --- /dev/null +++ b/ext/spl/tests/splfixedarray_json_encode.phpt @@ -0,0 +1,18 @@ +--TEST-- +json_encode() on SplFixedArray +--FILE-- + +--EXPECT-- +[] +[null] +[0,null,2]