<?php
namespace App\Amadeus\FileParser;

class Line_RX extends \App\Amadeus\FileParser\LineAbstract
{
	/**
	 * 4.157. RX-PNR Corporate Remarks
	 * see page 380(422)
	 *
	 * In this pseudo EDIFACT segment no separators are used.
	 * The 'segment' will be terminated by a Carriage Return.
	 * For those offices with AIR version 204 or above, the inclusion of these lines will be governed by customization key.
	 * Note that the presence of this remark is submitted to the Concealment of Confidential Element in Back Office A.I.R.  as specified in A02AIR3530.
	 */
	protected $structure = [
		['name'=> 'MSGTAG',  'val' => 'RX',    'type' => 'AN',      'req' => 'M',  'len' => 2,],  // 1 : Message Tag (RX)
		['name'=> 'COPRMK',                    'type' => 'AN',      'req' => 'M',],               // 2 : RX Remark text
		['name'=> 'SEGASS',  'val' => ';S',    'type' => 'ASSOC',],
		['name'=> 'PSGASS',  'val' => ';P',    'type' => 'ASSOC',],
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\Amadeus\FileParser\LineAbstract::postParse()
	 */
	protected function postParse()
	{
		parent::postParse();

		/**
		 * Fixes DAMA:4334(Problems with the item 3998 - process the Service Fee value also from confidential remarks)
		 * It says that the passenger assoc may be made by using /P1 instead of ;P1 as in any other segment
		*/
		$delim = "/P";
		$offset = strrpos($this->tree['COPRMK'], $delim);
		if(!$this->tree['PSGASS'] && $offset!==false){
			$res = $this->parseFromStructure_v2_generalAssoc( ['name'=>'PSGASS', 'val'=>preg_quote($delim, "/"), 'type'=>'ASSOC',], $this->tree['COPRMK'], $offset, [] );

			if($res['value']){
				$this->tree['COPRMK'] = substr($this->tree['COPRMK'], 0, $offset);
				$this->tree['PSGASS'] = $res['value'];
			}
		}
	}
}
