Skip to content

Commit

Permalink
Merge pull request #19 from theiconic/feature/enable-suffixes-in-seco…
Browse files Browse the repository at this point in the history
…nd-segment

Enable suffixes in second name segment
  • Loading branch information
wyrfel committed Jun 3, 2019
2 parents 9a25993 + 71fc4f4 commit dbffe73
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/Mapper/SuffixMapper.php
Expand Up @@ -11,10 +11,13 @@ class SuffixMapper extends AbstractMapper

protected $matchSinglePart = false;

public function __construct(array $suffixes, bool $matchSinglePart = false)
protected $reservedParts = 2;

public function __construct(array $suffixes, bool $matchSinglePart = false, int $reservedParts = 2)
{
$this->suffixes = $suffixes;
$this->matchSinglePart = $matchSinglePart;
$this->reservedParts = $reservedParts;
}

/**
Expand All @@ -32,7 +35,7 @@ public function map(array $parts): array

$start = count($parts) - 1;

for ($k = $start; $k > 1; $k--) {
for ($k = $start; $k > $this->reservedParts - 1; $k--) {
$part = $parts[$k];

if (!$this->isSuffix($part)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Parser.php
Expand Up @@ -105,7 +105,7 @@ protected function getFirstSegmentParser(): Parser

$parser->setMappers([
new SalutationMapper($this->getSalutations(), $this->getMaxSalutationIndex()),
new SuffixMapper($this->getSuffixes()),
new SuffixMapper($this->getSuffixes(), false, 2),
new LastnameMapper($this->getPrefixes(), true),
new FirstnameMapper(),
new MiddlenameMapper(),
Expand All @@ -123,7 +123,7 @@ protected function getSecondSegmentParser(): Parser

$parser->setMappers([
new SalutationMapper($this->getSalutations(), $this->getMaxSalutationIndex()),
new SuffixMapper($this->getSuffixes(), true),
new SuffixMapper($this->getSuffixes(), true, 1),
new NicknameMapper($this->getNicknameDelimiters()),
new InitialMapper(true),
new FirstnameMapper(),
Expand All @@ -138,7 +138,7 @@ protected function getThirdSegmentParser(): Parser
$parser = new Parser();

$parser->setMappers([
new SuffixMapper($this->getSuffixes(), true),
new SuffixMapper($this->getSuffixes(), true, 0),
]);

return $parser;
Expand Down
79 changes: 77 additions & 2 deletions tests/Mapper/SuffixMapperTest.php
Expand Up @@ -28,6 +28,10 @@ public function provider()
'Blueberg',
new Suffix('PhD'),
],
[
'matchSinglePart' => false,
'reservedParts' => 2,
]
],
[
'input' => [
Expand All @@ -40,6 +44,10 @@ public function provider()
'Alfred',
new Suffix('III'),
],
[
'matchSinglePart' => false,
'reservedParts' => 2,
]
],
[
'input' => [
Expand All @@ -52,6 +60,10 @@ public function provider()
new Lastname('Smith'),
new Suffix('Senior'),
],
[
'matchSinglePart' => false,
'reservedParts' => 2,
]
],
[
'input' => [
Expand All @@ -64,6 +76,10 @@ public function provider()
new Firstname('James'),
'Norrington',
],
[
'matchSinglePart' => false,
'reservedParts' => 2,
]
],
[
'input' => [
Expand All @@ -76,14 +92,73 @@ public function provider()
new Firstname('James'),
new Lastname('Norrington'),
],
[
'matchSinglePart' => false,
'reservedParts' => 2,
]
],
[
'input' => [
'James',
'Norrington',
'Senior',
],
'expectation' => [
'James',
'Norrington',
new Suffix('Senior'),
],
[
false,
2,
]
],
[
'input' => [
'Norrington',
'Senior',
],
'expectation' => [
'Norrington',
'Senior',
],
[
false,
2,
]
],
[
'input' => [
new Lastname('Norrington'),
'Senior',
],
'expectation' => [
new Lastname('Norrington'),
new Suffix('Senior'),
],
[
false,
1,
]
],
[
'input' => [
'Senior',
],
'expectation' => [
new Suffix('Senior'),
],
[
true,
]
],
];
}

protected function getMapper()
protected function getMapper($matchSinglePart = false, $reservedParts = 2)
{
$english = new English();

return new SuffixMapper($english->getSuffixes());
return new SuffixMapper($english->getSuffixes(), $matchSinglePart, $reservedParts);
}
}
36 changes: 35 additions & 1 deletion tests/ParserTest.php
Expand Up @@ -451,7 +451,41 @@ public function provider()
[
'firstname' => 'James',
'initials' => 'J',
'lastname' => 'Ma'
'lastname' => 'Ma',
]
],
[
'Tiptree, James, Jr.',
[
'lastname' => 'Tiptree',
'firstname' => 'James',
'suffix' => 'Jr',
]
],
[
'Miller, Walter M., Jr.',
[
'lastname' => 'Miller',
'firstname' => 'Walter',
'initials' => 'M.',
'suffix' => 'Jr',
]
],
[
'Tiptree, James Jr.',
[
'lastname' => 'Tiptree',
'firstname' => 'James',
'suffix' => 'Jr',
]
],
[
'Miller, Walter M. Jr.',
[
'lastname' => 'Miller',
'firstname' => 'Walter',
'initials' => 'M.',
'suffix' => 'Jr',
]
]
];
Expand Down

0 comments on commit dbffe73

Please sign in to comment.