Add `slice()`.
This commit is contained in:
parent
4fce31a10f
commit
d889b71f90
|
@ -1419,7 +1419,29 @@ PHP_METHOD(Collection, single)
|
||||||
|
|
||||||
PHP_METHOD(Collection, slice)
|
PHP_METHOD(Collection, slice)
|
||||||
{
|
{
|
||||||
|
zval* elements;
|
||||||
|
ZEND_PARSE_PARAMETERS_START(1, 1)
|
||||||
|
Z_PARAM_ZVAL(elements)
|
||||||
|
ZEND_PARSE_PARAMETERS_END();
|
||||||
|
ELEMENTS_VALIDATE(elements, ERR_BAD_ARGUMENT_TYPE, return);
|
||||||
|
zend_array* current = COLLECTION_FETCH_CURRENT();
|
||||||
|
ARRAY_NEW_EX(sliced, current);
|
||||||
|
ZEND_HASH_FOREACH_VAL(elements_arr, zval* val)
|
||||||
|
if (Z_TYPE_P(val) == IS_LONG) {
|
||||||
|
zval* found = zend_hash_index_find(current, Z_LVAL_P(val));
|
||||||
|
if (found) {
|
||||||
|
Z_TRY_ADDREF_P(found);
|
||||||
|
zend_hash_next_index_insert(sliced, found);
|
||||||
|
}
|
||||||
|
} else if (Z_TYPE_P(val) == IS_STRING) {
|
||||||
|
zval* found = zend_hash_find(current, Z_STR_P(val));
|
||||||
|
if (found) {
|
||||||
|
Z_TRY_ADDREF_P(found);
|
||||||
|
zend_hash_add(sliced, Z_STR_P(val), found);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ZEND_HASH_FOREACH_END();
|
||||||
|
RETVAL_NEW_COLLECTION(sliced);
|
||||||
}
|
}
|
||||||
|
|
||||||
PHP_METHOD(Collection, sort)
|
PHP_METHOD(Collection, sort)
|
||||||
|
|
|
@ -745,6 +745,7 @@ class Collection implements ArrayAccess, Countable
|
||||||
* Returns a list containing elements at specified keys.
|
* Returns a list containing elements at specified keys.
|
||||||
*
|
*
|
||||||
* @param array|Collection $keys
|
* @param array|Collection $keys
|
||||||
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
function slice($keys) {}
|
function slice($keys) {}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
--TEST--
|
||||||
|
Test Collection::slice().
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
$array = [1, 2, 3, 4, 5, 6];
|
||||||
|
$collection = Collection::init($array)->slice([2, -1, 3, 6]);
|
||||||
|
$array1 = ['a' => 'b', 'c' => 'd', 'e' => 'f'];
|
||||||
|
$collection1 = Collection::init($array1)->slice(['c', 'a', 'g']);
|
||||||
|
if ($collection->toArray() != [3, 4] || $collection1->toArray() != ['c' => 'd', 'a' => 'b'])
|
||||||
|
echo 'Collection::slice() failed.', PHP_EOL;
|
||||||
|
?>
|
||||||
|
--EXPECT--
|
Reference in New Issue