forked from theiconic/name-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
466 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.