<?php
namespace App\Amadeus\FileParser;

class Line_MFO extends \App\Amadeus\FileParser\LineAbstract
{
	/**
	 * 4.141. MFO MCO/EMD Original Issue/In Exchange For
	 * see page 350(392)
	 *
	 * DETAILS
	 */
	protected $structure = [
		['name'=> 'MSGTAG',     'val' => 'MFO',   'type' => 'AN',      'req' => 'M',  'len' => 3,],                          // 1 : Message Tag (MFO)
		['name'=> 'ORIGEXCH',                     'type' => 'AN',      'req' => 'M',  'len_min' => 1,   'len_max' => 127,],  // 2 : Original Issue/In Exchange For
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',                     'len' => 1,],
		['name'=> 'LINEASS',    'val' => 'L',     'type' => 'ASSOC:loose',],
		['name'=> 'PSGASS',     'val' => 'P',     'type' => 'ASSOC:loose',],
		['name'=> 'TSMASS',     'val' => 'D',     'type' => 'ASSOC:loose',],                                                      // 5 : TSM association identifier (constant value of 'D')
	];

	/**
	 * This exists in order to be able to parse
	 * 		2012-11-02 EMD/11/11.2.Reemitere EMD-A asociat tkt 11 reemis.APR, an example from RO/AMA
	 * @var array
	 */
	protected $structure_AMA = [
		['name'=> 'MSGTAG',     'val' => 'MFO',   'type' => 'AN',      'req' => 'M',  'len' => 3,],                          // 1 : Message Tag (MFO)
		['name'=> 'ORIGEXCH',                     'type' => 'AN',      'req' => 'M',  'len_min' => 1,   'len_max' => 127,],  // 2 : Original Issue/In Exchange For

		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',  'len' => 1,],
		['name'=> 'LINEASS',    'val' => 'L',     'type' => 'ASSOC:loose',],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',  'len' => 1,],
		['name'=> 'PSGASS',     'val' => 'P',     'type' => 'ASSOC:loose',],
		['name'=> 'DELIM',      'val' => ';',     'type' => 'AN',      'req' => 'M',  'len' => 1,],
		['name'=> 'TSMASS',     'val' => 'D',     'type' => 'ASSOC:loose',],
	];


	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\FileParser\LineAbstract::parse()
	 */
	public function parse()
	{
		$parsed = false;
		if (!$parsed) {
			try{
				$this->tree = $this->parseFromStructure($this->structure, $this->content);
				$this->postParse();
				$parsed = true;
			}catch(\App\Amadeus\FileParser\ParserException $ex){
				$parsed = false;
			}
		}

		if (!$parsed) {
			try{
				$this->tree = $this->parseFromStructure($this->structure_AMA, $this->content);
				$this->postParse();
				$parsed = true;
			}catch(\App\Amadeus\FileParser\ParserException $ex){
				$parsed = false;
			}
		}

		if (!$parsed) {
			throw new \App\Amadeus\FileParser\ParserException("Unknown line structure for the MFO line");
		}

		return $this;
	}
}
