Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable suffixes in second name segment #19

Merged
merged 1 commit into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Mapper/SuffixMapper.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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