From 70005a4d8bf79c485a0fb61a8954d106f0fcc79b Mon Sep 17 00:00:00 2001 From: Andre Wyrwa Date: Tue, 28 Aug 2018 21:35:08 +1000 Subject: [PATCH] Allow nicknames to occur at the end of the entire string --- src/Mapper/AbstractMapper.php | 1 + src/Mapper/LastnameMapper.php | 27 ++++++++++++++++++++++++++- tests/ParserTest.php | 8 ++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Mapper/AbstractMapper.php b/src/Mapper/AbstractMapper.php index a326033..68811ca 100644 --- a/src/Mapper/AbstractMapper.php +++ b/src/Mapper/AbstractMapper.php @@ -3,6 +3,7 @@ namespace TheIconic\NameParser\Mapper; use TheIconic\NameParser\Part\AbstractPart; +use TheIconic\NameParser\Part\Nickname; abstract class AbstractMapper { diff --git a/src/Mapper/LastnameMapper.php b/src/Mapper/LastnameMapper.php index 707efd3..0326e85 100644 --- a/src/Mapper/LastnameMapper.php +++ b/src/Mapper/LastnameMapper.php @@ -6,6 +6,7 @@ use TheIconic\NameParser\Part\AbstractPart; use TheIconic\NameParser\Part\Lastname; use TheIconic\NameParser\Part\LastnamePrefix; +use TheIconic\NameParser\Part\Nickname; use TheIconic\NameParser\Part\Suffix; class LastnameMapper extends AbstractMapper @@ -52,6 +53,10 @@ protected function mapReversedParts(array $parts): array continue; } + if ($part instanceof Nickname) { + continue; + } + if ($part instanceof AbstractPart) { break; } @@ -80,7 +85,7 @@ protected function mapReversedParts(array $parts): array */ protected function isFollowedByLastnamePart(array $parts, int $index): bool { - $next = $index + 1; + $next = $this->skipNicknameParts($parts, $index + 1); return (isset($parts[$next]) && $parts[$next] instanceof Lastname); } @@ -118,4 +123,24 @@ protected function isPrefix($word): bool { return (array_key_exists($this->getKey($word), $this->prefixes)); } + + /** + * find the next non-nickname index in parts + * + * @param $parts + * @param $startIndex + * @return int|void + */ + protected function skipNicknameParts($parts, $startIndex) + { + $total = count($parts); + + for ($i = $startIndex; $i < $total; $i++) { + if (!($parts[$i] instanceof Nickname)) { + return $i; + } + } + + return $total - 1; + } } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 5264bcb..090fcba 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -415,6 +415,14 @@ public function provider() 'lastname' => 'Müller', ] ], + [ + 'Charles Dixon (20th century)', + [ + 'firstname' => 'Charles', + 'lastname' => 'Dixon', + 'nickname' => '20Th Century' + ] + ], [ 'Smith, John Eric', [