Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| BookFieldValidator | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| validate | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| 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\Library\Presentation\Validator; |
| 11 | |
| 12 | use AppDemo\Library\Domain\Author; |
| 13 | use AppDemo\Library\Domain\Exception\InvalidAuthorException; |
| 14 | use AppDemo\Library\Domain\Exception\InvalidIsbnException; |
| 15 | use AppDemo\Library\Domain\Exception\InvalidTitleException; |
| 16 | use AppDemo\Library\Domain\ISBN; |
| 17 | use AppDemo\Library\Domain\Title; |
| 18 | |
| 19 | final class BookFieldValidator |
| 20 | { |
| 21 | public static function validate(string $title, string $author, string $isbn): array |
| 22 | { |
| 23 | $errors = []; |
| 24 | |
| 25 | try { |
| 26 | Title::fromString($title); |
| 27 | } catch (InvalidTitleException $invalidTitleException) { |
| 28 | $errors['title'] = $invalidTitleException->getMessage(); |
| 29 | } |
| 30 | |
| 31 | try { |
| 32 | Author::fromString($author); |
| 33 | } catch (InvalidAuthorException $invalidAuthorException) { |
| 34 | $errors['author'] = $invalidAuthorException->getMessage(); |
| 35 | } |
| 36 | |
| 37 | try { |
| 38 | ISBN::fromString($isbn); |
| 39 | } catch (InvalidIsbnException $invalidIsbnException) { |
| 40 | $errors['isbn'] = $invalidIsbnException->getMessage(); |
| 41 | } |
| 42 | |
| 43 | return $errors; |
| 44 | } |
| 45 | } |