Add `onEach()` and `forEach()`. Remove `orEmpty()`.

This commit is contained in:
CismonX 2018-05-09 21:00:18 +08:00
parent 570a03b943
commit dbf6fc6ecd
5 changed files with 42 additions and 16 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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);

View File

@ -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

View File

@ -0,0 +1,21 @@
--TEST--
Test Collection::forEach() and Collection::onEach().
--FILE--
<?php
$array = ['a', 'b', 'c', 'd', 'e'];
$result2 = $result1 = $result = '';
foreach ($array as $value)
$result .= $value;
$collection = Collection::init($array);
$collection->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--