-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathInMemoryBookRepository.php
More file actions
42 lines (34 loc) · 1.05 KB
/
InMemoryBookRepository.php
File metadata and controls
42 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
namespace App\BookStore\Infrastructure\InMemory;
use App\BookStore\Domain\Model\Book;
use App\BookStore\Domain\Repository\BookRepositoryInterface;
use App\BookStore\Domain\ValueObject\Author;
use App\BookStore\Domain\ValueObject\BookId;
use App\Shared\Infrastructure\InMemory\InMemoryRepository;
/**
* @extends InMemoryRepository<Book>
*/
final class InMemoryBookRepository extends InMemoryRepository implements BookRepositoryInterface
{
public function add(Book $book): void
{
$this->entities[(string) $book->id()] = $book;
}
public function remove(Book $book): void
{
unset($this->entities[(string) $book->id()]);
}
public function ofId(BookId $id): ?Book
{
return $this->entities[(string) $id] ?? null;
}
public function withAuthor(Author $author): static
{
return $this->filter(fn (Book $book) => $book->author()->isEqualTo($author));
}
public function withCheapestsFirst(): static
{
return $this->sort(['price' => 'asc']);
}
}