<?php
namespace App\Sabre\LineParser\Types\Line;

/**
 *
 * @author Lucian Sirbu
 */
class MB extends \App\Sabre\LineParser\Types\SabreBaseLine
{
	protected $extractRegexp = "/^(?P<rawData>MB.*)/s";

	/**
	 * MB – Miscellaneous Charge Order Record (MCO)
	 * The information is present only if the location is using ATB ticket stock and has used the “MISC”
	 * ticketing entries to create all the associated records.
	 */

	/**
	 * Children elements
	 */
	protected $children = [
		 [  'key'=>'IUBMID',  'type'=>'Constant',     'len'=>2,   'val'=>'MB',  'label'=>'IUBMID' ], // MESSAGE ID - "MB"
		 [  'key'=>'IUBPTY',  'type'=>'StringTrim',   'len'=>2,                 'label'=>'IUBPTY' ], // ASSOCIATED M5 ITEM NUMBER
		 [  'key'=>'IUBM1N',  'type'=>'StringTrim',   'len'=>2,                 'label'=>'IUBM1N' ], // ASSOCIATED PASSENGERS
		 [  'key'=>'IUBINF',  'type'=>'StringMultilineEndingWithNewLine',       'label'=>'IUBINF' ], // VARIABLE MCO DATA
	];

	protected function postExtract(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret)
	{
		$ret->extractedData['IUBINF_'] = [];
		if ($ret->extractedData['IUBINF']) {
			foreach (explode("\n", $ret->extractedData['IUBINF']) as $IUBINF) {
				$values = $this->getExtractor()->find("/^(?P<tag>[A-Z0-9]+)\/(?P<value>.*)$/", $IUBINF)->cleanup()->getMatches();
				$values['value'] = trim(implode("\n", array_map(function($s){
					return trim($s);
				}, explode("@", $values['value']))));

				if ($values['value']!=='') {
					$ret->extractedData['IUBINF_'][]= $values;
				}
			}
		}

		return $ret;
	}
}
