From ea17c0950a2e7a855107a26f688cbc2a7dcaa3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 14 Sep 2026 19:26:20 +0200 Subject: [PATCH] feat(hydrator): implement Kiota OpenAPI model hydrators --- src/Hydrator/BrandHydrator.php | 57 +++++ src/Hydrator/EmployeeHydrator.php | 81 +++++++ src/Hydrator/IncoCompoHydrator.php | 354 +++++++++++++++++++++++++++++ src/Hydrator/IncoHydrator.php | 244 ++++++++++++++++++++ src/Hydrator/IncoShortHydrator.php | 58 +++++ 5 files changed, 794 insertions(+) create mode 100644 src/Hydrator/BrandHydrator.php create mode 100644 src/Hydrator/EmployeeHydrator.php create mode 100644 src/Hydrator/IncoCompoHydrator.php create mode 100644 src/Hydrator/IncoHydrator.php create mode 100644 src/Hydrator/IncoShortHydrator.php diff --git a/src/Hydrator/BrandHydrator.php b/src/Hydrator/BrandHydrator.php new file mode 100644 index 0000000..5638604 --- /dev/null +++ b/src/Hydrator/BrandHydrator.php @@ -0,0 +1,57 @@ +getCode() ?? ''; + $name = $model->getName() ?? ''; + $label = mb_trim($name) !== '' ? $name : $code; + + return new Brand( + code: new BrandCode($code), + label: $label, + arcadiaId: $model->getArcadiaId(), + createdAt: $model->getCreatedAt(), + ); + } + + /** + * @return PageOf + */ + public function toPage(BrandListResponse $response, Pagination $pagination): PageOf + { + $items = []; + foreach ($response->getBrands() ?? [] as $brand) { + $items[] = $this->toDto($brand); + } + + return new PageOf($this->hydratePagination($response->getPagination(), $pagination), $items); + } + + private function hydratePagination(?PaginationMeta $meta, Pagination $pagination): PaginationDto + { + return new PaginationDto( + page: $meta?->getPage() ?? $pagination->pageNumber(), + pageSize: $meta?->getPageSize() ?? $pagination->pageSize(), + totalItems: $meta?->getTotalItems() ?? 0, + totalPages: $meta?->getTotalPages() ?? 0, + ); + } +} diff --git a/src/Hydrator/EmployeeHydrator.php b/src/Hydrator/EmployeeHydrator.php new file mode 100644 index 0000000..78f50b7 --- /dev/null +++ b/src/Hydrator/EmployeeHydrator.php @@ -0,0 +1,81 @@ +getMatricule() ?? ''; + $establishment = $model->getEstablishment() ?? ''; + $site = $model->getSite() ?? ''; + $service = $model->getService() ?? ''; + + $startDate = $this->dateToDateTime($model->getStartDate()) ?? new \DateTimeImmutable('today'); + $createdAt = $model->getCreatedAt() ?? new \DateTimeImmutable(); + + return new Employee( + matriculePublique: new ArcadiaEmployeeId($matricule), + lastname: $model->getLastName() ?? '', + firstname: $model->getFirstName() ?? '', + start_date: $startDate, + end_date: $this->dateToDateTime($model->getEndDate()), + planned_end_date: $this->dateToDateTime($model->getPlannedEndDate()), + establishment: new EstablishmentCode($establishment), + site: new SiteId($site), + service: new ServiceId($service), + created_at: $createdAt, + updated_at: $model->getUpdatedAt(), + ); + } + + /** + * @return PageOf + */ + public function toPage(EmployeeListResponse $response, Pagination $pagination): PageOf + { + $items = []; + foreach ($response->getEmployees() ?? [] as $employeeModel) { + $items[] = $this->toDto($employeeModel); + } + + return new PageOf($this->hydratePagination($response->getPagination(), $pagination), $items); + } + + private function hydratePagination(?PaginationMeta $meta, Pagination $pagination): PaginationDto + { + return new PaginationDto( + page: $meta?->getPage() ?? $pagination->pageNumber(), + pageSize: $meta?->getPageSize() ?? $pagination->pageSize(), + totalItems: $meta?->getTotalItems() ?? 0, + totalPages: $meta?->getTotalPages() ?? 0, + ); + } + + private function dateToDateTime(?Date $date): ?\DateTimeInterface + { + if ($date === null) { + return null; + } + + return new \DateTimeImmutable((string) $date); + } +} diff --git a/src/Hydrator/IncoCompoHydrator.php b/src/Hydrator/IncoCompoHydrator.php new file mode 100644 index 0000000..6b5562d --- /dev/null +++ b/src/Hydrator/IncoCompoHydrator.php @@ -0,0 +1,354 @@ +getIncoCompoId() ?? '0'); + $idInco = new IncoId($model->getIncoId() ?? '0'); + + return new IncoCompo( + id: $id, + idInco: $idInco, + identification: $this->hydrateIdentification($model), + energie: $this->hydrateEnergie($model), + macronutrimentsRef: $this->hydrateMacronutrimentsRef($model), + macronutrimentsPortion: $this->hydrateMacronutrimentsPortion($model), + fnl: $this->hydrateFnl($model), + apportsReference: $this->hydrateApportsReference($model), + nutriScore: $this->hydrateNutriScore($model), + pointsNutriScore: $this->hydratePointsNutriScore($model), + mineraux: $this->hydrateMineraux($model), + vitamines: $this->hydrateVitamines($model), + glucidesDetail: $this->hydrateGlucidesDetail($model), + acideGras: $this->hydrateAcideGras($model), + acidesAmines: $this->hydrateAcidesAmines($model), + composesSpeciaux: $this->hydrateComposesSpeciaux($model), + ); + } + + private function hydrateIdentification(KiotaIncoCompo $model): Identification + { + $ident = $model->getIdentification(); + + return new Identification( + nom: $ident?->getName() ?? '', + numero: $ident?->getNumber() ?? '', + source: $ident?->getSource() ?? '', + ); + } + + private function hydrateEnergie(KiotaIncoCompo $model): Energie + { + $energy = $model->getEnergy(); + $ref = $energy?->getReference(); + $portion = $energy?->getPortion(); + + return new Energie( + tailleReference: $ref?->getSize() ?? '', + valEnergKjReference: $ref?->getEnergyKj() ?? '', + valEnergKcalReference: $ref?->getEnergyKcal() ?? '', + taillePortion: $portion?->getSize() ?? '', + valEnergKjPortion: $portion?->getEnergyKj() ?? '', + valEnergKcalPortion: $portion?->getEnergyKcal() ?? '', + ); + } + + private function hydrateMacronutrimentsRef(KiotaIncoCompo $model): MacronutrimentsRef + { + $m = $model->getReferenceMacronutrients(); + + return new MacronutrimentsRef( + matGrassReference: $m?->getFat()?->getAmount() ?? '', + matGrassReferencePrec: $m?->getFat()?->getPrecision() ?? '', + acideGrasSatureReference: $m?->getSaturatedFat()?->getAmount() ?? '', + acideGrasSatureReferencePrec: $m?->getSaturatedFat()?->getPrecision() ?? '', + glucidesReference: $m?->getCarbohydrates()?->getAmount() ?? '', + glucidesReferencePrec: $m?->getCarbohydrates()?->getPrecision() ?? '', + sucreReference: $m?->getSugars()?->getAmount() ?? '', + sucreReferencePrec: $m?->getSugars()?->getPrecision() ?? '', + fibAlimReference: $m?->getFiber()?->getAmount() ?? '', + fibAlimReferencePrec: $m?->getFiber()?->getPrecision() ?? '', + proteineReference: $m?->getProtein()?->getAmount() ?? '', + proteineReferencePrec: $m?->getProtein()?->getPrecision() ?? '', + selReference: $m?->getSalt()?->getAmount() ?? '', + selReferencePrec: $m?->getSalt()?->getPrecision() ?? '', + sodiumReference: $m?->getSodium() ?? '', + ); + } + + private function hydrateMacronutrimentsPortion(KiotaIncoCompo $model): MacronutrimentsPortion + { + $m = $model->getPortionMacronutrients(); + + return new MacronutrimentsPortion( + matGrassPortion: $m?->getFat()?->getAmount() ?? '', + matGrassPortionPrec: $m?->getFat()?->getPrecision() ?? '', + acideGrasSaturePortion: $m?->getSaturatedFat()?->getAmount() ?? '', + acideGrasSaturePortionPrec: $m?->getSaturatedFat()?->getPrecision() ?? '', + glucidesPortion: $m?->getCarbohydrates()?->getAmount() ?? '', + glucidesPortionPrec: $m?->getCarbohydrates()?->getPrecision() ?? '', + sucrePortion: $m?->getSugars()?->getAmount() ?? '', + sucrePortionPrec: $m?->getSugars()?->getPrecision() ?? '', + fibAlimPortion: $m?->getFiber()?->getAmount() ?? '', + fibAlimPortionPrec: $m?->getFiber()?->getPrecision() ?? '', + proteinePortion: $m?->getProtein()?->getAmount() ?? '', + proteinePortionPrec: $m?->getProtein()?->getPrecision() ?? '', + selPortion: $m?->getSalt()?->getAmount() ?? '', + selPortionPrec: $m?->getSalt()?->getPrecision() ?? '', + sodiumPortion: $m?->getSodium() ?? '', + ); + } + + private function hydrateFnl(KiotaIncoCompo $model): FruitLegumeNoix + { + $fnl = $model->getFreshProduceAndNuts(); + + return new FruitLegumeNoix( + fruitsReference: $fnl?->getReference()?->getFruits() ?? '', + fruitsCoqueSechReference: $fnl?->getReference()?->getNuts() ?? '', + fruitsPortion: $fnl?->getPortion()?->getFruits() ?? '', + fruitsCoqueSechPortion: $fnl?->getPortion()?->getNuts() ?? '', + fruitLegumeNoixFrais: $fnl?->getReference()?->getFreshFnl() ?? '', + fruitLegumeNoixFraisPortion: $fnl?->getPortion()?->getFreshFnl() ?? '', + pourcentFruitsLegumesNoixFrais: $fnl?->getFreshFnlPercentage()?->getValue() ?? '', + pourcentFruits: $fnl?->getFruitsPercentage()?->getValue() ?? '', + ); + } + + private function hydrateApportsReference(KiotaIncoCompo $model): ApportsReference + { + $ri = $model->getReferenceIntakes(); + + return new ApportsReference( + pourcentApportRefE14P: $ri?->getEnergyE14()?->getValue() ?? '', + pourcentApportRefKjoP: $ri?->getEnergyKj()?->getValue() ?? '', + pourcentApportRefFatP: $ri?->getFat()?->getValue() ?? '', + pourcentApportRefFatsatP: $ri?->getSaturatedFat()?->getValue() ?? '', + pourcentApportRefChovalP: $ri?->getCarbohydrates()?->getValue() ?? '', + pourcentApportRefSugarP: $ri?->getSugars()?->getValue() ?? '', + pourcentApportRefProP: $ri?->getProtein()?->getValue() ?? '', + pourcentApportRefSalteqP: $ri?->getSalt()?->getValue() ?? '', + apportPort: $ri?->getPortionAmount() ?? '', + pSurL: $ri?->getProteinToLipidRatio() ?? '', + ); + } + + private function hydrateNutriScore(KiotaIncoCompo $model): NutriScore + { + $ns = $model->getNutriScore(); + + return new NutriScore( + composanteN: $ns?->getNegativeComponent() ?? '', + composanteN2022: $ns?->getNegativeComponent2022() ?? '', + composanteP: $ns?->getPositiveComponent() ?? '', + composanteP2022: $ns?->getPositiveComponent2022() ?? '', + scoreNutri: $ns?->getScore() ?? '', + scoreNutri2022: $ns?->getScore2022() ?? '', + classement: $ns?->getGrade() ?? '', + nutriScore2022: $ns?->getGrade2022() ?? '', + nutriAArt: $ns?->getIsGradeAArticle() ?? '', + viandeRouge: $ns?->getIsRedMeat() ?? '', + ); + } + + private function hydratePointsNutriScore(KiotaIncoCompo $model): PointsNutriScore + { + $nsp = $model->getNutriScorePoints(); + + return new PointsNutriScore( + pointsEnergie: $nsp?->getEnergyPoints() ?? '', + pointsAcideGrasSature: $nsp?->getSaturatedFatPoints() ?? '', + pointsSucres: $nsp?->getSugarsPoints() ?? '', + pointsSel: $nsp?->getSaltPoints() ?? '', + pointsFibres: $nsp?->getFiberPoints() ?? '', + pointsProteines: $nsp?->getProteinPoints() ?? '', + pointsFruitsLegumesNoixFrais: $nsp?->getFreshFnlPoints() ?? '', + ); + } + + private function hydrateMineraux(KiotaIncoCompo $model): Mineraux + { + $m = $model->getMinerals(); + + return new Mineraux( + nutCa: $m?->getCalcium() ?? '', + nutMg: $m?->getMagnesium() ?? '', + nutK: $m?->getPotassium() ?? '', + nutP: $m?->getPhosphorus() ?? '', + nutCld: $m?->getChloride() ?? '', + nutFe: $m?->getIron() ?? '', + nutZn: $m?->getZinc() ?? '', + nutCu: $m?->getCopper() ?? '', + nutMn: $m?->getManganese() ?? '', + nutSe: $m?->getSelenium() ?? '', + nutId: $m?->getIodine() ?? '', + nutCr: $m?->getChromium() ?? '', + nutMo: $m?->getMolybdenum() ?? '', + nutS6: $m?->getSulfur() ?? '', + nutNitra: $m?->getNitrates() ?? '', + ); + } + + private function hydrateVitamines(KiotaIncoCompo $model): Vitamines + { + $v = $model->getVitamins(); + + return new Vitamines( + nutVita: $v?->getVitaminA() ?? '', + nutVitb6: $v?->getVitaminB6() ?? '', + nutVitb12: $v?->getVitaminB12() ?? '', + nutVitc: $v?->getVitaminC() ?? '', + nutVitd: $v?->getVitaminD() ?? '', + nutVite: $v?->getVitaminE() ?? '', + nutVitk: $v?->getVitaminK() ?? '', + nutChocal: $v?->getCholecalciferol() ?? '', + nutNia: $v?->getNiacin() ?? '', + nutRibf: '', + nutThia: '', + nutPantac: '', + nutBiot: '', + nutFoldfe: '', + ); + } + + private function hydrateGlucidesDetail(KiotaIncoCompo $model): GlucidesDetail + { + $cd = $model->getCarbohydratesDetail(); + + return new GlucidesDetail( + nutStarch: $cd?->getStarch() ?? '', + nutMaltdex: $cd?->getMaltodextrin() ?? '', + nutSucs: $cd?->getSucrose() ?? '', + nutGlus: $cd?->getGlucose() ?? '', + nutLacs: $cd?->getLactose() ?? '', + nutMals: $cd?->getMaltose() ?? '', + nutGalfb: $cd?->getGalactose() ?? '', + nutGHc: $cd?->getCarbohydrateHydrates() ?? '', + nutFrufb: $cd?->getBoundFructose() ?? '', + nutFrus: $cd?->getFreeFructose() ?? '', + nutOlsac: $cd?->getOligosaccharides() ?? '', + nutPolyl: $cd?->getPolyols() ?? '', + nutFibsol: $cd?->getSolubleFiber() ?? '', + nutInotl: $cd?->getInositol() ?? '', + ); + } + + private function hydrateAcideGras(KiotaIncoCompo $model): AcideGras + { + $fa = $model->getFattyAcids(); + + return new AcideGras( + nutFd: $fa?->getNutFd() ?? '', + nutChol: $fa?->getNutChol() ?? '', + nutAgt: $fa?->getNutAgt() ?? '', + nutFformiq: $fa?->getNutFformiq() ?? '', + nutFacetiq: $fa?->getNutFacetiq() ?? '', + nutFpropioniq: $fa?->getNutFpropioniq() ?? '', + nutFbutyriq: $fa?->getNutFbutyriq() ?? '', + nutFvaleriq: $fa?->getNutFvaleriq() ?? '', + nutFcaproiq: $fa?->getNutFcaproiq() ?? '', + nutFenatiq: $fa?->getNutFenatiq() ?? '', + nutFcapryliq: $fa?->getNutFcapryliq() ?? '', + nutFpelargoniq: $fa?->getNutFpelargoniq() ?? '', + nutFcapriq: $fa?->getNutFcapriq() ?? '', + nutFundecyliq: $fa?->getNutFundecyliq() ?? '', + nutFlauriq: $fa?->getNutFlauriq() ?? '', + nutFtridecyliq: $fa?->getNutFtridecyliq() ?? '', + nutFmyristiq: $fa?->getNutFmyristiq() ?? '', + nutFpentadecyliq: $fa?->getNutFpentadecyliq() ?? '', + nutF16d0: $fa?->getNutF16d0() ?? '', + nutFmargariq: $fa?->getNutFmargariq() ?? '', + nutFsteariq: $fa?->getNutFsteariq() ?? '', + nutFarachidiq: $fa?->getNutFarachidiq() ?? '', + nutFheneicosyliq: $fa?->getNutFheneicosyliq() ?? '', + nutFbeheniq: $fa?->getNutFbeheniq() ?? '', + nutFtricosiliq: $fa?->getNutFtricosiliq() ?? '', + nutFlignoceriq: $fa?->getNutFlignoceriq() ?? '', + nutFcarboceriq: $fa?->getNutFcarboceriq() ?? '', + nutFcerotiq: $fa?->getNutFcerotiq() ?? '', + nutFceroplastiq: $fa?->getNutFceroplastiq() ?? '', + nutFmontaniq: $fa?->getNutFmontaniq() ?? '', + nutFnonacosyliq: $fa?->getNutFnonacosyliq() ?? '', + nutFmelissiq: $fa?->getNutFmelissiq() ?? '', + nutFlaceroiq: $fa?->getNutFlaceroiq() ?? '', + nutFhentriacontyliq: $fa?->getNutFhentriacontyliq() ?? '', + nutFhextriacontyliq: $fa?->getNutFhextriacontyliq() ?? '', + nutFheptatriacontyliq: $fa?->getNutFheptatriacontyliq() ?? '', + nutFoctatriacontyliq: $fa?->getNutFoctatriacontyliq() ?? '', + nutFnonadylique: $fa?->getNutFnonadylique() ?? '', + nutFpsylliq: $fa?->getNutFpsylliq() ?? '', + nutFpalm: $fa?->getNutFpalm() ?? '', + nutFoleiq: $fa?->getNutFoleiq() ?? '', + nutF18d: $fa?->getNutF18d() ?? '', + nutF19d: $fa?->getNutF19d() ?? '', + nutF11eico: $fa?->getNutF11eico() ?? '', + nutFgeddiq: $fa?->getNutFgeddiq() ?? '', + nutFeruciq: $fa?->getNutFeruciq() ?? '', + nutFnervoniq: $fa?->getNutFnervoniq() ?? '', + nutFhyeniq: $fa?->getNutFhyeniq() ?? '', + nutFamscis: $fa?->getNutFamscis() ?? '', + nutF18d2cn6: $fa?->getNutF18d2cn6() ?? '', + nutF18d3n3: $fa?->getNutF18d3n3() ?? '', + nutF20d4: $fa?->getNutF20d4() ?? '', + nutF20d5n3: $fa?->getNutF20d5n3() ?? '', + nutF22d6n3: $fa?->getNutF22d6n3() ?? '', + nutFapun3: $fa?->getNutFapun3() ?? '', + nutFapun6: $fa?->getNutFapun6() ?? '', + nutFapun9: $fa?->getNutFapun9() ?? '', + nutFapucis: $fa?->getNutFapucis() ?? '', + nutFapulc: $fa?->getNutFapulc() ?? '', + ); + } + + private function hydrateAcidesAmines(KiotaIncoCompo $model): AcidesAmines + { + $aa = $model->getAminoAcids(); + + return new AcidesAmines( + nutAae: $aa?->getEssentialAminoAcids() ?? '', + nutCasn: $aa?->getCaseins() ?? '', + nutWhey: $aa?->getWheyProtein() ?? '', + ); + } + + private function hydrateComposesSpeciaux(KiotaIncoCompo $model): ComposesSpeciaux + { + $sc = $model->getSpecialCompounds(); + + return new ComposesSpeciaux( + nutCholn: $sc?->getCholine() ?? '', + nutCarnitine: $sc?->getCarnitine() ?? '', + nutTau: $sc?->getTaurine() ?? '', + nutNucleotide: $sc?->getNucleotides() ?? '', + ); + } +} diff --git a/src/Hydrator/IncoHydrator.php b/src/Hydrator/IncoHydrator.php new file mode 100644 index 0000000..ba4bc79 --- /dev/null +++ b/src/Hydrator/IncoHydrator.php @@ -0,0 +1,244 @@ +getIncoId() ?? '0'), + name: $model->getName() ?? '', + libelle: $model->getLabel() ?? '', + description: $model->getDescription() ?? '', + allegations: $this->hydrateAllegations($model), + sellArguments: $model->getSellArguments() ?? '', + preparationAdvices: $model->getPreparationAdvices() ?? '', + tips: $model->getTips(), + sellFamilly: $model->getSellFamilly(), + ingredients: $this->hydrateIngredients($model), + portions: $this->hydratePortions($model), + allergens: $this->hydrateAllergens($model), + brand: $this->hydrateBrand($model), + isSeasonal: $model->getIsSeasonal() ?? false, + weight: $this->hydrateWeight($model), + nutriscore: $this->hydrateNutriscore($model), + storageCondition: $this->hydrateStorageCondition($model), + additives: $this->hydrateAdditives($model), + compo: $this->hydrateCompoId($model), + createdAt: $model->getCreatedAt(), + updatedAt: $model->getUpdatedAt(), + ); + } + + /** + * @return PageOf + */ + public function toPage(IncoListResponse $response, Pagination $pagination): PageOf + { + $items = []; + foreach ($response->getIncoSheets() ?? [] as $wrapper) { + $inco = $wrapper->getInco(); + if ($inco !== null) { + $items[] = $this->toDto($inco); + } + } + + return new PageOf($this->hydratePagination($response->getPagination(), $pagination), $items); + } + + private function hydratePagination(?PaginationMeta $meta, Pagination $pagination): PaginationDto + { + return new PaginationDto( + page: $meta?->getPage() ?? $pagination->pageNumber(), + pageSize: $meta?->getPageSize() ?? $pagination->pageSize(), + totalItems: $meta?->getTotalItems() ?? 0, + totalPages: $meta?->getTotalPages() ?? 0, + ); + } + + private function hydrateAllegations(KiotaInco $model): Allegations + { + $m = $model->getAllegations(); + + return new Allegations( + allegations: $m?->getAllegations() ?? [], + health: $m?->getHealth() ?? [], + nutrition: $m?->getNutrition() ?? [], + other: $m?->getOther() ?? [], + ); + } + + private function hydrateCompoId(KiotaInco $model): ?IncoCompoId + { + $compoId = $model->getIncoCompoId(); + + return $compoId !== null && $compoId !== '' ? new IncoCompoId($compoId) : null; + } + + private function hydrateIngredients(KiotaInco $model): Ingredients + { + $data = $model->getIngredients()?->getAdditionalData(); + if (! is_array($data) || empty($data)) { + return new Ingredients([]); + } + + /** @var array $cleanGroups */ + $cleanGroups = []; + foreach ($data as $key => $lines) { + if (is_array($lines)) { + $cleanLines = $this->sanitizeLines($lines); + if (! empty($cleanLines)) { + $cleanGroups[$key] = $cleanLines; + } + } + } + + return new Ingredients($cleanGroups); + } + + /** + * @param mixed[] $lines + * @return string[] + */ + private function sanitizeLines(array $lines): array + { + $cleanLines = []; + foreach ($lines as $line) { + if (is_string($line) && mb_trim($line) !== '') { + $cleanLines[] = $line; + } + } + + return $cleanLines; + } + + private function hydratePortions(KiotaInco $model): ?PortionAmount + { + $portionAmount = $model->getPortions()?->getIncoPortionAmount(); + if ($portionAmount === null) { + return null; + } + + return PortionAmount::fromJson([ + 'lowPart' => $portionAmount->getLowPart() ?? 0, + 'highPart' => $portionAmount->getHighPart() ?? 0, + 'approximative' => $portionAmount->getApproximative() ?? false, + ]); + } + + /** + * @return Allergen[] + */ + private function hydrateAllergens(KiotaInco $model): array + { + $allergens = []; + $index = 1; + foreach ($model->getAllergens() ?? [] as $label) { + $trimmed = mb_trim($label); + if ($trimmed !== '') { + $allergens[] = new Allergen($index, $trimmed); + $index++; + } + } + + return $allergens; + } + + private function hydrateBrand(KiotaInco $model): Brand + { + $brandModel = $model->getBrand(); + $codeStr = $brandModel?->getCode(); + $codeVal = $codeStr !== null && mb_trim($codeStr) !== '' ? mb_trim($codeStr) : 'UNKNOWN'; + + $label = $brandModel?->getLabel() ?? ''; + $label = mb_trim($label) !== '' ? $label : 'Marque'; + + return new Brand(new BrandCode($codeVal), $label); + } + + private function hydrateWeight(KiotaInco $model): ?IncoWeight + { + $weightModel = $model->getWeight()?->getIncoWeight(); + if ($weightModel === null) { + return null; + } + + return new IncoWeight( + netUvc: $weightModel->getNetWeight() ?? 0, + brutUvc: $weightModel->getGrossWeight() ?? 0, + moyTheo: $weightModel->getTheoreticalAverage() ?? 0, + ); + } + + private function hydrateNutriscore(KiotaInco $model): ?Nutriscore + { + $grade = $model->getNutriscore()?->getIncoNutriscore()?->getGrade()?->value(); + if ($grade === null || $grade === '') { + return null; + } + + $upper = strtoupper($grade); + if (! in_array($upper, Nutriscore::VALID_GRADES, true)) { + return null; + } + + return new Nutriscore($upper); + } + + private function hydrateStorageCondition(KiotaInco $model): ?StorageCondition + { + $instructions = $model->getStorageCondition()?->getIncoStorageCondition()?->getInstructions(); + if ($instructions === null || mb_trim($instructions) === '') { + return null; + } + + return new StorageCondition($instructions); + } + + /** + * @return Additive[] + */ + private function hydrateAdditives(KiotaInco $model): array + { + $additives = []; + foreach ($model->getAdditives() ?? [] as $additiveModel) { + $code = $additiveModel->getCode(); + $label = $additiveModel->getLabel(); + if ($code !== null && $label !== null && mb_trim($label) !== '') { + try { + $additives[] = new Additive($code, $label); + } catch (\Throwable) { + continue; + } + } + } + + return $additives; + } +} diff --git a/src/Hydrator/IncoShortHydrator.php b/src/Hydrator/IncoShortHydrator.php new file mode 100644 index 0000000..7a77a8f --- /dev/null +++ b/src/Hydrator/IncoShortHydrator.php @@ -0,0 +1,58 @@ +getIncoId() ?? '0'), + name: $model->getName() ?? '', + libelle: $model->getLabel() ?? '', + description: '', + created_at: $model->getCreatedAt(), + updated_at: $model->getUpdatedAt(), + ); + } + + /** + * @return PageOf + */ + public function toPage(IncoListResponse $response, Pagination $pagination): PageOf + { + $items = []; + foreach ($response->getIncoSheets() ?? [] as $wrapper) { + $incoShort = $wrapper->getIncoShort(); + if ($incoShort !== null) { + $items[] = $this->toDto($incoShort); + } + } + + return new PageOf($this->hydratePagination($response->getPagination(), $pagination), $items); + } + + private function hydratePagination(?PaginationMeta $meta, Pagination $pagination): PaginationDto + { + return new PaginationDto( + page: $meta?->getPage() ?? $pagination->pageNumber(), + pageSize: $meta?->getPageSize() ?? $pagination->pageSize(), + totalItems: $meta?->getTotalItems() ?? 0, + totalPages: $meta?->getTotalPages() ?? 0, + ); + } +}