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

Use InteractsWithDictionary in Eloquent collection #46196

Merged
merged 1 commit into from
Feb 21, 2023
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
15 changes: 9 additions & 6 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Queue\QueueableCollection;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection as BaseCollection;
use LogicException;
Expand All @@ -17,6 +18,8 @@
*/
class Collection extends BaseCollection implements QueueableCollection
{
use InteractsWithDictionary;

/**
* Find a model in the collection by key.
*
Expand Down Expand Up @@ -322,7 +325,7 @@ public function merge($items)
$dictionary = $this->getDictionary();

foreach ($items as $item) {
$dictionary[$item->getKey()] = $item;
$dictionary[$this->getDictionaryKey($item->getKey())] = $item;
}

return new static(array_values($dictionary));
Expand Down Expand Up @@ -398,7 +401,7 @@ public function diff($items)
$dictionary = $this->getDictionary($items);

foreach ($this->items as $item) {
if (! isset($dictionary[$item->getKey()])) {
if (! isset($dictionary[$this->getDictionaryKey($item->getKey())])) {
$diff->add($item);
}
}
Expand All @@ -423,7 +426,7 @@ public function intersect($items)
$dictionary = $this->getDictionary($items);

foreach ($this->items as $item) {
if (isset($dictionary[$item->getKey()])) {
if (isset($dictionary[$this->getDictionaryKey($item->getKey())])) {
$intersect->add($item);
}
}
Expand Down Expand Up @@ -459,7 +462,7 @@ public function only($keys)
return new static($this->items);
}

$dictionary = Arr::only($this->getDictionary(), $keys);
$dictionary = Arr::only($this->getDictionary(), array_map($this->getDictionaryKey(...), (array) $keys));

return new static(array_values($dictionary));
}
Expand All @@ -472,7 +475,7 @@ public function only($keys)
*/
public function except($keys)
{
$dictionary = Arr::except($this->getDictionary(), $keys);
$dictionary = Arr::except($this->getDictionary(), array_map($this->getDictionaryKey(...), (array) $keys));

return new static(array_values($dictionary));
}
Expand Down Expand Up @@ -545,7 +548,7 @@ public function getDictionary($items = null)
$dictionary = [];

foreach ($items as $value) {
$dictionary[$value->getKey()] = $value;
$dictionary[$this->getDictionaryKey($value->getKey())] = $value;
}

return $dictionary;
Expand Down
31 changes: 31 additions & 0 deletions tests/Database/DatabaseEloquentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,25 @@ public function testLoadExistsShouldCastBool()
$this->assertContainsOnly('bool', $commentsExists);
}

public function testWithNonScalarKey()
{
$fooKey = new EloquentTestKey('foo');
$foo = m::mock(Model::class);
$foo->shouldReceive('getKey')->andReturn($fooKey);

$barKey = new EloquentTestKey('bar');
$bar = m::mock(Model::class);
$bar->shouldReceive('getKey')->andReturn($barKey);

$collection = new Collection([$foo, $bar]);

$this->assertCount(1, $collection->only([$fooKey]));
$this->assertSame($foo, $collection->only($fooKey)->first());

$this->assertCount(1, $collection->except([$fooKey]));
$this->assertSame($bar, $collection->except($fooKey)->first());
}

/**
* Helpers...
*/
Expand Down Expand Up @@ -689,3 +708,15 @@ class EloquentTestCommentModel extends Model
protected $guarded = [];
public $timestamps = false;
}

class EloquentTestKey
{
public function __construct(private readonly string $key)
{
}

public function __toString()
{
return $this->key;
}
}