Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
LoanMapper
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromRow
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 toRow
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
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
8declare(strict_types=1);
9
10namespace AppDemo\Loan\Infrastructure\Mapper;
11
12use AppDemo\Loan\Domain\Loan;
13use AppDemo\Loan\Domain\LoanStatus;
14use DateTimeImmutable;
15use Phexium\Domain\Constant\DateTimeFormat;
16use Phexium\Plugin\IdGenerator\Port\IdGeneratorInterface;
17
18final readonly class LoanMapper
19{
20    public function __construct(
21        private IdGeneratorInterface $idGenerator,
22    ) {}
23
24    public function fromRow(array $row): Loan
25    {
26        return new Loan(
27            $this->idGenerator->from($row['id']),
28            $this->idGenerator->from($row['user_id']),
29            $this->idGenerator->from($row['book_id']),
30            new DateTimeImmutable($row['borrowed_at']),
31            new DateTimeImmutable($row['due_at']),
32            $row['returned_at'] !== null ? new DateTimeImmutable($row['returned_at']) : null,
33            LoanStatus::from($row['status'])
34        );
35    }
36
37    public function toRow(Loan $loan): array
38    {
39        return [
40            'id' => $loan->getId()->getValue(),
41            'user_id' => $loan->getUserId()->getValue(),
42            'book_id' => $loan->getBookId()->getValue(),
43            'borrowed_at' => $loan->getBorrowedAt()->format(DateTimeFormat::SQL_DATETIME),
44            'due_at' => $loan->getDueAt()->format(DateTimeFormat::SQL_DATETIME),
45            'returned_at' => $loan->getReturnedAt()?->format(DateTimeFormat::SQL_DATETIME),
46            'status' => $loan->getStatus()->value,
47        ];
48    }
49}