From 164929d5571e88dd182a72f1f325339673c79599 Mon Sep 17 00:00:00 2001 From: Marcus Boerger Date: Mon, 20 Nov 2006 19:58:01 +0000 Subject: [PATCH] - MFH: Add new test --- ext/spl/php_spl.c | 8 +++++++- ext/spl/spl.php | 3 ++- ext/spl/spl_iterators.c | 25 ++++++++++++++++++----- ext/spl/tests/spl_006.phpt | 41 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100755 ext/spl/tests/spl_006.phpt diff --git a/ext/spl/php_spl.c b/ext/spl/php_spl.c index 95a8a032ee..abfdd97d77 100755 --- a/ext/spl/php_spl.c +++ b/ext/spl/php_spl.c @@ -646,6 +646,12 @@ PHP_MINFO_FUNCTION(spl) } /* }}} */ +static +ZEND_BEGIN_ARG_INFO(arginfo_iterator_to_array, 0) + ZEND_ARG_OBJ_INFO(0, iterator, Traversable, 0) + ZEND_ARG_INFO(0, use_keys) +ZEND_END_ARG_INFO(); + static ZEND_BEGIN_ARG_INFO(arginfo_iterator, 0) ZEND_ARG_OBJ_INFO(0, iterator, Traversable, 0) @@ -672,7 +678,7 @@ zend_function_entry spl_functions[] = { PHP_FE(class_implements, NULL) PHP_FE(spl_object_hash, NULL) #ifdef SPL_ITERATORS_H - PHP_FE(iterator_to_array, arginfo_iterator) + PHP_FE(iterator_to_array, arginfo_iterator_to_array) PHP_FE(iterator_count, arginfo_iterator) PHP_FE(iterator_apply, arginfo_iterator_apply) #endif /* SPL_ITERATORS_H */ diff --git a/ext/spl/spl.php b/ext/spl/spl.php index 2987024f26..9a850075e0 100755 --- a/ext/spl/spl.php +++ b/ext/spl/spl.php @@ -229,9 +229,10 @@ function iterator_count(Traversable $it) {/**/}; * @since PHP 5.1 * * @param it iterator to copy + * @param use_keys whether touse the keys * @return array with elements copied from the iterator */ -function iterator_to_array(Traversable $it) {/**/}; +function iterator_to_array(Traversable $it, $use_keys = true) {/**/}; /** @ingroup ZendEngine * @brief Basic Exception class. diff --git a/ext/spl/spl_iterators.c b/ext/spl/spl_iterators.c index 97e59d3c74..4054160d0d 100755 --- a/ext/spl/spl_iterators.c +++ b/ext/spl/spl_iterators.c @@ -2734,23 +2734,38 @@ static int spl_iterator_to_array_apply(zend_object_iterator *iter, void *puser T } /* }}} */ -/* {{{ proto array iterator_to_array(Traversable it) +static int spl_iterator_to_values_apply(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{ */ +{ + zval **data, *return_value = (zval*)puser; + + iter->funcs->get_current_data(iter, &data TSRMLS_CC); + if (EG(exception)) { + return ZEND_HASH_APPLY_STOP; + } + (*data)->refcount++; + add_next_index_zval(return_value, *data); + return ZEND_HASH_APPLY_KEEP; +} +/* }}} */ + +/* {{{ proto array iterator_to_array(Traversable it [, bool use_keys = true]) Copy the iterator into an array */ PHP_FUNCTION(iterator_to_array) { zval *obj; + zend_bool use_keys = 1; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, zend_ce_traversable) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &obj, zend_ce_traversable, &use_keys) == FAILURE) { RETURN_FALSE; } array_init(return_value); - - if (spl_iterator_apply(obj, spl_iterator_to_array_apply, (void*)return_value TSRMLS_CC) != SUCCESS) { + + if (spl_iterator_apply(obj, use_keys ? spl_iterator_to_array_apply : spl_iterator_to_values_apply, (void*)return_value TSRMLS_CC) != SUCCESS) { zval_dtor(return_value); RETURN_NULL(); } -} +} /* }}} */ static int spl_iterator_count_apply(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{ */ { diff --git a/ext/spl/tests/spl_006.phpt b/ext/spl/tests/spl_006.phpt new file mode 100755 index 0000000000..89859624db --- /dev/null +++ b/ext/spl/tests/spl_006.phpt @@ -0,0 +1,41 @@ +--TEST-- +SPL: iterator_to_array() without keys +--SKIPIF-- + +--FILE-- +append(new ArrayIterator(array(1,2))); +$it->append(new ArrayIterator(array(2,3))); + +var_dump(iterator_to_array($it)); +var_dump(iterator_to_array($it, false)); +var_dump(iterator_to_array($it, true)); + +?> +===DONE=== +--EXPECT-- +array(2) { + [0]=> + int(2) + [1]=> + int(3) +} +array(4) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(2) + [3]=> + int(3) +} +array(2) { + [0]=> + int(2) + [1]=> + int(3) +} +===DONE=== -- 2.40.0