Skip to content

Commit

Permalink
cleanup; add more tests; improve handling of edge cases;
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrfel committed Dec 14, 2016
1 parent c49e66c commit 491547b
Show file tree
Hide file tree
Showing 26 changed files with 721 additions and 463 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "4.4.*",
"phpunit/phpunit": "^5.0",
"satooshi/php-coveralls": "^1.0"
},
"autoload": {
"psr-4": {
"TheIconic\\NameParser\\": "src/"
"TheIconic\\NameParser\\": ["src/", "tests/"]
}
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>
<testsuites>
<testsuite name="The Iconic Name Parser Test Suite">
<directory>./tests/TheIconic/NameParser</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
5 changes: 3 additions & 2 deletions src/Mapper/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

abstract class AbstractMapper
{

/**
* @var array
*/
protected $options = [];

/**
Expand All @@ -26,5 +28,4 @@ public function __construct(array $options = null)
* @return array $parts - the mapped parts
*/
abstract public function map(array $parts);

}
54 changes: 35 additions & 19 deletions src/Mapper/FirstnameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class FirstnameMapper extends AbstractMapper
{

/**
* map firstnames in parts array
*
Expand All @@ -19,23 +18,11 @@ class FirstnameMapper extends AbstractMapper
*/
public function map(array $parts) {
if (count($parts) < 2) {
if ($parts[0] instanceof AbstractPart) {
return $parts;
}

$parts[0] = new Firstname($parts[0]);

return $parts;
return [$this->handleSinglePart($parts[0])];
}

// skip to after salutation
$length = count($parts);
$start = 0;
for ($i = 0; $i < $length; $i++) {
if ($parts[$i] instanceof Salutation) {
$start = $i + 1;
}
}
$start = $this->getStartIndex($parts, $length);

$pos = null;

Expand All @@ -46,10 +33,8 @@ public function map(array $parts) {
break;
}

if ($part instanceof Initial) {
if (null === $pos) {
$pos = $k;
}
if ($part instanceof Initial && null === $pos) {
$pos = $k;
}

if ($part instanceof AbstractPart) {
Expand All @@ -67,4 +52,35 @@ public function map(array $parts) {
return $parts;
}

/**
* @param $part
* @return Firstname
*/
protected function handleSinglePart($part)
{
if ($part instanceof AbstractPart) {
return $part;
}

return new Firstname($part);
}

/**
* @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 Salutation) {
$start = $i + 1;
}
}

return $start;
}
}
6 changes: 3 additions & 3 deletions src/Mapper/InitialMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Initial;

// single letter, possibly followed by a period
/**
* single letter, possibly followed by a period
*/
class InitialMapper extends AbstractMapper
{

/**
* map intials in parts array
*
Expand All @@ -28,5 +29,4 @@ function map(array $parts) {

return $parts;
}

}
5 changes: 1 addition & 4 deletions src/Mapper/LastnameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class LastnameMapper extends AbstractMapper
{

/**
* @var array options
*/
Expand Down Expand Up @@ -67,8 +66,7 @@ public function map(array $parts) {
*/
protected function hasUnmappedPartsBefore(array $parts, $index)
{
foreach ($parts as $k => $part)
{
foreach ($parts as $k => $part) {
if ($k === $index) {
break;
}
Expand All @@ -80,5 +78,4 @@ protected function hasUnmappedPartsBefore(array $parts, $index)

return false;
}

}
26 changes: 19 additions & 7 deletions src/Mapper/MiddlenameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class MiddlenameMapper extends AbstractMapper
{

/**
* map middlenames in the parts array
*
Expand All @@ -23,12 +22,7 @@ public function map(array $parts) {

// skip to after salutation
$length = count($parts);
$start = 0;
for ($i = 0; $i < $length; $i++) {
if ($parts[$i] instanceof Firstname) {
$start = $i + 1;
}
}
$start = $this->getStartIndex($parts, $length);

for ($k = $start; $k < $length; $k++) {
$part = $parts[$k];
Expand All @@ -47,4 +41,22 @@ 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;
}
}
22 changes: 17 additions & 5 deletions src/Mapper/NicknameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,40 @@
use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Nickname;

// single letter, possibly followed by a period
class NicknameMapper extends AbstractMapper
{

/**
* map nicknames in the parts array
*
* @param array $parts the name parts
* @return array the mapped parts
*/
function map(array $parts) {
$isEncapsulated = false;

foreach ($parts as $k => $part) {
if ($part instanceof AbstractPart) {
continue;
}

if (preg_match('/^[\(\[\<\{].*[\)\]\>\}]$/', $part)) {
$parts[$k] = new Nickname(substr($part, 1, -1));
if (preg_match('/^[\(\[\<\{]/', $part)) {
$isEncapsulated = true;

$part = substr($part, 1);
}

$addPart = $isEncapsulated;

if (preg_match('/[\)\]\>\}]$/', $part)) {
$isEncapsulated = false;
$part = substr($part, 0, -1);
}

if ($addPart) {
$parts[$k] = new Nickname($part);
}
}

return $parts;
}

}
2 changes: 0 additions & 2 deletions src/Mapper/SalutationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class SalutationMapper extends AbstractMapper
{

/**
* map salutations in the parts array
*
Expand All @@ -27,5 +26,4 @@ function map(array $parts) {

return $parts;
}

}
12 changes: 11 additions & 1 deletion src/Mapper/SuffixMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

class SuffixMapper extends AbstractMapper
{
/**
* @var array options
*/
protected $options = [
'match_single' => false,
];

/**
* map suffixes in the parts array
Expand All @@ -15,6 +21,11 @@ class SuffixMapper extends AbstractMapper
* @return array the mapped parts
*/
function map(array $parts) {
if ($this->options['match_single'] && count($parts) == 1 && Suffix::isSuffix($parts[0])) {
$parts[0] = new Suffix($parts[0]);
return $parts;
}

$start = count($parts) - 1;

for ($k = $start; $k > 1; $k--) {
Expand All @@ -33,5 +44,4 @@ function map(array $parts) {

return $parts;
}

}
6 changes: 4 additions & 2 deletions src/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class Name
{

/**
* @var array the parts that make up this name
*/
protected $parts = [];

/**
* constructor takes the array of parts this name consists of
*
* @param array|null $parts
*/
public function __construct(array $parts = null)
Expand Down Expand Up @@ -46,6 +46,9 @@ public function getParts()
return $this->parts;
}

/**
* @return array
*/
public function getAll()
{
$results = [];
Expand Down Expand Up @@ -149,5 +152,4 @@ protected function export($type)

return implode(' ', $matched);
}

}
Loading

0 comments on commit 491547b

Please sign in to comment.