Skip to content

Commit

Permalink
Merge pull request #31 from theiconic/feature/detect-lastname-prefix-…
Browse files Browse the repository at this point in the history
…in-combined-parts

Detect last name prefixes in combined lastname parts (fixes #30)
  • Loading branch information
wyrfel authored Nov 6, 2019
2 parents 87b10e6 + 3432592 commit 1961d10
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
54 changes: 48 additions & 6 deletions src/Mapper/LastnameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ protected function mapParts(array $parts): array
}

if ($this->isFollowedByLastnamePart($parts, $k)) {
if ($this->isApplicablePrefix($parts, $k)) {
$parts[$k] = new LastnamePrefix($part, $this->prefixes[$this->getKey($part)]);
if ($mapped = $this->mapAsPrefixIfPossible($parts, $k)) {
$parts[$k] = $mapped;
continue;
}

Expand All @@ -78,6 +78,45 @@ protected function mapParts(array $parts): array
return $parts;
}

/**
* try to map this part as a lastname prefix or as a combined
* lastname part containing a prefix
*
* @param array $parts
* @param int $k
* @return Lastname|null
*/
private function mapAsPrefixIfPossible(array $parts, int $k): ?Lastname
{
if ($this->isApplicablePrefix($parts, $k)) {
return new LastnamePrefix($parts[$k], $this->prefixes[$this->getKey($parts[$k])]);
}

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

return null;
}

/**
* check if the given part is a combined lastname part
* that ends in a lastname prefix
*
* @param string $part
* @return bool
*/
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 +137,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 +152,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 +178,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 @@ -527,6 +527,13 @@ public function provider()
'lastname' => 'Judy',
'salutation' => 'Her Honour Mrs.'
]
],
[
'Etje Heijdanus-De Boer',
[
'firstname' => 'Etje',
'lastname' => 'Heijdanus-De Boer',
]
]
];
}
Expand Down

0 comments on commit 1961d10

Please sign in to comment.