Skip to content

Commit

Permalink
refactor and improvements; fix tests; add docblocks;
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrfel committed May 9, 2015
1 parent 6c7e20b commit fafda79
Show file tree
Hide file tree
Showing 19 changed files with 666 additions and 293 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
<?php
$parser = new TheIconic\NameParser\Parser();
$parser->init();
$name = $parser->parse($name);
echo $name->getSalutation();
echo $name->getFirstname();
echo $name->getLastname();
echo $name->getMiddlename();
echo $name->getNickname();
echo $name->getInitials();
echo $name->getSuffix();
```
20 changes: 20 additions & 0 deletions src/Mapper/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
abstract class AbstractMapper
{

protected $options = [];

/**
* constructor allows passing of options
*
* @param array $options
*/
public function __construct(array $options = null)
{
if (null !== $options) {
$this->options = array_merge($this->options, $options);
}
}

/**
* implements the mapping of parts
*
* @param array $parts - the name parts
* @return array $parts - the mapped parts
*/
abstract public function map(array $parts);

}
27 changes: 19 additions & 8 deletions src/Mapper/FirstnameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
use TheIconic\NameParser\Part\Initial;
use TheIconic\NameParser\Part\Salutation;

class FirstnameMapper
class FirstnameMapper extends AbstractMapper
{

public function map($parts) {
/**
* map firstnames in parts array
*
* @param array $parts the parts
* @return array the mapped parts
*/
public function map(array $parts) {
if (count($parts) < 2) {
if ($parts[0] instanceof AbstractPart) {
return $parts;
Expand All @@ -31,6 +37,8 @@ public function map($parts) {
}
}

$pos = null;

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

Expand All @@ -39,18 +47,21 @@ public function map($parts) {
}

if ($part instanceof Initial) {
if (!isset($parts[$k-1]) || $parts[$k-1] instanceof Firstname || $parts[$k-1] instanceof Initial) {
continue;
if (null === $pos) {
$pos = $k;
}

$parts[$k] = new Firstname($part);
}

if ($part instanceof AbstractPart) {
break;
continue;
}

$parts[$k] = new Firstname($part);
$pos = $k;
break;
}

if (null !== $pos) {
$parts[$pos] = new Firstname($parts[$pos]);
}

return $parts;
Expand Down
8 changes: 7 additions & 1 deletion src/Mapper/InitialMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
use TheIconic\NameParser\Part\Initial;

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

/**
* map intials in parts array
*
* @param array $parts the name parts
* @return array the mapped parts
*/
function map(array $parts) {
foreach ($parts as $k => $part) {
if ($part instanceof AbstractPart) {
Expand Down
47 changes: 15 additions & 32 deletions src/Mapper/LastnameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,24 @@
use TheIconic\NameParser\Part\Lastname;
use TheIconic\NameParser\Part\Suffix;

class LastnameMapper
class LastnameMapper extends AbstractMapper
{

protected $prefixes = [
'vere' => 'vere',
'von' => 'von',
'van' => 'van',
'de' => 'de',
'der' => 'der',
'del' => 'del',
'della' => 'della',
'di' => 'di',
'da' => 'da',
'pietro' => 'pietro',
'vanden' => 'vanden',
'du' => 'du',
'st' => 'st.',
'la' => 'la',
'ter' => 'ter'
/**
* @var array options
*/
protected $options = [
'match_single' => false,
];

/**
* map lastnames in the parts array
*
* @param array $parts the name parts
* @return array the mapped parts
*/
public function map(array $parts) {
if (count($parts) < 2) {
if (!$this->options['match_single'] && count($parts) < 2) {
return $parts;
}

Expand All @@ -47,9 +42,9 @@ public function map(array $parts) {
break;
}

if (false !== $prefix = $this->isPrefix($part)) {
if (Lastname::isPrefix($part)) {
if (isset($parts[$k-1]) && $parts[$k-1] instanceof Lastname) {
$parts[$k] = new Lastname($prefix);
$parts[$k] = new Lastname($part);
}
} else if (!isset($parts[$k-1]) || !($parts[$k-1] instanceof Lastname)) {
$parts[$k] = new Lastname($part);
Expand All @@ -61,16 +56,4 @@ public function map(array $parts) {
return array_reverse($parts);
}

protected function isPrefix($part)
{
$part = str_replace('.', '', $part);
$part = strtolower($part);

if (array_key_exists($part, $this->prefixes)) {
return $this->prefixes[$part];
}

return false;
}

}
50 changes: 50 additions & 0 deletions src/Mapper/MiddlenameMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Firstname;
use TheIconic\NameParser\Part\Lastname;
use TheIconic\NameParser\Part\Middlename;

class MiddlenameMapper extends AbstractMapper
{

/**
* map middlenames in the parts array
*
* @param array $parts the name parts
* @return array the mapped parts
*/
public function map(array $parts) {
if (count($parts) < 3) {
return $parts;
}

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

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

if ($part instanceof Lastname) {
break;
}

if ($part instanceof AbstractPart) {
continue;
}

$parts[$k] = new Middlename($part);
}

return $parts;
}

}
32 changes: 32 additions & 0 deletions src/Mapper/NicknameMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace TheIconic\NameParser\Mapper;

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) {
foreach ($parts as $k => $part) {
if ($part instanceof AbstractPart) {
continue;
}

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

return $parts;
}

}
27 changes: 9 additions & 18 deletions src/Mapper/SalutationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,23 @@
use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Salutation;

class SalutationMapper
class SalutationMapper extends AbstractMapper
{

protected $salutations = [
'mr' => 'Mr.',
'master' => 'Mr.',
'mister' => 'Mr.',
'mrs' => 'Mrs.',
'miss' => 'Ms.',
'ms' => 'Ms.',
'dr' => 'Dr.',
'rev' => 'Rev.',
'fr' => 'Fr.',
];

/**
* map salutations in the parts array
*
* @param array $parts the name parts
* @return array the mapped parts
*/
function map(array $parts) {
foreach ($parts as $k => $part) {
if ($part instanceof AbstractPart) {
break;
}

$part = str_replace('.', '', $part);
$part = strtolower($part);

if (array_key_exists($part, $this->salutations)) {
$parts[$k] = new Salutation($this->salutations[$part]);
if (Salutation::isSalutation($part)) {
$parts[$k] = new Salutation($part);
}
}

Expand Down
46 changes: 17 additions & 29 deletions src/Mapper/SuffixMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,33 @@
use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Suffix;

class SuffixMapper
class SuffixMapper extends AbstractMapper
{

protected $suffixes = [
'i' => 'I',
'ii' => 'II',
'iii' => 'III',
'iv' => 'IV',
'v' => 'V',
'seniour' => 'Senior',
'junior' => 'Junior',
'jr' => 'Jr',
'sr' => 'Sr',
'phd' => 'PhD',
'apr' => 'APR',
'rph' => 'RPh',
'pe' => 'PE',
'md' => 'MD',
'ma' => 'MA',
'dmd' => 'DMD',
'cme' => 'CME',
];

/**
* map suffixes in the parts array
*
* @param array $parts the name parts
* @return array the mapped parts
*/
function map(array $parts) {
$parts = array_reverse($parts);
foreach ($parts as $k => $part) {
$start = count($parts) - 1;

for ($k = $start; $k > 1; $k--) {
$part = $parts[$k];

if ($part instanceof AbstractPart) {
break;
}

$part = str_replace('.', '', $part);
$part = strtolower($part);

if (array_key_exists($part, $this->suffixes)) {
$parts[$k] = new Suffix($this->suffixes[$part]);
if (Suffix::isSuffix($part)) {
$parts[$k] = new Suffix($part);
} else {
break;
}
}

return array_reverse($parts);
return $parts;
}

}
Loading

0 comments on commit fafda79

Please sign in to comment.