Skip to content

Commit 2fdf1aa

Browse files
[5.x] Add whereInId to EntryRepository (#11668)
Co-authored-by: Jason Varga <[email protected]>
1 parent bebbb83 commit 2fdf1aa

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/Contracts/Entries/EntryRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public function whereCollection(string $handle);
1010

1111
public function whereInCollection(array $handles);
1212

13+
// public function whereInId(array $ids);
14+
1315
public function find($id);
1416

1517
public function findOrFail($id);

src/Stache/Repositories/EntryRepository.php

Lines changed: 13 additions & 0 deletions
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 whereInId($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()) {

tests/Stache/Repositories/EntryRepositoryTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ public function it_gets_entry_by_structure_uri()
190190
$this->assertEquals('Directors', $entry->title());
191191
}
192192

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

0 commit comments

Comments
 (0)