Skip to content

Commit 225886c

Browse files
committed
Implement JsonSerializable for SplFixedArray
This returns an array for SplFixedArray JSON encoding, which is more appropriate than an object with integer string keys. Implements request https://bugs.php.net/bug.php?id=81112.
1 parent 291d8db commit 225886c

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

ext/spl/php_spl.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,14 @@ PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
735735
return SUCCESS;
736736
} /* }}} */
737737

738-
/* {{{ spl_module_entry */
738+
static const zend_module_dep spl_deps[] = {
739+
ZEND_MOD_REQUIRED("json")
740+
ZEND_MOD_END
741+
};
742+
739743
zend_module_entry spl_module_entry = {
740-
STANDARD_MODULE_HEADER,
744+
STANDARD_MODULE_HEADER_EX, NULL,
745+
spl_deps,
741746
"SPL",
742747
ext_functions,
743748
PHP_MINIT(spl),
@@ -748,4 +753,3 @@ zend_module_entry spl_module_entry = {
748753
PHP_SPL_VERSION,
749754
STANDARD_MODULE_PROPERTIES
750755
};
751-
/* }}} */

ext/spl/spl_fixedarray.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "spl_fixedarray.h"
3232
#include "spl_exceptions.h"
3333
#include "spl_iterators.h"
34+
#include "ext/json/php_json.h"
3435

3536
zend_object_handlers spl_handler_SplFixedArray;
3637
PHPAPI zend_class_entry *spl_ce_SplFixedArray;
@@ -758,6 +759,18 @@ PHP_METHOD(SplFixedArray, getIterator)
758759
zend_create_internal_iterator_zval(return_value, ZEND_THIS);
759760
}
760761

762+
PHP_METHOD(SplFixedArray, jsonSerialize)
763+
{
764+
ZEND_PARSE_PARAMETERS_NONE();
765+
766+
spl_fixedarray_object *intern = Z_SPLFIXEDARRAY_P(ZEND_THIS);
767+
array_init_size(return_value, intern->array.size);
768+
for (zend_long i = 0; i < intern->array.size; i++) {
769+
zend_hash_next_index_insert_new(Z_ARR_P(return_value), &intern->array.elements[i]);
770+
Z_TRY_ADDREF(intern->array.elements[i]);
771+
}
772+
}
773+
761774
static void spl_fixedarray_it_dtor(zend_object_iterator *iter)
762775
{
763776
zval_ptr_dtor(&iter->data);
@@ -838,7 +851,8 @@ zend_object_iterator *spl_fixedarray_get_iterator(zend_class_entry *ce, zval *ob
838851

839852
PHP_MINIT_FUNCTION(spl_fixedarray)
840853
{
841-
spl_ce_SplFixedArray = register_class_SplFixedArray(zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_countable);
854+
spl_ce_SplFixedArray = register_class_SplFixedArray(
855+
zend_ce_aggregate, zend_ce_arrayaccess, zend_ce_countable, php_json_serializable_ce);
842856
spl_ce_SplFixedArray->create_object = spl_fixedarray_new;
843857
spl_ce_SplFixedArray->get_iterator = spl_fixedarray_get_iterator;
844858
spl_ce_SplFixedArray->ce_flags |= ZEND_ACC_REUSE_GET_ITERATOR;

ext/spl/spl_fixedarray.stub.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/** @generate-class-entries */
44

5-
class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable
5+
class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable, JsonSerializable
66
{
77
public function __construct(int $size = 0) {}
88

@@ -49,4 +49,6 @@ public function offsetSet($index, mixed $value) {}
4949
public function offsetUnset($index) {}
5050

5151
public function getIterator(): Iterator {}
52+
53+
public function jsonSerialize(): array {}
5254
}

ext/spl/spl_fixedarray_arginfo.h

Lines changed: 8 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: aeac254f38638c19a11f7d79ac2e5c2d40924e58 */
2+
* Stub hash: 115b2d974b18287654be925c4fdc2236674423eb */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SplFixedArray___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, size, IS_LONG, 0, "0")
@@ -39,6 +39,9 @@ ZEND_END_ARG_INFO()
3939
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_SplFixedArray_getIterator, 0, 0, Iterator, 0)
4040
ZEND_END_ARG_INFO()
4141

42+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_SplFixedArray_jsonSerialize, 0, 0, IS_ARRAY, 0)
43+
ZEND_END_ARG_INFO()
44+
4245

4346
ZEND_METHOD(SplFixedArray, __construct);
4447
ZEND_METHOD(SplFixedArray, __wakeup);
@@ -52,6 +55,7 @@ ZEND_METHOD(SplFixedArray, offsetGet);
5255
ZEND_METHOD(SplFixedArray, offsetSet);
5356
ZEND_METHOD(SplFixedArray, offsetUnset);
5457
ZEND_METHOD(SplFixedArray, getIterator);
58+
ZEND_METHOD(SplFixedArray, jsonSerialize);
5559

5660

5761
static const zend_function_entry class_SplFixedArray_methods[] = {
@@ -67,16 +71,17 @@ static const zend_function_entry class_SplFixedArray_methods[] = {
6771
ZEND_ME(SplFixedArray, offsetSet, arginfo_class_SplFixedArray_offsetSet, ZEND_ACC_PUBLIC)
6872
ZEND_ME(SplFixedArray, offsetUnset, arginfo_class_SplFixedArray_offsetUnset, ZEND_ACC_PUBLIC)
6973
ZEND_ME(SplFixedArray, getIterator, arginfo_class_SplFixedArray_getIterator, ZEND_ACC_PUBLIC)
74+
ZEND_ME(SplFixedArray, jsonSerialize, arginfo_class_SplFixedArray_jsonSerialize, ZEND_ACC_PUBLIC)
7075
ZEND_FE_END
7176
};
7277

73-
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)
78+
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)
7479
{
7580
zend_class_entry ce, *class_entry;
7681

7782
INIT_CLASS_ENTRY(ce, "SplFixedArray", class_SplFixedArray_methods);
7883
class_entry = zend_register_internal_class_ex(&ce, NULL);
79-
zend_class_implements(class_entry, 3, class_entry_IteratorAggregate, class_entry_ArrayAccess, class_entry_Countable);
84+
zend_class_implements(class_entry, 4, class_entry_IteratorAggregate, class_entry_ArrayAccess, class_entry_Countable, class_entry_JsonSerializable);
8085

8186
return class_entry;
8287
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
json_encode() on SplFixedArray
3+
--FILE--
4+
<?php
5+
6+
echo json_encode(new SplFixedArray()) . "\n";
7+
echo json_encode(new SplFixedArray(1)) . "\n";
8+
9+
$a = new SplFixedArray(3);
10+
$a[0] = 0;
11+
$a[2] = 2;
12+
echo json_encode($a) . "\n";
13+
14+
?>
15+
--EXPECT--
16+
[]
17+
[null]
18+
[0,null,2]

0 commit comments

Comments
 (0)