From 9badb93463fdabc370bdfa9eed97613a956bbafe Mon Sep 17 00:00:00 2001 From: Italo Israel Baeza Cabrera Date: Sat, 11 Oct 2025 14:02:34 -0300 Subject: [PATCH 1/5] [12.x] Adds simple key transformation to Collection and Arr helper. --- src/Illuminate/Collections/Arr.php | 12 ++++++++++++ src/Illuminate/Collections/Collection.php | 15 +++++++++++++++ tests/Support/SupportArrTest.php | 9 +++++++++ tests/Support/SupportCollectionTest.php | 7 +++++++ 4 files changed, 43 insertions(+) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index e6586edd7f95..d0f464af9c69 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -1158,6 +1158,18 @@ public static function toCssStyles($array) return implode(' ', $styles); } + /** + * Executes a callback that transforms each key of the array. + * + * @param array $array + * @param callable $callback + * @return array + */ + public static function transformKeys(array $array, callable $callback) + { + return array_combine(array_map($callback, array_keys($array)), array_values($array)); + } + /** * Filter the array using the given callback. * diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 08a852fe19fa..c78eb31a8934 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1742,6 +1742,21 @@ public function transform(callable $callback) return $this; } + /** + * Transform each key in the collection using a callback. + * + * @param callable(TKey): TKey $callback + * @return $this + * + * @phpstan-this-out static + */ + public function transformKeys(callable $callback) + { + $this->items = Arr::transformKeys($this->all(), $callback); + + return $this; + } + /** * Flatten a multi-dimensional associative array with dots. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index ab5c2f2150a7..d0b66f808450 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -1491,6 +1491,15 @@ public function testToCssStyles() $this->assertSame('font-weight: bold; margin-top: 4px; margin-left: 2px;', $styles); } + public function testTransformKeys() + { + $array = [100, '200', 300, '400', 500]; + + $new = Arr::transformKeys($array, fn($key) => $key + 1); + + $this->assertSame([1 => 100, 2 => '200', 3 => 300, 4 => '400', 5 => 500], $new); + } + public function testWhere() { $array = [100, '200', 300, '400', 500]; diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 81a6a3fd7dbe..745ae2dac460 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -3305,6 +3305,13 @@ public function testTransform() $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all()); } + public function testTransformKeys() + { + $data = new Collection(['first' => 'taylor', 'last' => 'otwell']); + $data->transformKeys('strtoupper'); + $this->assertSame(['FIRST' => 'taylor', 'LAST' => 'otwell'], $data->all()); + } + #[DataProvider('collectionClassProvider')] public function testGroupByAttribute($collection) { From 070823b7aa7fd2bb4ba665da79fa25110e129a23 Mon Sep 17 00:00:00 2001 From: Italo Israel Baeza Cabrera Date: Sat, 11 Oct 2025 14:02:34 -0300 Subject: [PATCH 2/5] [12.x] Adds simple key map to Collection and Arr helper. --- src/Illuminate/Collections/Arr.php | 24 +++++++++++++- src/Illuminate/Collections/Collection.php | 13 ++++++++ tests/Support/SupportArrTest.php | 39 +++++++++++++++++++++++ tests/Support/SupportCollectionTest.php | 2 +- 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index d0f464af9c69..fb65baf66edd 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -1165,11 +1165,33 @@ public static function toCssStyles($array) * @param callable $callback * @return array */ - public static function transformKeys(array $array, callable $callback) + public static function mapKeys(array $array, callable $callback) { return array_combine(array_map($callback, array_keys($array)), array_values($array)); } + /** + * Transforms each key of the array into "camelCase". + * + * @param array $array + * @return array + */ + public static function mapKeysToCamel(array $array) + { + return static::mapKeys($array, [Str::class, 'camel']); + } + + /** + * Transforms each key of the array into "snake_case". + * + * @param array $array + * @return array + */ + public static function mapKeysToSnake(array $array) + { + return static::mapKeys($array, [Str::class, 'snake']); + } + /** * Filter the array using the given callback. * diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index c78eb31a8934..2cecea233e0e 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -846,6 +846,19 @@ public function mapToDictionary(callable $callback) return new static($dictionary); } + /** + * Map each key in the collection using a callback. + * + * @template TNewKey of array-key + * + * @param callable(TKey): TNewKey $callback + * @return static + */ + public function mapKeys(callable $callback) + { + return new static(Arr::mapKeys($this->all(), $callback)); + } + /** * Run an associative map over each of the items. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index d0b66f808450..c9d250757065 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -994,6 +994,45 @@ public function testMapNullValues() $this->assertEquals(['first' => 'first-taylor', 'last' => 'last-'], $mapped); } + public function testMapKeys() + { + $array = [100, '200', 300, '400', 500]; + + $array = Arr::mapKeys($array, fn($key) => $key + 1); + + $this->assertSame([1 => 100, 2 => '200', 3 => 300, 4 => '400', 5 => 500], $array); + } + + public function testMapKeysToCamel() + { + $array = [ + 1 => 'foo', + 'foo_bar' => 'bar', + 'cuzCux' => 'baz', + ]; + + $this->assertSame([ + '1' => 'foo', + 'fooBar' => 'bar', + 'cuzCux' => 'baz', + ], Arr::mapKeysToCamel($array)); + } + + public function testMapKeysToSnake() + { + $array = [ + 1 => 'foo', + 'foo_bar' => 'bar', + 'cuzCux' => 'baz', + ]; + + $this->assertSame([ + '1' => 'foo', + 'foo_bar' => 'bar', + 'cuz_cux' => 'baz', + ], Arr::mapKeysToSnake($array)); + } + public function testMapWithKeys() { $data = [ diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 745ae2dac460..6dbe16dde7cb 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -3308,7 +3308,7 @@ public function testTransform() public function testTransformKeys() { $data = new Collection(['first' => 'taylor', 'last' => 'otwell']); - $data->transformKeys('strtoupper'); + $data->mapKeys('strtoupper'); $this->assertSame(['FIRST' => 'taylor', 'LAST' => 'otwell'], $data->all()); } From 6e6b343a69b8ede6865c730cc0f9730d1f270a1d Mon Sep 17 00:00:00 2001 From: Italo Israel Baeza Cabrera Date: Sun, 12 Oct 2025 16:52:47 -0300 Subject: [PATCH 3/5] Removes wrong test. --- tests/Support/SupportArrTest.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index c9d250757065..c301546d2bd3 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -1530,15 +1530,6 @@ public function testToCssStyles() $this->assertSame('font-weight: bold; margin-top: 4px; margin-left: 2px;', $styles); } - public function testTransformKeys() - { - $array = [100, '200', 300, '400', 500]; - - $new = Arr::transformKeys($array, fn($key) => $key + 1); - - $this->assertSame([1 => 100, 2 => '200', 3 => 300, 4 => '400', 5 => 500], $new); - } - public function testWhere() { $array = [100, '200', 300, '400', 500]; From 7b98dce864b82f7b4d606ae2398d8add981a9498 Mon Sep 17 00:00:00 2001 From: Italo Israel Baeza Cabrera Date: Sun, 12 Oct 2025 16:57:59 -0300 Subject: [PATCH 4/5] Fixes wrong method call. --- src/Illuminate/Collections/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 2cecea233e0e..feaec6d02bad 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1765,7 +1765,7 @@ public function transform(callable $callback) */ public function transformKeys(callable $callback) { - $this->items = Arr::transformKeys($this->all(), $callback); + $this->items = Arr::mapKeys($this->all(), $callback); return $this; } From ffb2c7886ba96784d0e92a747100b68328d61545 Mon Sep 17 00:00:00 2001 From: Italo Israel Baeza Cabrera Date: Sun, 12 Oct 2025 17:12:01 -0300 Subject: [PATCH 5/5] Fixes test expectation. --- tests/Support/SupportCollectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 6dbe16dde7cb..8765ee88547c 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -3308,7 +3308,7 @@ public function testTransform() public function testTransformKeys() { $data = new Collection(['first' => 'taylor', 'last' => 'otwell']); - $data->mapKeys('strtoupper'); + $data = $data->mapKeys('strtoupper'); $this->assertSame(['FIRST' => 'taylor', 'LAST' => 'otwell'], $data->all()); }