<?php
namespace App\Bsp\Hot230\Lines\Types;

/**
 *
 * @author Lucian Sirbu
 *
 */
class DateAuto extends \App\Utils\BaseParser\TextParser\Types\Date
{
	public function __construct($options, $parent = null)
	{
		if (!$options['fmt'] && !$options['fmts']) {
			$options['fmts'] = ['DDMMM', 'DDMMMYY', 'YYMMDD', ];
			$options['allowSpaces'] = true;
		}

		parent::__construct($options, $parent);

		$this->extractRegexp = "/^(?P<rawData>({$this->subregexp})|([\s]{{$this->options['len']}}))/"; 	// allow spaces
	}

	public function getSubregexpForSpecificFmt($fmt, $options)
	{
		$subregexp = parent::getSubregexpForSpecificFmt($fmt, $options);
		if ($options['len'] && ($options['len'] - strlen($fmt))>0) {
			$subregexp.= sprintf("[\s]{%d}", $options['len'] - strlen($fmt));
		}

		return $subregexp;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Utils\BaseParser\TextParser\Types\Date::getDate()
	 */
	public function getDate($matches, $options, $renamedPatternNames) {
		if (trim($matches['rawData'])==='' || trim($matches['rawData'], '0')==='') {
			return null;
		}

		return parent::getDate($matches, $options, $renamedPatternNames);
	}
}
