Skip to content

Commit

Permalink
Merge pull request #11 from OS2web/f/OS2FORMS-359-cpr-lookup
Browse files Browse the repository at this point in the history
Avoiding TypeError or wrong CPR code of the child
  • Loading branch information
stankut authored Jun 2, 2023
2 parents c4e3f4b + ae8cb5a commit 38e166b
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/Plugin/os2web/DataLookup/ServiceplatformenCPRExtended.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,23 @@ public function lookup($cpr, $allowCprTestModeReplace = TRUE) {
$cprResult->setPostalCode($address->aktuelAdresse->postnummer ?? '');
$cprResult->setCity($address->aktuelAdresse->postdistrikt ?? '');
$cprResult->setMunicipalityCode($address->aktuelAdresse->kommunekode ?? '');
$cprResult->setAddress($address->aktuelAdresse->standardadresse ?? '');

// Composing full address in one line.
$address = $cprResult->getStreet();
if ($cprResult->getHouseNr()) {
$address .= ' ' . $cprResult->getHouseNr();
}
if ($cprResult->getFloor()) {
$address .= ' ' . $cprResult->getFloor();
}
if ($cprResult->getApartmentNr()) {
$address .= ' ' . $cprResult->getApartmentNr();
}
if ($cprResult->getPostalCode() && $cprResult->getCity()) {
$address .= ', ' . $cprResult->getPostalCode() . ' ' . $cprResult->getCity();
}

$cprResult->setAddress($address ?? '');
}

$relationship = $result['relationer'];
Expand All @@ -122,13 +138,20 @@ public function lookup($cpr, $allowCprTestModeReplace = TRUE) {
$relationship->barn = [$relationship->barn];
}

foreach ($relationship->barn as $child) {
$childCprResult = $this->lookup($child->personnummer, FALSE);

$children[] = [
'cpr' => $childCprResult->getCpr(),
'name' => $childCprResult->getName(),
foreach ($relationship->barn as $relationshipChild) {
// Sometimes CPR lookup can return no results, creating child without
// name.
$child = [
'cpr' => $relationshipChild->personnummer,
'name' => '',
];

$childCprResult = $this->lookup($relationshipChild->personnummer, FALSE);
if ($childCprResult->isSuccessful()) {
$child['name'] = $childCprResult->getName();
}

$children[] = $child;
}
}
$cprResult->setChildren($children);
Expand Down

0 comments on commit 38e166b

Please sign in to comment.