Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
28 changes: 28 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TNewKey, TValue>
*/
public function mapKeys(callable $callback)
{
return new static(Arr::mapKeys($this->all(), $callback));
}

/**
* Run an associative map over each of the items.
*
Expand Down Expand Up @@ -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<TKey, TValue>
*/
public function transformKeys(callable $callback)
{
$this->items = Arr::mapKeys($this->all(), $callback);

return $this;
}

/**
* Flatten a multi-dimensional associative array with dots.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Loading