From 1e3ab3aee5ce5a04d231d1970b28923ee346fdc8 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 9 Jul 2018 10:49:52 -0400 Subject: [PATCH] Fix #7, support for middlenames being last when lastname comes first --- src/Mapper/MiddlenameMapper.php | 18 +++++++++++++++--- src/Parser.php | 2 +- tests/Mapper/MiddlenameMapperTest.php | 17 +++++++++++++++-- tests/ParserTest.php | 10 +++++++++- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/Mapper/MiddlenameMapper.php b/src/Mapper/MiddlenameMapper.php index 18e0735..be129b7 100644 --- a/src/Mapper/MiddlenameMapper.php +++ b/src/Mapper/MiddlenameMapper.php @@ -9,6 +9,13 @@ class MiddlenameMapper extends AbstractMapper { + protected $mapWithoutLastname = false; + + public function __construct(bool $mapWithoutLastname = false) + { + $this->mapWithoutLastname = $mapWithoutLastname; + } + /** * map middlenames in the parts array * @@ -17,7 +24,10 @@ class MiddlenameMapper extends AbstractMapper */ public function map(array $parts): array { - if (count($parts) < 3) { + // If we don't expect a lastname, match a mimimum of 2 parts + $minumumParts = ($this->mapWithoutLastname ? 2 : 3); + + if (count($parts) < $minumumParts) { return $parts; } @@ -37,9 +47,11 @@ public function map(array $parts): array */ protected function mapFrom($start, $parts): array { - $length = count($parts); + // If we don't expect a lastname, include the last part, + // otherwise skip the last (-1) because it should be a lastname + $length = count($parts) - ($this->mapWithoutLastname ? 0 : 1); - for ($k = $start; $k < $length - 1; $k++) { + for ($k = $start; $k < $length; $k++) { $part = $parts[$k]; if ($part instanceof Lastname) { diff --git a/src/Parser.php b/src/Parser.php index e019f99..c73f3fe 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -122,7 +122,7 @@ protected function getSecondSegmentParser(): Parser new NicknameMapper($this->getNicknameDelimiters()), new InitialMapper(true), new FirstnameMapper(), - new MiddlenameMapper(), + new MiddlenameMapper(true), ]); return $parser; diff --git a/tests/Mapper/MiddlenameMapperTest.php b/tests/Mapper/MiddlenameMapperTest.php index 4dd4cec..a301159 100644 --- a/tests/Mapper/MiddlenameMapperTest.php +++ b/tests/Mapper/MiddlenameMapperTest.php @@ -77,11 +77,24 @@ public function provider() 'Einstein', ], ], + [ + 'input' => [ + new Firstname('James'), + 'Tiberius', + ], + 'expectation' => [ + new Firstname('James'), + new Middlename('Tiberius'), + ], + 'arguments' => [ + true + ], + ], ]; } - protected function getMapper() + protected function getMapper($mapWithoutLastname = false) { - return new MiddlenameMapper(); + return new MiddlenameMapper($mapWithoutLastname); } } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 1791bcf..5264bcb 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -414,7 +414,15 @@ public function provider() 'firstname' => 'Markus', 'lastname' => 'Müller', ] - ] + ], + [ + 'Smith, John Eric', + [ + 'lastname' => 'Smith', + 'firstname' => 'John', + 'middlename' => 'Eric', + ] + ], ]; }