<?php
namespace App\Amadeus\FileParser;

class Line_MFT extends \App\Amadeus\FileParser\LineAbstract
{
	/**
	 * 4.145. MFT MCO/EMD Tour Code
	 * see page 359(401)
	 *
	 * DETAILS
	 */
	protected $structure = [
		['name'=> 'MSGTAG',     'val' => 'MFT',   'type' => 'AN',      'req' => 'M',                        'len' => 3,],  // 1 : Message Tag (MFT)
		['name'=> 'TOUR',                         'type' => 'AN',      'req' => 'M',                        'len_min' => 1,   'len_max' => 127,],   // 2 : Tour Code
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'len' => 1,],
		['name'=> 'LINEASS',    'val' => 'L',     'type' => 'ASSOC:loose',],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'len' => 1,],
		['name'=> 'PSGASS',     'val' => 'P',     'type' => 'ASSOC:loose',],
	];

	protected $structure_post01 = [
		['name'=> 'TSMID',      'val' => ';D',    'type' => 'AN',      'len' => 2,],                                      // 5 : TSM association identifier (constant value of 'D')
		['name'=> 'TSMNBR',                       'type' => 'N',       'req' => 'M',  'requires' => ['TSMID',],],   // 6 : TSM number
	];

	protected $structure_post02 = [
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'len' => 1,],
		['name'=> 'TSMID',      'val' => 'D',     'type' => 'AN',      'len' => 1,],
		['name'=> 'TSMNBR',                       'type' => 'N',       'req' => 'M',  'requires' => ['TSMID',],],   // 6 : TSM number
	];


	public function parse()
	{
		if(preg_match("/;;;/", $this->content)) {
			$this->tree = $this->parseFromStructure(array_merge($this->structure, $this->structure_post01), $this->content);
		}
		else {
			$this->tree = $this->parseFromStructure(array_merge($this->structure, $this->structure_post02), $this->content);
		}

		$this->postParse();
		return $this;
	}
}
