<?php
namespace App\Utils\BaseParser\Utils; class PassengerNameParser { use \App\Utils\BaseParser\OptionTrait; protected $text = null; protected $suffixes = [ 'MR' => ['mr', 'm', ], 'MRS' => ['mrs', 'f', ], 'MS' => ['ms', 'f', ], ]; public function __construct() { $this->options['allowInlineSuffix'] = true; $this->options['extractSuffix'] = true; $this->options['handleInlineSlashAndSpaceDelimiters'] = true; } public function setText($text) { $this->text = $text; return $this; } public function resetSuffixes() { $this->suffixes = []; return $this; } public function addSuffix($text, $title, $type = null) { $this->suffixes[$text] = [$title, $type]; return $this; } protected function doExtract(\App\Utils\BaseParser\Utils\Types\Passenger $ret) { $ret->isOwner = null; $extractor = new \App\Utils\BaseParser\Utils\Extractor\Single(); $xtrList = []; if ($this->options['handleInlineSlashAndSpaceDelimiters'] && $extractor->contains("/[\/]/ui", $this->text) && $extractor->contains("/[\s]/ui", $this->text)) { $xtrList[]= $extractor->find("/^" ."(?P<lastName>[^\/]+)" ."([\s\/]+(?P<nm1>[^\s]+))?" ."([\s\/]+(?P<nm2>[^\s]+))?" ."([\s\/]+(?P<nm3>[^\s]+))?" ."([\s\/]+(?P<nm4>[^\s]+))?" ."([\s\/]+(?P<nm5>[^\s]+))?" ."([\s\/]+(?P<nm6>[^\s]+))?" ."([\s\/]+(?P<nm7>[^\s]+))?" ."([\s\/]+(?P<nm8>[^\s]+))?" ."([\s\/]+(?P<nm9>[^\s]+))?" ."$/ui", $this->text); } if (!$xtrList) { $xtrList[]= $extractor->find("/^" ."(?P<lastName>[^\/]+)" ."(\/(?P<nm1>[^\/]+))?" ."(\/(?P<nm2>[^\/]+))?" ."(\/(?P<nm3>[^\/]+))?" ."(\/(?P<nm4>[^\/]+))?" ."(\/(?P<nm5>[^\/]+))?" ."(\/(?P<nm6>[^\/]+))?" ."(\/(?P<nm7>[^\/]+))?" ."(\/(?P<nm8>[^\/]+))?" ."(\/(?P<nm9>[^\/]+))?" ."$/ui", $this->text); $xtrList[]= $extractor->find("/^" ."[\s]*(?P<lastName>[^\s]+)" ."(\s+(?P<nm1>[^\s]+))?" ."(\s+(?P<nm2>[^\s]+))?" ."(\s+(?P<nm3>[^\s]+))?" ."(\s+(?P<nm4>[^\s]+))?" ."(\s+(?P<nm5>[^\s]+))?" ."(\s+(?P<nm6>[^\s]+))?" ."(\s+(?P<nm7>[^\s]+))?" ."(\s+(?P<nm8>[^\s]+))?" ."(\s+(?P<nm9>[^\s]+))?" ."[\s]*$/ui", $this->text); $xtrList[]= $extractor->find("/^" ."(?P<lastName>[^\s\/]+)" ."([\s\/](?P<nm1>[^\s\/]+))?" ."([\s\/](?P<nm2>[^\s\/]+))?" ."([\s\/](?P<nm3>[^\s\/]+))?" ."([\s\/](?P<nm4>[^\s\/]+))?" ."([\s\/](?P<nm5>[^\s\/]+))?" ."([\s\/](?P<nm6>[^\s\/]+))?" ."([\s\/](?P<nm7>[^\s\/]+))?" ."([\s\/](?P<nm8>[^\s\/]+))?" ."([\s\/](?P<nm9>[^\s\/]+))?" ."$/ui", $this->text); } usort($xtrList, function($a, $b) { return $b->getRealCount() - $a->getRealCount(); }); $xtr = reset($xtrList); if ($xtr->hasValue()) { $names = array_filter(array_map(function($nm){ return trim((string)$nm); }, [$xtr->nm1, $xtr->nm2, $xtr->nm3, $xtr->nm4, $xtr->nm5, $xtr->nm6, $xtr->nm7, $xtr->nm8, $xtr->nm9, ])); $lname = end($names); if ($this->options['extractSuffix'] && in_array($lname, array_keys($this->suffixes))) { $ret->title = $this->suffixes[$lname][0]; $ret->type = $this->suffixes[$lname][1]; $names = array_slice($names, 0, -1); } elseif ($this->options['extractSuffix'] && $this->options['allowInlineSuffix'] && $sfx=$extractor->findAndStore("/.+(?P<suffix>".$extractor->getRegexpToMatchAnyOf(array_keys($this->suffixes)).")$/ui", $lname)) { $ret->title = $this->suffixes[$sfx->suffix][0]; $ret->type = $this->suffixes[$sfx->suffix][1]; $lname = substr($lname, 0, -1*strlen($sfx->suffix)); $names = array_merge(array_slice($names, 0, -1), [$lname]); } $ret->lastName = $xtr->lastName; $ret->firstName = implode(" ", $names); } else { $ret->lastName = trim($this->text); } return $ret; } protected function cleanupResult(\App\Utils\BaseParser\Utils\Types\Passenger $ret) { $ret->firstName = trim((string)$ret->firstName, "\t .,/"); $ret->lastName = trim((string)$ret->lastName, "\t .,/"); $ret->firstName = preg_replace("/[\s]+/", ' ', $ret->firstName); $ret->lastName = preg_replace("/[\s]+/", ' ', $ret->lastName); return $ret; } public function extract() { $ret = new \App\Utils\BaseParser\Utils\Types\Passenger(); return $this->cleanupResult($this->doExtract($ret)); } } 