diff --git a/README.md b/README.md index 8154a17..6b55cae 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ $names = Collection::init($employees) ->sortedByDescending(function ($value) { return $value['age']; }) - ->mapValues(function ($value) { + ->map(function ($value) { return $value['name']; }) ->toArray(); diff --git a/src/collections_me.c b/src/collections_me.c index 49e128c..571eff4 100644 --- a/src/collections_me.c +++ b/src/collections_me.c @@ -168,11 +168,7 @@ const zend_function_entry collection_methods[] = { PHP_ME(Collection, last, predicate_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Collection, lastIndexOf, element_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Collection, map, transform_arginfo, ZEND_ACC_PUBLIC) - PHP_ME(Collection, mapKeys, transform_arginfo, ZEND_ACC_PUBLIC) - PHP_ME(Collection, mapKeysTo, destination_transform_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Collection, mapTo, destination_transform_arginfo, ZEND_ACC_PUBLIC) - PHP_ME(Collection, mapValues, transform_arginfo, ZEND_ACC_PUBLIC) - PHP_ME(Collection, mapValuesTo, destination_transform_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Collection, max, NULL, ZEND_ACC_PUBLIC) PHP_ME(Collection, maxBy, selector_arginfo, ZEND_ACC_PUBLIC) PHP_ME(Collection, maxWith, comparator_arginfo, ZEND_ACC_PUBLIC) diff --git a/src/collections_methods.c b/src/collections_methods.c index 7e7b150..00c7aff 100644 --- a/src/collections_methods.c +++ b/src/collections_methods.c @@ -1023,32 +1023,39 @@ PHP_METHOD(Collection, lastIndexOf) PHP_METHOD(Collection, map) { - -} - -PHP_METHOD(Collection, mapKeys) -{ - -} - -PHP_METHOD(Collection, mapKeysTo) -{ - + 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(); + ARRAY_NEW_EX(new_collection, current); + ZEND_HASH_FOREACH_BUCKET(current, Bucket* bucket) + CALLBACK_KEYVAL_INVOKE(params, bucket); + zend_hash_next_index_insert(new_collection, &retval); + ZEND_HASH_FOREACH_END(); + RETVAL_NEW_COLLECTION(new_collection); } PHP_METHOD(Collection, mapTo) { - -} - -PHP_METHOD(Collection, mapValues) -{ - -} - -PHP_METHOD(Collection, mapValuesTo) -{ - + zend_fcall_info fci; + zend_fcall_info_cache fcc; + zval* dest; + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_OBJECT_OF_CLASS(dest, collections_collection_ce) + Z_PARAM_FUNC(fci, fcc) + ZEND_PARSE_PARAMETERS_END(); + INIT_FCI(2); + zend_array* current = COLLECTION_FETCH_CURRENT(); + zend_array* dest_arr = COLLECTION_FETCH(dest); + SEPARATE_COLLECTION(dest_arr, dest); + ZEND_HASH_FOREACH_BUCKET(current, Bucket* bucket) + CALLBACK_KEYVAL_INVOKE(params, bucket); + zend_hash_next_index_insert(dest_arr, &retval); + ZEND_HASH_FOREACH_END(); + RETVAL_ZVAL(dest, 1, 0); } PHP_METHOD(Collection, max) diff --git a/src/php_collections_me.h b/src/php_collections_me.h index ade62c4..90e1f39 100644 --- a/src/php_collections_me.h +++ b/src/php_collections_me.h @@ -54,11 +54,7 @@ PHP_METHOD(Collection, keys); PHP_METHOD(Collection, last); PHP_METHOD(Collection, lastIndexOf); PHP_METHOD(Collection, map); -PHP_METHOD(Collection, mapKeys); -PHP_METHOD(Collection, mapKeysTo); PHP_METHOD(Collection, mapTo); -PHP_METHOD(Collection, mapValues); -PHP_METHOD(Collection, mapValuesTo); PHP_METHOD(Collection, max); PHP_METHOD(Collection, maxBy); PHP_METHOD(Collection, maxWith); diff --git a/stubs/Collection.php b/stubs/Collection.php index 8d4fd7f..251364d 100644 --- a/stubs/Collection.php +++ b/stubs/Collection.php @@ -39,7 +39,7 @@ class Collection implements ArrayAccess, Countable * Returns a collection containing key-value pairs provided by transform function applied * to elements of the given collection. * - * @param callable $transform ($value) -> Pair + * @param callable $transform ($value, $key) -> Pair * @return Collection */ function associate($transform) {} @@ -49,7 +49,7 @@ class Collection implements ArrayAccess, Countable * function applied to each element of the given collection. * * @param Collection $destination - * @param callable $transform ($value) -> Pair + * @param callable $transform ($value, $key) -> Pair * @return Collection */ function associateTo($destination, $transform) {} @@ -58,7 +58,7 @@ class Collection implements ArrayAccess, Countable * Returns a collection containing the elements from the given collection indexed by the key * returned from keySelector function applied to each element. * - * @param callable $key_selector ($value) -> $key + * @param callable $key_selector ($value, $key) -> $new_key * @return Collection */ function associateBy($key_selector) {} @@ -69,7 +69,7 @@ class Collection implements ArrayAccess, Countable * and value is the element itself. * * @param Collection $destination - * @param callable $key_selector ($value) -> $key + * @param callable $key_selector ($value, $key) -> $new_key * @return Collection */ function associateByTo($destination, $key_selector) {} @@ -398,62 +398,24 @@ class Collection implements ArrayAccess, Countable function lastIndexOf($element) {} /** - * Returns a collection containing the results of applying the given transform function - * to each element in the original collection. + * Returns a collection containing an array of values obtained by applying the transform function + * to each element of the original collection. * - * @param callable $transform ($value, $key) -> Pair + * @param callable $transform ($value, $key) -> $new_value * @return Collection */ function map($transform) {} /** - * Returns a new collection with entries having the keys obtained by applying the transform function - * to each keys and values of this collection. - * - * @param callable $transform ($value, $key) -> $new_key - * @return Collection - */ - function mapKeys($transform) {} - - /** - * Populates the given destination collection with entries having the keys obtained by applying the - * transform function to each keys and values of this collections. + * Populates the given destination collection with an array of values obtained by applying the + * transform function to each element of the original collection. * * @param Collection $destination - * @param callable $transform ($value, $key) -> $new_key - * @return Collection - */ - function mapKeysTo($destination, $transform) {} - - /** - * Applies the given transform function to each element of the original collection and appends the - * results to the given destination. - * - * @param Collection $destination - * @param callable $transform ($value, $key) -> Pair + * @param callable $transform ($value, $key) -> $new_value * @return Collection */ function mapTo($destination, $transform) {} - /** - * Returns a new collection with entries having the keys of this collection and the values obtained - * by applying the transform function to each entry in this collection. - * - * @param callable $transform ($value, $key) -> $new_value - * @return Collection - */ - function mapValues($transform) {} - - /** - * Populates the given destination collection with entries having the keys of this collection and - * the values obtained by applying the transform function to each entry in this collection. - * - * @param Collection $destination - * @param callable $transform ($value, $key) -> $new_value - * @return Collection - */ - function mapValuesTo($destination, $transform) {} - /** * Returns the largest element or null if there are no elements. The collection should contain * only one type of numeric elements(int/double). diff --git a/tests/037-map.phpt b/tests/037-map.phpt new file mode 100644 index 0000000..449ab8c --- /dev/null +++ b/tests/037-map.phpt @@ -0,0 +1,18 @@ +--TEST-- +Test Collection::map() and Collection::mapTo(). +--FILE-- + 'bar', 'bar' => 'baz']; +$collection = Collection::init($array)->map(function ($value, $key) { + return $key.$value; +}); +if ($collection->toArray() != ['foobar', 'barbaz']) + echo 'Collection::map() failed.', PHP_EOL; +$collection1 = Collection::init(['dummy']); +$collection2 = Collection::init($array)->mapTo($collection1, function ($value, $key) { + return $value.$key; +}); +if ($collection1->toArray() != $collection2->toArray() || $collection1->toArray() != ['dummy', 'barfoo','bazbar']) + echo 'Collection::mapTo() failed.', PHP_EOL; +?> +--EXPECT--