diff --git a/src/Bovin/Dto/AnimalSummaryDto.php b/src/Bovin/Dto/AnimalSummaryDto.php new file mode 100644 index 0000000..b6a81d9 --- /dev/null +++ b/src/Bovin/Dto/AnimalSummaryDto.php @@ -0,0 +1,16 @@ + $presencePeriods + */ + public function __construct( + public ?BovinIdentificationDto $identification, + public array $presencePeriods, + ) {} +} diff --git a/src/Bovin/Mapper/AnimalSummaryMapper.php b/src/Bovin/Mapper/AnimalSummaryMapper.php new file mode 100644 index 0000000..7bedb34 --- /dev/null +++ b/src/Bovin/Mapper/AnimalSummaryMapper.php @@ -0,0 +1,32 @@ +IdentiteBovin ?? null; + $identification = is_object($identificationNode) ? $this->mapIdentification($identificationNode) : null; + + $presencePeriods = []; + $presencePeriodsNode = $bovinNode->PeriodesPresences->PeriodePresence ?? null; + foreach ($this->normalizeToList($presencePeriodsNode) as $presencePeriodNode) { + if (!is_object($presencePeriodNode)) { + continue; + } + $presencePeriods[] = $this->mapPresencePeriod($presencePeriodNode); + } + + return new AnimalSummaryDto( + identification: $identification, + presencePeriods: $presencePeriods, + ); + } +} diff --git a/tests/Unit/Bovin/Mapper/AnimalSummaryMapperTest.php b/tests/Unit/Bovin/Mapper/AnimalSummaryMapperTest.php new file mode 100644 index 0000000..e5e1da3 --- /dev/null +++ b/tests/Unit/Bovin/Mapper/AnimalSummaryMapperTest.php @@ -0,0 +1,70 @@ +makeBovinNode(); + + $summary = new AnimalSummaryMapper()->map($node); + + self::assertInstanceOf(AnimalSummaryDto::class, $summary); + self::assertNotNull($summary->identification); + self::assertSame('FR1234567890', $summary->identification->bovin?->nationalNumber); + self::assertSame('F', $summary->identification->sex); + self::assertCount(2, $summary->presencePeriods); + } + + public function testMapHandlesMissingOptionalNodes(): void + { + $summary = new AnimalSummaryMapper()->map(new stdClass()); + + self::assertNull($summary->identification); + self::assertSame([], $summary->presencePeriods); + } + + private function makeBovinNode(): object + { + $node = new stdClass(); + $node->IdentiteBovin = new stdClass(); + $node->IdentiteBovin->Sexe = 'F'; + $node->IdentiteBovin->Bovin = new stdClass(); + $node->IdentiteBovin->Bovin->CodePays = 'FR'; + $node->IdentiteBovin->Bovin->NumeroNational = 'FR1234567890'; + + $node->PeriodesPresences = new stdClass(); + $node->PeriodesPresences->PeriodePresence = [ + $this->makePresencePeriod('2024-01-10', '2024-06-01'), + $this->makePresencePeriod('2024-06-02', null), + ]; + + return $node; + } + + private function makePresencePeriod(string $entryDate, ?string $exitDate): object + { + $period = new stdClass(); + $period->Entree = new stdClass(); + $period->Entree->DateEntree = $entryDate; + if (null !== $exitDate) { + $period->Sortie = new stdClass(); + $period->Sortie->DateSortie = $exitDate; + } + + return $period; + } +}