Skip to content

Commit

Permalink
Merge pull request #9 from francislavoie/fix-7
Browse files Browse the repository at this point in the history
Fix #7, support for middlenames being last when lastname comes first
  • Loading branch information
wyrfel authored Jul 10, 2018
2 parents 06a8b18 + 1e3ab3a commit 28fc5ef
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
18 changes: 15 additions & 3 deletions src/Mapper/MiddlenameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function getSecondSegmentParser(): Parser
new NicknameMapper($this->getNicknameDelimiters()),
new InitialMapper(true),
new FirstnameMapper(),
new MiddlenameMapper(),
new MiddlenameMapper(true),
]);

return $parser;
Expand Down
17 changes: 15 additions & 2 deletions tests/Mapper/MiddlenameMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 9 additions & 1 deletion tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,15 @@ public function provider()
'firstname' => 'Markus',
'lastname' => 'Müller',
]
]
],
[
'Smith, John Eric',
[
'lastname' => 'Smith',
'firstname' => 'John',
'middlename' => 'Eric',
]
],
];
}

Expand Down

0 comments on commit 28fc5ef

Please sign in to comment.