View source
<?php
class HumanNameParser_Parser {
private $name;
private $nameParts = array();
private $leadingInit;
private $first;
private $nicknames;
private $middle;
private $last;
private $suffix;
private $category;
private $type;
private $literal;
private $suffixes;
private $prefixes;
public function __construct($name = NULL) {
$this->suffixes = array(
'esq',
'esquire',
'jr',
'sr',
'2',
'ii',
'iii',
'iv',
);
$this->prefixes = array(
'bar',
'ben',
'bin',
'da',
'dal',
'de la',
'de la Rue du',
'de',
'del',
'der',
'di',
'ibn',
'la',
'le',
'san',
'st',
'ste',
'van',
'van der',
'van den',
'vel',
'von',
);
$this
->setName($name);
}
public function parseName($name = NULL, $category = NULL) {
$this->literal = 0;
$this->category = 1;
$this->type = 1;
if (is_array($name) && isset($name['name'])) {
if (isset($name['auth_category']) && !empty($name['auth_category']) && empty($category)) {
$this->category = $name['auth_category'];
}
elseif (!empty($category)) {
$this->category = $category;
}
if (isset($name['auth_type']) && !empty($name['auth_type'])) {
$this->type = $name['auth_type'];
}
$this->nameParts = $name;
$this
->setName($name['name'], $category);
}
else {
$this->nameParts['name'] = $name;
$this
->setName($name, $category);
}
return $this
->getArray();
}
public function setName($name = NULL, $category = NULL) {
if ($name) {
$this->category == $category;
if (is_object($name) && get_class($name) == "HumanNameParser_Name") {
$this->name = $name;
}
elseif (is_array($name) && isset($name['name'])) {
$this->name = new HumanNameParser_Name($name['name']);
$this->nameParts = $name;
}
else {
$this->name = new HumanNameParser_Name($name);
}
$this->leadingInit = "";
$this->first = "";
$this->nicknames = "";
$this->middle = "";
$this->last = "";
$this->suffix = "";
if ($this->category == 5 || $this->type == 5) {
$this->last = $name;
$this->literal = TRUE;
}
else {
$this
->parse();
}
}
}
public function getleadingInit() {
return $this->leadingInit;
}
public function getFirst() {
return $this->first;
}
public function getNicknames() {
return $this->nicknames;
}
public function getMiddle() {
return $this->middle;
}
public function getLast() {
return $this->last;
}
public function getSuffix() {
return $this->suffix;
}
public function getName() {
return $this->name;
}
public function getArray($arrType = 'assoc') {
$arr = array();
$arr['prefix'] = $this->leadingInit;
$arr['firstname'] = $this->first;
$arr['nicknames'] = $this->nicknames;
$arr['initials'] = substr($this->middle, 0, 10);
$arr['lastname'] = $this->last;
$arr['suffix'] = $this->suffix;
$arr['md5'] = md5(json_encode($arr));
$arr['literal'] = $this->literal;
if ($arrType == 'assoc') {
return array_merge($this->nameParts, $arr);
}
elseif ($arrType == 'int') {
return array_values($arr);
}
else {
throw new Exception("Array must be associative ('assoc') or numeric ('num').");
}
}
private function parse() {
$suffixes = implode("\\.*|\\s", $this->suffixes) . "\\.*";
$prefixes = implode(" |", $this->prefixes) . " ";
$nicknamesRegex = "/ ('|\"|\\(\"*'*)(.+?)('|\"|\"*'*\\)) /";
$suffixRegex = "/,* *({$suffixes})\$/";
$lastRegex = "/(?!^)\\b([^ ]+ y |{$prefixes})*[^ ]+\$/u";
$leadingInitRegex = "/^(.\\.*)(?= \\p{L}{2})/";
$firstRegex = "/^[^ ]+/";
if (preg_match('@^\\s*(\\p{L}+)\\s*$@u', $this->name
->getStr(), $matches)) {
$this->last = $matches[1];
return TRUE;
}
$this->nicknames = $this->name
->chopWithRegex($nicknamesRegex, 2);
$this->suffix = $this->name
->chopWithRegex($suffixRegex, 1);
$this->name
->flip(",");
$this->last = $this->name
->chopWithRegex($lastRegex, 0);
if (!$this->last) {
throw new Exception("Couldn't find a last name in '{$this->name->getStr()}'.");
}
$this->leadingInit = $this->name
->chopWithRegex($leadingInitRegex, 1);
$this->first = $this->name
->chopWithRegex($firstRegex, 0);
if (!$this->first && $this->category != 5) {
throw new Exception("Couldn't find a first name in '{$this->name->getStr()}'");
}
$this->middle = $this->name
->getStr();
return TRUE;
}
}