<?php
namespace App\Galileo\LineParser\Types;

/**
 * Data line
 * @author Lucian Sirbu
 */
class Data_A12 extends \App\Galileo\LineParser\Types\GalileoBase
{
	/**
	 *    PHONE DATA
	 */

	/**
	 * Children elements
	 */
	protected $children = [
		[ 'key'=>'A12SEC',  'type'=>'Constant',  'len'=>3,          'val'=>'A12',          'label'=>'SECTION LABEL "A12"' ],
		[ 'key'=>'A12CTY',  'type'=>'StringRaw', 'len'=>3,                                 'label'=>'CITY CODE' ],
		[ 'key'=>'A12LOC',  'type'=>'StringTrim','len'=>[1,2],                             'label'=>'LOCATION TYPE' ],
		[ 'key'=>'A12PHN',  'type'=>'StringRaw',                    'req'=>false,          'label'=>'FREEFORM PHONE DATA' ],
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\Utils\BaseParser\TextParser\BlockAbstract::postExtract()
	 */
	protected function postExtract(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret)
	{
		if ($ret->extractedData['A12LOC']=='E') {	// we have an email in A12PHN
			if ($xtr = $this->getExtractor()->findAndStore('/\*(?P<email>.+)$/', $ret->extractedData['A12PHN'])) {
				$ret->extractedData['A12PHN_'] = [
					'email' => str_replace('//', '@', $xtr->email),
				];
			}
		}
		elseif ($ret->extractedData['A12LOC']=='N') {	// we have a telephone in A12PHN
		    if ($xtr = $this->getExtractor()->findAndStore('/\*(?P<telephone>.+)$/', $ret->extractedData['A12PHN'])) {
		        $ret->extractedData['A12PHN_'] = [
		            'telephone' => $xtr->telephone,
		        ];
		    }
		}
		return $ret;
	}
}
