Skip to content

Commit 38e166b

Browse files
authored
Merge pull request #11 from OS2web/f/OS2FORMS-359-cpr-lookup
Avoiding TypeError or wrong CPR code of the child
2 parents c4e3f4b + ae8cb5a commit 38e166b

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/Plugin/os2web/DataLookup/ServiceplatformenCPRExtended.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,23 @@ public function lookup($cpr, $allowCprTestModeReplace = TRUE) {
111111
$cprResult->setPostalCode($address->aktuelAdresse->postnummer ?? '');
112112
$cprResult->setCity($address->aktuelAdresse->postdistrikt ?? '');
113113
$cprResult->setMunicipalityCode($address->aktuelAdresse->kommunekode ?? '');
114-
$cprResult->setAddress($address->aktuelAdresse->standardadresse ?? '');
114+
115+
// Composing full address in one line.
116+
$address = $cprResult->getStreet();
117+
if ($cprResult->getHouseNr()) {
118+
$address .= ' ' . $cprResult->getHouseNr();
119+
}
120+
if ($cprResult->getFloor()) {
121+
$address .= ' ' . $cprResult->getFloor();
122+
}
123+
if ($cprResult->getApartmentNr()) {
124+
$address .= ' ' . $cprResult->getApartmentNr();
125+
}
126+
if ($cprResult->getPostalCode() && $cprResult->getCity()) {
127+
$address .= ', ' . $cprResult->getPostalCode() . ' ' . $cprResult->getCity();
128+
}
129+
130+
$cprResult->setAddress($address ?? '');
115131
}
116132

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

125-
foreach ($relationship->barn as $child) {
126-
$childCprResult = $this->lookup($child->personnummer, FALSE);
127-
128-
$children[] = [
129-
'cpr' => $childCprResult->getCpr(),
130-
'name' => $childCprResult->getName(),
141+
foreach ($relationship->barn as $relationshipChild) {
142+
// Sometimes CPR lookup can return no results, creating child without
143+
// name.
144+
$child = [
145+
'cpr' => $relationshipChild->personnummer,
146+
'name' => '',
131147
];
148+
149+
$childCprResult = $this->lookup($relationshipChild->personnummer, FALSE);
150+
if ($childCprResult->isSuccessful()) {
151+
$child['name'] = $childCprResult->getName();
152+
}
153+
154+
$children[] = $child;
132155
}
133156
}
134157
$cprResult->setChildren($children);

0 commit comments

Comments
 (0)