feat(hydrator): implement Kiota OpenAPI model hydrators
This commit is contained in:
parent
bbb3acc234
commit
ea17c0950a
5 changed files with 794 additions and 0 deletions
57
src/Hydrator/BrandHydrator.php
Normal file
57
src/Hydrator/BrandHydrator.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Uwil\ArcadiaApiPhpClient\Hydrator;
|
||||
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Brand;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\Pagination;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\Brand as KiotaBrand;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\BrandListResponse;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\PaginationMeta;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class BrandHydrator
|
||||
{
|
||||
public function toDto(KiotaBrand $model): Brand
|
||||
{
|
||||
$code = $model->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<Brand>
|
||||
*/
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
81
src/Hydrator/EmployeeHydrator.php
Normal file
81
src/Hydrator/EmployeeHydrator.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Uwil\ArcadiaApiPhpClient\Hydrator;
|
||||
|
||||
use Microsoft\Kiota\Abstractions\Types\Date;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Employee;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ArcadiaEmployeeId;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\EstablishmentCode;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ServiceId;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\SiteId;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\Pagination;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\Employee as KiotaEmployee;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\EmployeeListResponse;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\PaginationMeta;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class EmployeeHydrator
|
||||
{
|
||||
public function toDto(KiotaEmployee $model): Employee
|
||||
{
|
||||
$matricule = $model->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<Employee>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
354
src/Hydrator/IncoCompoHydrator.php
Normal file
354
src/Hydrator/IncoCompoHydrator.php
Normal file
|
|
@ -0,0 +1,354 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Uwil\ArcadiaApiPhpClient\Hydrator;
|
||||
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\AcideGras;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\AcidesAmines;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\ApportsReference;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\ComposesSpeciaux;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\Energie;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\FruitLegumeNoix;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\GlucidesDetail;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\Identification;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\MacronutrimentsPortion;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\MacronutrimentsRef;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\Mineraux;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\NutriScore;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\PointsNutriScore;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo\Vitamines;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoCompoId;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompo as KiotaIncoCompo;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @phpcs:disable Generic.Metrics.CyclomaticComplexity
|
||||
*/
|
||||
final class IncoCompoHydrator
|
||||
{
|
||||
public function toDto(KiotaIncoCompo $model): IncoCompo
|
||||
{
|
||||
$id = new IncoCompoId($model->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() ?? '',
|
||||
);
|
||||
}
|
||||
}
|
||||
244
src/Hydrator/IncoHydrator.php
Normal file
244
src/Hydrator/IncoHydrator.php
Normal file
|
|
@ -0,0 +1,244 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Uwil\ArcadiaApiPhpClient\Hydrator;
|
||||
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Brand;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Additive;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Allegations;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Allergen;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\IncoWeight;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Ingredients;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Nutriscore;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\PortionAmount;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\StorageCondition;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoCompoId;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\Pagination;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\Inco as KiotaInco;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\PaginationMeta;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class IncoHydrator
|
||||
{
|
||||
public function toDto(KiotaInco $model): Inco
|
||||
{
|
||||
return new Inco(
|
||||
id: new IncoId($model->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<Inco>
|
||||
*/
|
||||
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<int|string, string[]> $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;
|
||||
}
|
||||
}
|
||||
58
src/Hydrator/IncoShortHydrator.php
Normal file
58
src/Hydrator/IncoShortHydrator.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Uwil\ArcadiaApiPhpClient\Hydrator;
|
||||
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoShort;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\Pagination;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoShort as KiotaIncoShort;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\PaginationMeta;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class IncoShortHydrator
|
||||
{
|
||||
public function toDto(KiotaIncoShort $model): IncoShort
|
||||
{
|
||||
return new IncoShort(
|
||||
id: new IncoId($model->getIncoId() ?? '0'),
|
||||
name: $model->getName() ?? '',
|
||||
libelle: $model->getLabel() ?? '',
|
||||
description: '',
|
||||
created_at: $model->getCreatedAt(),
|
||||
updated_at: $model->getUpdatedAt(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PageOf<IncoShort>
|
||||
*/
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue