Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement mapByKey #54129

Closed
wants to merge 2 commits into from
Closed
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
29 changes: 29 additions & 0 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,35 @@ public static function mapWithKeys(array $array, callable $callback)
return $result;
}

/**
* Run an associative map over each of the item's values filtered by their key.
*
* The callback should return the same resultset with the modified values of the given keys.
*
* @param array $array
* @param string|array $keys
* @param callable $callback
* @return array
*/
public static function mapByKey(array $array, $keys, callable $callback)
{
$result = [];

foreach ($array as $parentKey => $row) {
foreach ($row as $key => $value) {
if (! in_array($key, (array) $keys, true)) {
$result[$parentKey][$key] = $value;

continue;
}

$result[$parentKey][$key] = $callback($value, $key, $row);
}
}

return $result;
}

/**
* Run a map over each nested chunk of items.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,20 @@ public function mapWithKeys(callable $callback)
return new static(Arr::mapWithKeys($this->items, $callback));
}

/**
* Run an associative map over each of the item's values filtered by their key.
*
* The callback should return the same resultset with the modified values of the given keys.
*
* @param string|array $keys
* @param callable $callback
* @return static
*/
public function mapByKey($keys, callable $callback)
{
return new static(Arr::mapByKey($this->items, $keys, $callback));
}

/**
* Merge the collection with the given items.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,20 @@ public function mapWithKeys(callable $callback)
});
}

/**
* Run an associative map over each of the item's values filtered by their key.
*
* The callback should return the same resultset with the modified values of the given keys.
*
* @param string|array $keys
* @param callable $callback
* @return static
*/
public function mapByKey($keys, callable $callback)
{
return $this->passthru('mapByKey', func_get_args());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure this is the correct way to go about adding a method to LazyCollection.

}

/**
* Merge the collection with the given items.
*
Expand Down
56 changes: 56 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Number;
use Illuminate\Support\Str;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use stdClass;
Expand Down Expand Up @@ -804,6 +806,60 @@ public function testMapWithKeys()
);
}

public function testMapByKey()
{
$invoices = [
['recipient' => 'Dries', 'currency' => 'USD', 'amount' => 1],
['recipient' => 'Taylor', 'currency' => 'USD', 'amount' => 25.67],
['recipient' => 'Nuno', 'currency' => 'EUR', 'amount' => 123987],
];

$invoices = Arr::mapByKey($invoices, 'amount', function ($value, $key, $row) {
return Number::currency($value, $row['currency']);
});

$this->assertEquals(
[
['recipient' => 'Dries', 'currency' => 'USD', 'amount' => '$1.00'],
['recipient' => 'Taylor', 'currency' => 'USD', 'amount' => '$25.67'],
['recipient' => 'Nuno', 'currency' => 'EUR', 'amount' => '€123,987.00'],
],
$invoices
);

$posts = [
['title' => 'Hello World', 'body' => '# Hello World', 'preview' => 'Test *post*'],
['title' => 'New Release', 'body' => '**New Release**', 'preview' => '**Release notes**'],
];

$posts = Arr::mapByKey($posts, ['body', 'preview'], fn ($value) => Str::markdown($value));

$this->assertEquals(
[
['title' => 'Hello World', 'body' => "<h1>Hello World</h1>\n", 'preview' => "<p>Test <em>post</em></p>\n"],
['title' => 'New Release', 'body' => "<p><strong>New Release</strong></p>\n", 'preview' => "<p><strong>Release notes</strong></p>\n"],
],
$posts
);

$data = [
['foo' => 'Dries', 'bar' => 'Joe', 'baz' => 'Jess'],
['foo' => 'Taylor', 'baz' => 'James'],
['bar' => 'Nuno'],
];

$data = Arr::mapByKey($data, ['bar', 'baz'], fn ($value) => Str::lower($value));

$this->assertEquals(
[
['foo' => 'Dries', 'bar' => 'joe', 'baz' => 'jess'],
['foo' => 'Taylor', 'baz' => 'james'],
['bar' => 'nuno'],
],
$data
);
}

public function testMapByReference()
{
$data = ['first' => 'taylor', 'last' => 'otwell'];
Expand Down
21 changes: 21 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3053,6 +3053,27 @@ public function testMapWithKeysCallbackKey($collection)
);
}

#[DataProvider('collectionClassProvider')]
public function testMapByKey($collection)
{
$data = new $collection([
['foo' => 'Dries', 'bar' => 'Joe', 'baz' => 'Jess'],
['foo' => 'Taylor', 'baz' => 'James'],
['bar' => 'Nuno'],
]);

$data = $data->mapByKey(['bar', 'baz'], fn ($value) => Str::lower($value));

$this->assertEquals(
[
['foo' => 'Dries', 'bar' => 'joe', 'baz' => 'jess'],
['foo' => 'Taylor', 'baz' => 'james'],
['bar' => 'nuno'],
],
$data->all()
);
}

#[DataProvider('collectionClassProvider')]
public function testMapInto($collection)
{
Expand Down