Skip to content

Commit

Permalink
Detect last name prefixes in combined lastname parts (fixes #30)
Browse files Browse the repository at this point in the history
This enables the detection of lastnames combined via a dash with
the prefix to a following lastname, e.g. in "Etje Heijdanus-De Boer".
At this point the entire part is treated as Lastname as there is
no support for re-combining last name parts or prefixes.
This means the 'De' prefix in 'De Boer' above cannot be accessed
individually.
  • Loading branch information
wyrfel committed Nov 6, 2019
1 parent 3bf2c11 commit 90e8adc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
27 changes: 23 additions & 4 deletions src/Mapper/LastnameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ protected function mapParts(array $parts): array
continue;
}

if ($this->isCombinedWithPrefix($part)) {
$parts[$k] = new Lastname($part);
continue;
}

if ($this->shouldStopMapping($parts, $k)) {
break;
}
Expand All @@ -78,6 +83,17 @@ protected function mapParts(array $parts): array
return $parts;
}

private function isCombinedWithPrefix(string $part): bool
{
$pos = strpos($part, '-');

if (false === $pos) {
return false;
}

return $this->isPrefix(substr($part, $pos + 1));
}

/**
* skip through the parts we want to ignore and return the start index
*
Expand All @@ -98,7 +114,7 @@ protected function skipIgnoredParts(array $parts): int
}

/**
* indicates if we should stop mapping at the give index $k
* indicates if we should stop mapping at the given index $k
*
* the assumption is that lastname parts have already been found
* but we want to see if we should add more parts
Expand All @@ -113,11 +129,15 @@ protected function shouldStopMapping(array $parts, int $k): bool
return true;
}

if ($parts[$k + 1] instanceof LastnamePrefix) {
$lastPart = $parts[$k + 1];

if ($lastPart instanceof LastnamePrefix) {
return true;
}

return strlen($parts[$k + 1]->getValue()) >= 3;


return strlen($lastPart->getValue()) >= 3;
}

/**
Expand All @@ -135,7 +155,6 @@ protected function isIgnoredPart($part) {
*
* if the mapping did not derive any lastname this is called to transform
* any previously ignored parts into lastname parts
* the parts array is still reversed at this point
*
* @param array $parts
* @return array
Expand Down
10 changes: 1 addition & 9 deletions src/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,7 @@ public function getAll(bool $format = false): array
*/
public function getGivenName(): string
{
$fullNameParts = [];

foreach ($this->parts as $part) {
if ($part instanceof GivenNamePart) {
$fullNameParts[] = $part->normalize();
}
}

return implode(' ', $fullNameParts);
return $this->export('GivenNamePart');
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,13 @@ public function provider()
'lastname' => 'Du',
'firstname' => 'Yumeng',
]
],
[
'Etje Heijdanus-De Boer',
[
'firstname' => 'Etje',
'lastname' => 'Heijdanus-De Boer',
]
]
];
}
Expand Down

0 comments on commit 90e8adc

Please sign in to comment.