Skip to content

Commit

Permalink
Merge pull request #12 from theiconic/feature/allow-nicknames-at-end
Browse files Browse the repository at this point in the history
Allow nicknames to occur at the end of the entire string (fixes #8)
  • Loading branch information
wyrfel committed Aug 28, 2018
2 parents 28fc5ef + 70005a4 commit 0bf85f4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Mapper/AbstractMapper.php
Expand Up @@ -3,6 +3,7 @@
namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Nickname;

abstract class AbstractMapper
{
Expand Down
27 changes: 26 additions & 1 deletion src/Mapper/LastnameMapper.php
Expand Up @@ -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
Expand Down Expand Up @@ -52,6 +53,10 @@ protected function mapReversedParts(array $parts): array
continue;
}

if ($part instanceof Nickname) {
continue;
}

if ($part instanceof AbstractPart) {
break;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
}
8 changes: 8 additions & 0 deletions tests/ParserTest.php
Expand Up @@ -415,6 +415,14 @@ public function provider()
'lastname' => 'Müller',
]
],
[
'Charles Dixon (20th century)',
[
'firstname' => 'Charles',
'lastname' => 'Dixon',
'nickname' => '20Th Century'
]
],
[
'Smith, John Eric',
[
Expand Down

0 comments on commit 0bf85f4

Please sign in to comment.