From dbf6fc6ecd00a5e80b93072a48befc0752362088 Mon Sep 17 00:00:00 2001 From: CismonX Date: Wed, 9 May 2018 21:00:18 +0800 Subject: [PATCH] Add `onEach()` and `forEach()`. Remove `orEmpty()`. --- src/collections_me.c | 1 - src/collections_methods.c | 28 +++++++++++++++++++++------- src/php_collections_me.h | 1 - stubs/Collection.php | 7 ------- tests/030-foreach-oneach.phpt | 21 +++++++++++++++++++++ 5 files changed, 42 insertions(+), 16 deletions(-) create mode 100644 tests/030-foreach-oneach.phpt diff --git a/src/collections_me.c b/src/collections_me.c index 5733cbb..72d46dc 100644 --- a/src/collections_me.c +++ b/src/collections_me.c @@ -186,7 +186,6 @@ const zend_function_entry collection_methods[] = { PHP_ME(Collection, minusValuesAssign, elements_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Collection, none, predicate_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Collection, onEach, action_arginfo, ZEND_ACC_PUBLIC) - PHP_ME(Collection, orEmpty, NULL, ZEND_ACC_PUBLIC) PHP_ME(Collection, partition, predicate_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Collection, plus, elements_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Collection, plusAssign, elements_arginfo, ZEND_ACC_PUBLIC) diff --git a/src/collections_methods.c b/src/collections_methods.c index 1de6f44..8a394b8 100644 --- a/src/collections_methods.c +++ b/src/collections_methods.c @@ -804,7 +804,16 @@ PHP_METHOD(Collection, foldRight) PHP_METHOD(Collection, forEach) { - + zend_fcall_info fci; + zend_fcall_info_cache fcc; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_FUNC(fci, fcc) + ZEND_PARSE_PARAMETERS_END(); + INIT_FCI(2); + zend_array* current = COLLECTION_FETCH_CURRENT(); + ZEND_HASH_FOREACH_BUCKET(current, Bucket* bucket) + CALLBACK_KEYVAL_INVOKE(params, bucket); + ZEND_HASH_FOREACH_END(); } PHP_METHOD(Collection, get) @@ -1092,12 +1101,17 @@ PHP_METHOD(Collection, none) PHP_METHOD(Collection, onEach) { - -} - -PHP_METHOD(Collection, orEmpty) -{ - + zend_fcall_info fci; + zend_fcall_info_cache fcc; + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_FUNC(fci, fcc) + ZEND_PARSE_PARAMETERS_END(); + INIT_FCI(2); + zend_array* current = COLLECTION_FETCH_CURRENT(); + ZEND_HASH_FOREACH_BUCKET(current, Bucket* bucket) + CALLBACK_KEYVAL_INVOKE(params, bucket); + ZEND_HASH_FOREACH_END(); + RETVAL_ZVAL(getThis(), 1, 0); } PHP_METHOD(Collection, partition) diff --git a/src/php_collections_me.h b/src/php_collections_me.h index eae7d0c..bb85252 100644 --- a/src/php_collections_me.h +++ b/src/php_collections_me.h @@ -72,7 +72,6 @@ PHP_METHOD(Collection, minusValues); PHP_METHOD(Collection, minusValuesAssign); PHP_METHOD(Collection, none); PHP_METHOD(Collection, onEach); -PHP_METHOD(Collection, orEmpty); PHP_METHOD(Collection, partition); PHP_METHOD(Collection, plus); PHP_METHOD(Collection, plusAssign); diff --git a/stubs/Collection.php b/stubs/Collection.php index c400a0d..f62179c 100644 --- a/stubs/Collection.php +++ b/stubs/Collection.php @@ -598,13 +598,6 @@ class Collection implements ArrayAccess, Countable */ function onEach($action) {} - /** - * Returns this collection if it's not null and the empty collection otherwise. - * - * @return Collection - */ - function orEmpty() {} - /** * Splits the original collection into pair of collections, where first collection contains * elements for which predicate yielded true, while second collection contains elements for diff --git a/tests/030-foreach-oneach.phpt b/tests/030-foreach-oneach.phpt new file mode 100644 index 0000000..e4eb3e9 --- /dev/null +++ b/tests/030-foreach-oneach.phpt @@ -0,0 +1,21 @@ +--TEST-- +Test Collection::forEach() and Collection::onEach(). +--FILE-- +forEach(function ($value) use (&$result1) { + $result1 .= $value; +}); +$collection1 = $collection->onEach(function ($value) use (&$result2) { + $result2 .= $value; +}); +if ($result1 != $result) + echo 'Collection::forEach() failed.', PHP_EOL; +if ($result2 != $result || $collection1->toArray() != $collection->toArray()) + echo 'Collection::onEach() failed.', PHP_EOL; +?> +--EXPECT--