From 7b557bffd8ee403afc9aeb71e36e26eac0fde40a Mon Sep 17 00:00:00 2001 From: ecrmnn Date: Sat, 14 May 2022 18:37:32 +0200 Subject: [PATCH 1/2] Adds Arr::map and Arr::transform --- src/Illuminate/Collections/Arr.php | 30 +++++++++++++++++++++++ src/Illuminate/Collections/Collection.php | 6 +---- tests/Support/SupportArrTest.php | 20 +++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 70c3a57b03c7..b372e8c6345a 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -530,6 +530,36 @@ protected static function explodePluckParameters($value, $key) return [$value, $key]; } + /** + * Run a map over each of the items in the array. + * + * @param array $array + * @param callable $callback + * @return array + */ + public static function map(array $array, callable $callback) + { + $keys = array_keys($array); + + $items = array_map($callback, $array, $keys); + + return array_combine($keys, $items); + } + + /** + * Transform each item in the array using a callback. + * + * @param array $array + * @param callable $callback + * @return array + */ + public static function transform(&$array, callable $callback) + { + $array = Arr::map($array, $callback); + + return $array; + } + /** * Push an item onto the beginning of an array. * diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 2a6126088399..ee5a74ae6ee9 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -720,11 +720,7 @@ public function pluck($value, $key = null) */ public function map(callable $callback) { - $keys = array_keys($this->items); - - $items = array_map($callback, $this->items, $keys); - - return new static(array_combine($keys, $items)); + return new static(Arr::map($this->items, $callback)); } /** diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 94e048646b21..cd1d52a40409 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -622,6 +622,26 @@ public function testArrayPluckWithNestedArrays() $this->assertEquals([['taylorotwell@gmail.com'], [null, null]], Arr::pluck($array, 'users.*.email')); } + public function testMap() + { + $data = ['first' => 'taylor', 'last' => 'otwell']; + $mapped = Arr::map($data, function ($value, $key) { + return $key.'-'.strrev($value); + }); + $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $mapped); + $this->assertEquals(['first' => 'taylor', 'last' => 'otwell'], $data); + } + + public function testTransform() + { + $data = ['first' => 'taylor', 'last' => 'otwell']; + $mapped = Arr::transform($data, function ($value, $key) { + return $key.'-'.strrev($value); + }); + $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $mapped); + $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data); + } + public function testPrepend() { $array = Arr::prepend(['one', 'two', 'three', 'four'], 'zero'); From 3cac12b3d0c9dacd87bd193fba1c0c8af1169b3b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 16 May 2022 10:19:38 -0500 Subject: [PATCH 2/2] remove transform --- src/Illuminate/Collections/Arr.php | 14 -------------- tests/Support/SupportArrTest.php | 10 ---------- 2 files changed, 24 deletions(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index b372e8c6345a..0c0e9d2b0486 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -546,20 +546,6 @@ public static function map(array $array, callable $callback) return array_combine($keys, $items); } - /** - * Transform each item in the array using a callback. - * - * @param array $array - * @param callable $callback - * @return array - */ - public static function transform(&$array, callable $callback) - { - $array = Arr::map($array, $callback); - - return $array; - } - /** * Push an item onto the beginning of an array. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index cd1d52a40409..eb3032d99c88 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -632,16 +632,6 @@ public function testMap() $this->assertEquals(['first' => 'taylor', 'last' => 'otwell'], $data); } - public function testTransform() - { - $data = ['first' => 'taylor', 'last' => 'otwell']; - $mapped = Arr::transform($data, function ($value, $key) { - return $key.'-'.strrev($value); - }); - $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $mapped); - $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data); - } - public function testPrepend() { $array = Arr::prepend(['one', 'two', 'three', 'four'], 'zero');