<?php
namespace App\Utils\BaseParser\TextParser\Types; class Time extends \App\Utils\BaseParser\TextParser\BlockRegexpAbstract { protected $matchedData = []; protected $subregexp = null; public function __construct($options, $parent = null){ $this->subregexp = $this->getSubregexp($options); $this->extractRegexp = "/^(?P<rawData>{$this->subregexp})/sim"; parent::__construct($options, $parent); } public function getSubregexp($options) { switch ($options['fmt']) { case 'HHMMP': $subregexp = "(?P<HH>[0-9]{2})(?P<MM>[0-9]{2})(?P<P> |A|P)"; break; case 'HHMMP+': $subregexp = "(?P<HH>[0-9]{2})(?P<MM>[0-9]{2})(?P<P> |A|P|N|M)"; break; case 'PHHMM': $subregexp = "(?P<P> |A|P)(?P<HH>[0-9]{2})(?P<MM>[0-9]{2})"; break; case 'HHMM': $subregexp = "(?P<HH>[0-9]{2})(?P<MM>[0-9]{2})"; break; default: throw new \Exception("Unknown time format '{$options['fmt']}'"); } return $subregexp; } public function getTime($matches, $fmt) { $return = null; switch($fmt){ case 'HHMMP': case 'HHMMP+': case 'PHHMM': if ($matches['P']=='N' && $matches['HH']=='12' && $matches['MM']=='00') { $matches['HH'] = '12'; $matches['MM'] = '00'; } elseif ($matches['P']=='M' && $matches['HH']=='12' && $matches['MM']=='00') { $matches['HH'] = '00'; $matches['MM'] = '00'; } elseif($matches['P']=='P' && $matches['HH']!='12') { $matches['HH']+=12; } elseif($matches['P']=='A' && $matches['HH']=='12') { $matches['HH']= '00'; } $return = sprintf("%02d:%02d:00", $matches['HH'], $matches['MM']); break; case 'HHMM': $return = sprintf("%02d:%02d:00", $matches['HH'], $matches['MM']); break; default: throw new \Exception("Unknown time format '{$fmt}'"); } if($return===null){ return null; } return $return; } protected function _extract(\App\Utils\BaseParser\TextParser\ExtractedBlock $ret, $originalRawData) { $m = $this->safeMatch($this->extractRegexp, (string)$originalRawData); $this->rawData = $m->matches['rawData']; $this->matchedData = $m->matches; $ret->remainingGlobalData = substr((string)$originalRawData, strlen((string)$this->rawData)); $this->extractedData = $this->getTime($m->matches, $this->options['fmt']); $ret->extractedData = $this->extractedData; \Engine\Debug\ParserCallTrace\ParserCallTrace::getInstance()->logInput([ 'regexp' => $this->extractRegexp, ], 'regexp'); return $ret; } } 