Skip to content

Commit

Permalink
initial prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrfel committed May 8, 2015
1 parent ec7b9bd commit 993432d
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
10 changes: 10 additions & 0 deletions src/Mapper/AbstractMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace TheIconic\NameParser\Mapper;

abstract class AbstractMapper
{

abstract public function map(array $parts);

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

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Firstname;
use TheIconic\NameParser\Part\Lastname;
use TheIconic\NameParser\Part\Initial;
use TheIconic\NameParser\Part\Salutation;

class FirstnameMapper
{

public function map($parts) {
if (count($parts) < 2) {
if ($parts[0] instanceof AbstractPart) {
return $parts;
}

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

return $parts;
}

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

if ($part instanceof Lastname) {
break;
}

if ($part instanceof Initial) {
if ($parts[$k-1] instanceof Firstname || $parts[$k-1] instanceof Initial) {
continue;
}

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

return $parts;
}

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

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\Initial;

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

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

if ((strlen($part) == 1) || (strlen($part) == 2 && substr($part, -1) === '.')) {
$parts[$k] = new Initial($part);
}
}

return parts;
}

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

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Lastname;
use TheIconic\NameParser\Part\Suffix;

class FirstnameMapper
{

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'
];

public function map(array $parts) {
if (count($parts) < 2) {
return $parts;
}

if (count($parts) === 2 && $parts[0] instanceof AbstractPart) {
$parts[1] = new Lastname($parts[1]);
}

$parts = array_reverse($parts);

$lastNames = [];

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

if ($part instanceof AbstractPart) {
break;
}

if (false !== $prefix = $this->isPrefix($part)) {
$lastNames[] = $prefix;
}

$parts[$k] = new Lastname(array_reverse($lastNames));
}

return array_reverse($parts);
}

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

return false;
}

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

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Salutation;

class SalutationMapper
{

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

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]);
}
}

return $parts;
}

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

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Suffix;

class SuffixMapper
{

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',
];

function map(array $parts) {
$parts = array_reverse($parts);
foreach ($parts as $k => $part) {
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]);
}
}

return array_reverse($parts);
}

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

namespace TheIconic\NameParser;

class Name
{

protected $parts;

public function __construct($parts = null)
{
if (null !== $parts) {
$this->parts = $parts;
}
}

public function getFirstname()
{
return $this->export('Firstname');
}

public function getLastname()
{
return $this->export('Lastname');
}

public function getInitials()
{
return $this->export('Initial');
}

public function getSuffix()
{
return $this->export('Suffix');
}

public function getSalutation()
{
return $this->export('Salutation');
}

protected function export($type)
{
$matched = [];

foreach ($this->parts as $part) {
if ($part instanceof AbstractPart && is_subclass_of($part, $type)) {
$matched[] = $part->getValue();
}
}

return implode(' ', $matched);
}

}
Loading

0 comments on commit 993432d

Please sign in to comment.