Skip to content

Commit 434e627

Browse files
committed
Adds EntryRepository#findByIds()
1 parent 20c8888 commit 434e627

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Diff for: src/Contracts/Entries/EntryRepository.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public function findOrFail($id);
1616

1717
public function findByUri(string $uri);
1818

19+
public function findByIds($ids);
20+
1921
public function make();
2022

2123
public function query();

Diff for: src/Stache/Repositories/EntryRepository.php

+13
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ public function findByUri(string $uri, ?string $site = null): ?Entry
9696
: $entry;
9797
}
9898

99+
public function findByIds($ids): EntryCollection
100+
{
101+
$entries = $this->query()->whereIn('id', $ids)->get();
102+
$entriesById = $entries->keyBy->id();
103+
104+
$ordered = collect($ids)
105+
->map(fn ($id) => $entriesById->get($id))
106+
->filter()
107+
->values();
108+
109+
return EntryCollection::make($ordered);
110+
}
111+
99112
public function save($entry)
100113
{
101114
if (! $entry->id()) {

Diff for: tests/Stache/Repositories/EntryRepositoryTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Stache\Repositories;
44

55
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\Attributes\Group;
67
use PHPUnit\Framework\Attributes\Test;
78
use Statamic\Contracts\Entries\Entry;
89
use Statamic\Entries\EntryCollection;
@@ -190,6 +191,27 @@ public function it_gets_entry_by_structure_uri()
190191
$this->assertEquals('Directors', $entry->title());
191192
}
192193

194+
#[Test, Group('EntryRepository#findByIds')]
195+
#[DataProvider('entriesByIdsProvider')]
196+
public function it_gets_entries_by_ids($ids, $expected)
197+
{
198+
$actual = $this->repo->findByIds($ids);
199+
200+
$this->assertInstanceOf(EntryCollection::class, $actual);
201+
$this->assertEquals($expected, $actual->map->get('title')->all());
202+
}
203+
204+
public static function entriesByIdsProvider()
205+
{
206+
return [
207+
'no ids' => [[], []],
208+
'single' => [['numeric-one'], ['One']],
209+
'multiple' => [['numeric-one', 'numeric-two', 'numeric-three'], ['One', 'Two', 'Three']],
210+
'missing' => [['numeric-one', 'unknown', 'numeric-three'], ['One', 'Three']],
211+
'ordered' => [['numeric-three', 'numeric-one', 'numeric-two'], ['Three', 'One', 'Two']],
212+
];
213+
}
214+
193215
#[Test]
194216
public function it_saves_an_entry_to_the_stache_and_to_a_file()
195217
{

0 commit comments

Comments
 (0)