<?php
namespace App\Sabre\LineParser;

class PassengerNameParser extends \App\Utils\BaseParser\Utils\PassengerNameParser
{
	public function extract()
	{
		$paxTypes = [
			'MR' => 	['type'=>'adult', 		'title'=>'Mr', 	'sex'=>'m',],
			'MRS' => 	['type'=>'adult', 		'title'=>'Ms', 	'sex'=>'f',],
			'MS' => 	['type'=>'adult', 		'title'=>'Ms', 	'sex'=>'f',],
			'CHD' => 	['type'=>'child', 		'title'=>'', 	'sex'=>'',],
			'INF' => 	['type'=>'infant', 	'title'=>'', 	'sex'=>'',],
		];

		$ret = new \App\Utils\BaseParser\Utils\Types\Passenger();
		$ret->isOwner = null;	// this defaults to 'true'. reset it

		$extractor = new \App\Utils\BaseParser\Utils\Extractor\Single();
		$m = $extractor->find("/^(?P<lastname>[A-Z\s]+)\/(?P<firstname>[A-Z\s]+?)( (?P<title>(MR|MRS|MS|CHD|INF)))?$/", $this->text);

		if (!$m->hasValue()) {
			$m = $extractor->find("/^(?P<lastname>[A-Z\s]+?) (?P<firstname>[A-Z\s]+?)( (?P<title>(MR|MRS|MS|CHD|INF)))?$/", $this->text);
		}

		// fill data
		$ret->lastName = 	$m->lastname;
		$ret->firstName = 	$m->firstname;
		$ret->title = 		$paxTypes[$m->title]['title'];
		$ret->type = 		$paxTypes[$m->title]['type'];

		if ($paxTypes[$ret->firstName]) {
			$nm = $ret->firstName;
			$ret->firstName = null;
			$ret->title = 		$paxTypes[$nm]['title'];
			$ret->type = 		$paxTypes[$nm]['type'];
		}

		return $ret;
	}
}
