Skip to content

Commit

Permalink
feat(Resolve address): use field dalsiUdaje
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Nov 8, 2024
1 parent b15be7e commit 329e162
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
51 changes: 47 additions & 4 deletions src/Ares/Core/JsonToDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class JsonToDataTransformer
{
private const RegisterPriority = ['rzp', 'res', 'vr'];


public function transform(stdClass $json): Data
{
Expand All @@ -26,8 +28,19 @@ public function transform(stdClass $json): Data

$addressExists = isset($json->sidlo) && self::updateAddress($data, $json->sidlo);

if ($addressExists === false && isset($json->dalsiUdaje[0]->sidlo[0]->sidlo)) {
$addressExists = self::updateAddress($data, $json->dalsiUdaje[0]->sidlo[0]->sidlo);
if ($addressExists === false) {
$additionalData = isset($json->dalsiUdaje) ? self::prepareForAddress($json->dalsiUdaje) : [];
if ($additionalData !== []) {
foreach (self::RegisterPriority as $register) {
$key = self::keyForAddress($register, $json->pravniForma);
if (isset($additionalData[$key])) {
$addressExists = self::updateAddress($data, $additionalData[$key]);
if ($addressExists === true) {
break;
}
}
}
}
}

if ($addressExists === false && isset($json->sidlo->textovaAdresa)) {
Expand Down Expand Up @@ -71,8 +84,11 @@ private static function updateAddress(Data $data, stdClass $sidlo): bool

private static function isAddressFilled(Data $data): bool
{
return $data->zip !== null
|| $data->street !== null
if ($data->zip === null) {
return false;
}

return $data->street !== null
|| $data->country !== null
|| $data->country_code !== null
|| $data->city !== null
Expand All @@ -82,4 +98,31 @@ private static function isAddressFilled(Data $data): bool
|| $data->house_number !== null;
}


/**
* @param array<stdClass> $dalsiUdaje
* @return array<stdClass>
*/
private static function prepareForAddress(array $dalsiUdaje): array
{
$out = [];
foreach ($dalsiUdaje as $record) {
$x = self::keyForAddress($record->datovyZdroj, $record->pravniForma);
foreach ($record->sidlo ?? [] as $sidlo) {
if ($sidlo?->primarniZaznam === true && isset($sidlo->sidlo)) {
$out[$x] = $sidlo->sidlo;
break;
}
}
}

return $out;
}


private static function keyForAddress(string $datovyZdroj, string $pravniForma): string
{
return "$datovyZdroj|$pravniForma";
}

}
44 changes: 44 additions & 0 deletions tests/fixtures/ares/26005492.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"active": true,
"city": "Olešnice",
"company": "TANUS s.r.o.",
"created": "2004-02-28T00:00:00+01:00",
"dissolved": null,
"city_district": "Hoděčín",
"city_post": null,
"in": "26005492",
"is_person": false,
"legal_form_code": 112,
"house_number": "26",
"street": null,
"district": "Rychnov nad Kněžnou",
"tin": "CZ26005492",
"vat_payer": true,
"zip": "51721",
"country": "Česká republika",
"country_code": "CZ",
"nace": [
"74",
"461",
"772",
"791",
"47790",
"49410"
],
"sources": {
"stavZdrojeVr": true,
"stavZdrojeRes": true,
"stavZdrojeRzp": true,
"stavZdrojeNrpzs": "NEEXISTUJICI",
"stavZdrojeRpsh": "NEEXISTUJICI",
"stavZdrojeRcns": "NEEXISTUJICI",
"stavZdrojeSzr": "NEEXISTUJICI",
"stavZdrojeDph": true,
"stavZdrojeSd": "NEEXISTUJICI",
"stavZdrojeIr": "NEEXISTUJICI",
"stavZdrojeCeu": "NEEXISTUJICI",
"stavZdrojeRs": "NEEXISTUJICI",
"stavZdrojeRed": true,
"stavZdrojeMonitor": "NEEXISTUJICI"
}
}
1 change: 1 addition & 0 deletions tests/src/E2E/Ares/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected static function getMask(): string
protected function provideCore(): array
{
return [
['26005492'], // read address from sidlo
['26577321'], // address from dalsiUdaje[0]->sidlo[0]->sidlo
['25528351'], // diff address
['67909442'], // create date does not exist
Expand Down

0 comments on commit 329e162

Please sign in to comment.