Skip to content

Commit

Permalink
improve MiddlenameMapper to be less greedy and improve its unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrfel committed May 16, 2017
1 parent c4f174f commit a33cb5f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 31 deletions.
18 changes: 18 additions & 0 deletions src/Mapper/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,22 @@ protected function hasUnmappedPartsBefore(array $parts, $index)

return false;
}

/**
* @param string $type
* @param array $parts
* @return int|bool
*/
protected function findFirstMapped(string $type, array $parts)
{
$total = count($parts);

for ($i = 0; $i < $total; $i++) {
if ($parts[$i] instanceof $type) {
return $i;
}
}

return false;
}
}
16 changes: 6 additions & 10 deletions src/Mapper/FirstnameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function map(array $parts)
}

$length = count($parts);
$start = $this->getStartIndex($parts, $length);
$start = $this->getStartIndex($parts);

$pos = null;

Expand Down Expand Up @@ -68,20 +68,16 @@ protected function handleSinglePart($part)

/**
* @param array $parts
* @param int $total
* @return int
*/
protected function getStartIndex(array $parts, $total)
protected function getStartIndex(array $parts)
{
// skip to after salutation
$start = 0;
$index = $this->findFirstMapped(Salutation::class, $parts);

for ($i = 0; $i < $total; $i++) {
if ($parts[$i] instanceof Salutation) {
$start = $i + 1;
}
if (false === $index) {
return 0;
}

return $start;
return $index + 1;
}
}
27 changes: 6 additions & 21 deletions src/Mapper/MiddlenameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public function map(array $parts)

// skip to after salutation
$length = count($parts);
$start = $this->getStartIndex($parts, $length);
$start = $this->findFirstMapped(Firstname::class, $parts);

for ($k = $start; $k < $length; $k++) {
if (false === $start) {
return $parts;
}

for ($k = $start; $k < $length - 1; $k++) {
$part = $parts[$k];

if ($part instanceof Lastname) {
Expand All @@ -41,23 +45,4 @@ public function map(array $parts)

return $parts;
}

/**
* @param array $parts
* @param int $total
* @return int
*/
protected function getStartIndex(array $parts, $total)
{
// skip to after salutation
$start = 0;

for ($i = 0; $i < $total; $i++) {
if ($parts[$i] instanceof Firstname) {
$start = $i + 1;
}
}

return $start;
}
}
24 changes: 24 additions & 0 deletions tests/Mapper/MiddlenameMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public function provider()
new Lastname('Brown'),
],
],
[
'input' => [
'Mr',
new Firstname('James'),
'Tiberius',
'Kirk',
],
'expectation' => [
'Mr',
new Firstname('James'),
new Middlename('Tiberius'),
'Kirk',
],
],
[
'input' => [
'Albert',
'Einstein',
],
'expectation' => [
'Albert',
'Einstein',
],
],
];
}
}

0 comments on commit a33cb5f

Please sign in to comment.