diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index e6586edd7f95..fb65baf66edd 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -1158,6 +1158,40 @@ 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 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 08a852fe19fa..feaec6d02bad 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. * @@ -1742,6 +1755,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::mapKeys($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..c301546d2bd3 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 81a6a3fd7dbe..8765ee88547c 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 = $data->mapKeys('strtoupper'); + $this->assertSame(['FIRST' => 'taylor', 'LAST' => 'otwell'], $data->all()); + } + #[DataProvider('collectionClassProvider')] public function testGroupByAttribute($collection) {