Add `min()`, `max()`, `none()`, `keys()`, `values()`.

This commit is contained in:
CismonX 2018-04-24 17:22:43 +08:00
parent 1c787a4a41
commit ed1926580f
6 changed files with 99 additions and 28 deletions

View File

@ -864,7 +864,17 @@ PHP_METHOD(Collection, isNotEmpty)
PHP_METHOD(Collection, keys)
{
zend_array* current = COLLECTION_FETCH_EX();
ARRAY_NEW_EX(new_collection, current);
ZEND_HASH_FOREACH_BUCKET(current, Bucket* bucket)
zval val;
if (bucket->key)
ZVAL_STR(&val, bucket->key);
else
ZVAL_LONG(&val, bucket->h);
zend_hash_next_index_insert(new_collection, &val);
ZEND_HASH_FOREACH_END();
RETVAL_NEW_COLLECTION(new_collection);
}
PHP_METHOD(Collection, last)
@ -925,7 +935,11 @@ PHP_METHOD(Collection, mapValuesTo)
PHP_METHOD(Collection, max)
{
zend_array* current = COLLECTION_FETCH_EX();
zval* retval = zend_hash_minmax(current, numeric_compare_function, 1);
if (retval)
RETURN_ZVAL(retval, 0, 0);
RETVAL_NULL();
}
PHP_METHOD(Collection, maxBy)
@ -940,7 +954,11 @@ PHP_METHOD(Collection, maxWith)
PHP_METHOD(Collection, min)
{
zend_array* current = COLLECTION_FETCH_EX();
zval* retval = zend_hash_minmax(current, numeric_compare_function, 0);
if (retval)
RETURN_ZVAL(retval, 0, 0);
RETVAL_NULL();
}
PHP_METHOD(Collection, minBy)
@ -985,7 +1003,19 @@ PHP_METHOD(Collection, minusValuesAssign)
PHP_METHOD(Collection, none)
{
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();
zend_array* current = COLLECTION_FETCH_EX();
ZEND_HASH_FOREACH_BUCKET(current, Bucket* bucket)
CALLBACK_KEYVAL_INVOKE(params, bucket);
if (zend_is_true(&retval))
RETURN_FALSE;
ZEND_HASH_FOREACH_END();
RETURN_TRUE;
}
PHP_METHOD(Collection, onEach)
@ -1199,7 +1229,12 @@ PHP_METHOD(Collection, union)
PHP_METHOD(Collection, values)
{
zend_array* current = COLLECTION_FETCH_EX();
ARRAY_NEW_EX(new_collection, current);
ZEND_HASH_FOREACH_VAL(current, zval* val)
zend_hash_next_index_insert(new_collection, val);
ZEND_HASH_FOREACH_END();
RETVAL_NEW_COLLECTION(new_collection);
}
PHP_METHOD(Pair, __construct)

View File

@ -447,7 +447,8 @@ class Collection implements ArrayAccess, Countable
function mapValuesTo($destination, $transform) {}
/**
* Returns the largest element or null if there are no elements.
* Returns the largest element or null if there are no elements. The collection should contain
* only one type of numeric elements(int/double).
*
* @return mixed
*/
@ -472,7 +473,8 @@ class Collection implements ArrayAccess, Countable
function maxWith($comparator) {}
/**
* Returns the largest element or null if there are no elements.
* Returns the largest element or null if there are no elements. The collection should contain
* only one type of numeric elements(int/double).
*
* @return mixed
*/

View File

@ -0,0 +1,31 @@
--TEST--
Test Collection::all() and Collection::any().
--FILE--
<?php
$collection = Collection::init(['abc' => 12, 'de' => 5, 'f' => 100]);
$result = $collection->all(function ($value) {
return $value < 100;
});
$result1 = $collection->all(function ($value, $key) {
return strlen($key) < 4;
});
if ($result || !$result1)
echo 'Collection::all() failed.', PHP_EOL;
$result = $collection->any(function ($value) {
return $value % 3 == 0;
});
$result1 = $collection->any(function ($value, $key) {
return strpos($key, 'g') !== false;
});
if (!$result || $result1)
echo 'Collection::any() failed.', PHP_EOL;
$result = $collection->none(function ($value) {
return $value < 0;
});
$result1 = $collection->none(function ($value, $key) {
return ctype_alnum($value . $key);
});
if (!$result || $result1)
echo 'Collection::none() failed.', PHP_EOL;
?>
--EXPECT--

View File

@ -1,21 +0,0 @@
--TEST--
Test Collection::all() and Collection::any().
--FILE--
<?php
$collection = Collection::init(['abc' => 12, 'de' => 5, 'f' => 100]);
$result = $collection->all(function ($value) {
return $value < 100;
});
$result1 = $collection->all(function ($value, $key) {
return strlen($key) < 4;
});
$result2 = $collection->any(function ($value) {
return $value % 3 == 0;
});
$result3 = $collection->any(function ($value, $key) {
return strpos($key, 'g') !== false;
});
if ($result || !$result1 || !$result2 || $result3)
echo 'Collection::all() failed.', PHP_EOL;
?>
--EXPECT--

12
tests/023-min-max.phpt Normal file
View File

@ -0,0 +1,12 @@
--TEST--
Test Collection::min() and Collection::max().
--FILE--
<?php
$array = [3.42, 7.15, 0.0, -4.2, 1.64];
$collection = Collection::init($array);
if ($collection->max() != max($array) || Collection::init()->max() != null)
echo 'Collection::max() failed.', PHP_EOL;
if ($collection->min() != min($array) || Collection::init()->min() != null)
echo 'Collection::min() failed.', PHP_EOL;
?>
--EXPECT--

View File

@ -0,0 +1,12 @@
--TEST--
Test Collection::none().
--FILE--
<?php
$array = ['a' => 'b', 'c' => ['d'], 10 => 'f'];
$collection = Collection::init($array);
if ($collection->keys()->toArray() != array_keys($array))
echo 'Collection::keys() failed.', PHP_EOL;
if ($collection->values()->toArray() != array_values($array))
echo 'Collection::values() failed.', PHP_EOL;
?>
--EXPECT--