Implemented the requested feature from calcifer:#29

This commit is contained in:
Tim Schumacher 2014-09-30 07:05:21 +02:00
parent 0f1e587c65
commit 260170995d
3 changed files with 58 additions and 2 deletions

View file

@ -45,7 +45,7 @@ class DateTranslator {
}
}
public function translate($string) {
return $this->translator->trans($string);
public function translate($string,$params = []) {
return $this->translator->trans($string,$params);
}
}

View file

@ -34,6 +34,12 @@ class RelativeDateParser {
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.');
}

View file

@ -0,0 +1,50 @@
<?php
/**
* Created by PhpStorm.
* User: tim
* Date: 30.09.2014
* Time: 21:47
*/
namespace enko\RelativeDateParser;
class RelativeDateType3 {
private $day = -1;
public static function getRegex() {
return DateTranslator::getInstance()->translate('/Jeder (\d+)\. Tag eines Monats/im');
}
private function getDay() {
return $this->day;
}
public function getCurrent(\DateTime $now) {
if ($this->day > cal_days_in_month(CAL_GREGORIAN,$now->format('m'),$now->format('Y'))) {
throw new \BadMethodCallException(DateTranslator::getInstance()->translate('Angegebener Tag ist nicht im Monat %date% enthalten.',['date' => $now->format('F Y')]));
}
$date = new \DateTime();
$date->setTimestamp (strtotime (sprintf ('%s %s %d', $this->getDay (), $now->format('F'), $now->format('Y'))));
$date->setTime($now->format('H'),$now->format('i'));
return $date;
}
public function getNext(\DateTime $now) {
$date = clone $now;
$date->add(\DateInterval::createFromDateString('+1 Month'));
return $this->getCurrent($date);
}
function __construct($data) {
$day = intval($data[1]);
if (is_integer($day)) {
$this->day = $day;
} else {
throw new \BadMethodCallException(DateTranslator::getInstance()->translate('Tag wurde nicht angegeben oder ist falsch.'));
}
}
}