From 8524ef3e57e2f6806f5a6142f9168cd9dd9a87b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Sun, 15 Oct 2023 23:23:05 +0200 Subject: [PATCH] I don't understand why but for this 'res' source, the country code is returned as "1", yes a string, instead of "cz", so let's deal with that It could be easily removed in the future, and there's a test for it (the one for the company with id 00256081), so if it is removed and it shouldn't be, I should know about it rather soon. Fix #242 --- site/app/CompanyInfo/CompanyRegisterAres.php | 21 ++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/site/app/CompanyInfo/CompanyRegisterAres.php b/site/app/CompanyInfo/CompanyRegisterAres.php index 3659a47d2..9adabe8fa 100644 --- a/site/app/CompanyInfo/CompanyRegisterAres.php +++ b/site/app/CompanyInfo/CompanyRegisterAres.php @@ -61,8 +61,9 @@ public function getDetails(string $companyId): CompanyInfoDetails 'psc' => Expect::int(), 'kodStatu' => Expect::string(), ])->otherItems(), + 'primarniZdroj' => Expect::string(), ])->otherItems(); - /** @var object{ico:string, dic:string|null, obchodniJmeno:string, sidlo:object{nazevObce:string, nazevUlice:string, cisloDomovni:int, cisloOrientacni:int, cisloOrientacniPismeno:string, psc:int, kodStatu:string}} $data */ + /** @var object{ico:string, dic:string|null, obchodniJmeno:string, sidlo:object{nazevObce:string, nazevUlice:string, cisloDomovni:int, cisloOrientacni:int, cisloOrientacniPismeno:string, psc:int, kodStatu:string}, primarniZdroj:string|null} $data */ $data = $this->schemaProcessor->process($schema, Json::decode($content)); } catch (JsonException | ValidationException $e) { throw new CompanyInfoException($e->getMessage(), previous: $e); @@ -84,7 +85,7 @@ public function getDetails(string $companyId): CompanyInfoDetails $streetAndNumber, $data->sidlo->nazevObce, (string)$data->sidlo->psc, - strtolower($data->sidlo->kodStatu), + $this->resolveCountryCode($data->primarniZdroj, $data->sidlo->kodStatu, $companyId), ); } @@ -122,4 +123,20 @@ private function formatStreet(?string $city, ?string $street, ?int $houseNumber, return $result; } + + /** + * @throws CompanyInfoException + */ + private function resolveCountryCode(?string $source, string $code, string $companyId): string + { + if ($source === 'res') { + if ($code === '1') { + return 'cz'; + } else { + throw new CompanyInfoException("Invalid country code {$code} from source {$source} for company {$companyId}"); + } + } + return strtolower($code); + } + }