diff --git a/src/Mapper/SuffixMapper.php b/src/Mapper/SuffixMapper.php index 74a732a..49206cc 100644 --- a/src/Mapper/SuffixMapper.php +++ b/src/Mapper/SuffixMapper.php @@ -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; } /** @@ -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)) { diff --git a/src/Parser.php b/src/Parser.php index a99f5fb..6f86c5d 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -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(), @@ -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(), @@ -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; diff --git a/tests/Mapper/SuffixMapperTest.php b/tests/Mapper/SuffixMapperTest.php index c90b689..629aa64 100644 --- a/tests/Mapper/SuffixMapperTest.php +++ b/tests/Mapper/SuffixMapperTest.php @@ -28,6 +28,10 @@ public function provider() 'Blueberg', new Suffix('PhD'), ], + [ + 'matchSinglePart' => false, + 'reservedParts' => 2, + ] ], [ 'input' => [ @@ -40,6 +44,10 @@ public function provider() 'Alfred', new Suffix('III'), ], + [ + 'matchSinglePart' => false, + 'reservedParts' => 2, + ] ], [ 'input' => [ @@ -52,6 +60,10 @@ public function provider() new Lastname('Smith'), new Suffix('Senior'), ], + [ + 'matchSinglePart' => false, + 'reservedParts' => 2, + ] ], [ 'input' => [ @@ -64,6 +76,10 @@ public function provider() new Firstname('James'), 'Norrington', ], + [ + 'matchSinglePart' => false, + 'reservedParts' => 2, + ] ], [ 'input' => [ @@ -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); } } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index dc6e5be..ced8d23 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -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', ] ] ];