diff --git a/src/collections_methods.c b/src/collections_methods.c index 80c73b7..0667699 100644 --- a/src/collections_methods.c +++ b/src/collections_methods.c @@ -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) diff --git a/stubs/Collection.php b/stubs/Collection.php index e893bbb..b16d0de 100644 --- a/stubs/Collection.php +++ b/stubs/Collection.php @@ -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 */ diff --git a/tests/004-all-any-none.phpt b/tests/004-all-any-none.phpt new file mode 100644 index 0000000..e591f2c --- /dev/null +++ b/tests/004-all-any-none.phpt @@ -0,0 +1,31 @@ +--TEST-- +Test Collection::all() and Collection::any(). +--FILE-- + 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-- diff --git a/tests/004-all-any.phpt b/tests/004-all-any.phpt deleted file mode 100644 index 6779d24..0000000 --- a/tests/004-all-any.phpt +++ /dev/null @@ -1,21 +0,0 @@ ---TEST-- -Test Collection::all() and Collection::any(). ---FILE-- - 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-- diff --git a/tests/023-min-max.phpt b/tests/023-min-max.phpt new file mode 100644 index 0000000..8c65ccb --- /dev/null +++ b/tests/023-min-max.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test Collection::min() and Collection::max(). +--FILE-- +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-- diff --git a/tests/024-keys-values.phpt b/tests/024-keys-values.phpt new file mode 100644 index 0000000..61881db --- /dev/null +++ b/tests/024-keys-values.phpt @@ -0,0 +1,12 @@ +--TEST-- +Test Collection::none(). +--FILE-- + '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--