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

[5.5] Add buildToDictionary Method #21505

Merged
merged 3 commits into from
Oct 3, 2017
Merged
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
13 changes: 3 additions & 10 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,11 @@ protected function getRelationValue(array $dictionary, $key, $type)
*/
protected function buildDictionary(Collection $results)
{
$dictionary = [];

$foreign = $this->getForeignKeyName();

// First we will create a dictionary of models keyed by the foreign key of the
// relationship as this will allow us to quickly access all of the related
// models without having to do nested looping which will be quite slow.
foreach ($results as $result) {
$dictionary[$result->{$foreign}][] = $result;
}

return $dictionary;
return $results->mapToDictionary(function ($result) use ($foreign) {
return [$result->{$foreign} => $result];
})->all();
}

/**
Expand Down
23 changes: 19 additions & 4 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -851,22 +851,37 @@ public function mapSpread(callable $callback)
}

/**
* Run a grouping map over the items.
* Run a dictionary map over the items.
*
* The callback should return an associative array with a single key/value pair.
*
* @param callable $callback
* @return static
*/
public function mapToGroups(callable $callback)
public function mapToDictionary(callable $callback)
{
$groups = $this->map($callback)->reduce(function ($groups, $pair) {
$dictionary = $this->map($callback)->reduce(function ($groups, $pair) {
$groups[key($pair)][] = reset($pair);

return $groups;
}, []);

return (new static($groups))->map([$this, 'make']);
return new static($dictionary);
}

/**
* Run a grouping map over the items.
*
* The callback should return an associative array with a single key/value pair.
*
* @param callable $callback
* @return static
*/
public function mapToGroups(callable $callback)
{
$groups = $this->mapToDictionary($callback);

return $groups->map([$this, 'make']);
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,35 @@ public function testFlatMap()
$this->assertEquals(['programming', 'basketball', 'music', 'powerlifting'], $data->all());
}

public function testMapToDictionary()
{
$data = new Collection([
['id' => 1, 'name' => 'A'],
['id' => 2, 'name' => 'B'],
['id' => 3, 'name' => 'C'],
['id' => 4, 'name' => 'B'],
]);

$groups = $data->mapToDictionary(function ($item, $key) {
return [$item['name'] => $item['id']];
});

$this->assertInstanceOf(Collection::class, $groups);
$this->assertEquals(['A' => [1], 'B' => [2, 4], 'C' => [3]], $groups->toArray());
$this->assertInternalType('array', $groups['A']);
}

public function testMapToDictionaryWithNumericKeys()
{
$data = new Collection([1, 2, 3, 2, 1]);

$groups = $data->mapToDictionary(function ($item, $key) {
return [$item => $key];
});

$this->assertEquals([1 => [0, 4], 2 => [1, 3], 3 => [2]], $groups->toArray());
}

public function testMapToGroups()
{
$data = new Collection([
Expand Down