This repository has been archived on 2024-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
RelativeDateParser/src/enko/RelativeDateParser/RelativeDateParser.php
2014-09-30 07:05:21 +02:00

58 lines
1.5 KiB
PHP
Executable file

<?php
namespace enko\RelativeDateParser;
class RelativeDateParser {
/** @var RelativeDateType */
private $datetype = null;
private $now = null;
function __construct ($string,\DateTime $now = null, $lang = 'en') {
// initialize the translator
DateTranslator::getInstance($lang);
// set the now date
if (is_null($now)) {
$this->now = new \DateTime();
} else {
$this->now = $now;
}
// then try to determine which datetype we have here
$success = preg_match(RelativeDateType1::getRegex(),$string,$match);
if ($success === 1) {
$this->datetype = new RelativeDateType1($match);
return $this;
}
$success = preg_match(RelativeDateType2::getRegex(),$string,$match);
if ($success === 1) {
$this->datetype = new RelativeDateType2($match);
return $this;
}
$success = preg_match(RelativeDateType3::getRegex(),$string,$match);
if ($success === 1) {
$this->datetype = new RelativeDateType3($match);
return $this;
}
throw new \Exception('No DateType found.');
}
public function getCurrent() {
return $this->datetype->getCurrent($this->now);
}
public function getNext() {
return $this->datetype->getNext($this->now);
}
public function setNow(\DateTime $now) {
$this->now = $now;
}
}