Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| LoanRepositoryMappingTrait | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| rowToLoan | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| loanToRow | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace AppDemo\Loan\Infrastructure\Trait; |
| 11 | |
| 12 | use AppDemo\Loan\Domain\Loan; |
| 13 | use AppDemo\Loan\Domain\LoanStatus; |
| 14 | use DateTimeImmutable; |
| 15 | use Phexium\Domain\Constant\DateTimeFormat; |
| 16 | |
| 17 | trait LoanRepositoryMappingTrait |
| 18 | { |
| 19 | protected function rowToLoan(array $row): Loan |
| 20 | { |
| 21 | return new Loan( |
| 22 | $this->idGenerator->from($row['id']), |
| 23 | $this->idGenerator->from($row['user_id']), |
| 24 | $this->idGenerator->from($row['book_id']), |
| 25 | new DateTimeImmutable($row['borrowed_at']), |
| 26 | new DateTimeImmutable($row['due_at']), |
| 27 | $row['returned_at'] !== null ? new DateTimeImmutable($row['returned_at']) : null, |
| 28 | LoanStatus::from($row['status']) |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | protected function loanToRow(Loan $loan): array |
| 33 | { |
| 34 | return [ |
| 35 | 'id' => $loan->getId()->getValue(), |
| 36 | 'user_id' => $loan->getUserId()->getValue(), |
| 37 | 'book_id' => $loan->getBookId()->getValue(), |
| 38 | 'borrowed_at' => $loan->getBorrowedAt()->format(DateTimeFormat::SQL_DATETIME), |
| 39 | 'due_at' => $loan->getDueAt()->format(DateTimeFormat::SQL_DATETIME), |
| 40 | 'returned_at' => $loan->getReturnedAt()?->format(DateTimeFormat::SQL_DATETIME), |
| 41 | 'status' => $loan->getStatus()->value, |
| 42 | ]; |
| 43 | } |
| 44 | } |