From eae1170c8cd44faf134e9258f0fb29c547d99ddc Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sun, 21 Sep 2014 19:00:03 +0200 Subject: [PATCH] Changed Class structure to psr0 --- RelativeDateParser.php | 264 ------------------ composer.json | 3 + .../RelativeDateParser/DateTranslator.php | 49 ++++ .../RelativeDateParser/RelativeDateParser.php | 52 ++++ .../RelativeDateParser/RelativeDateType.php | 8 + .../RelativeDateParser/RelativeDateType1.php | 122 ++++++++ .../RelativeDateParser/RelativeDateType2.php | 47 ++++ 7 files changed, 281 insertions(+), 264 deletions(-) delete mode 100755 RelativeDateParser.php mode change 100644 => 100755 composer.json create mode 100755 src/enko/RelativeDateParser/DateTranslator.php create mode 100755 src/enko/RelativeDateParser/RelativeDateParser.php create mode 100755 src/enko/RelativeDateParser/RelativeDateType.php create mode 100755 src/enko/RelativeDateParser/RelativeDateType1.php create mode 100755 src/enko/RelativeDateParser/RelativeDateType2.php diff --git a/RelativeDateParser.php b/RelativeDateParser.php deleted file mode 100755 index 861f76d..0000000 --- a/RelativeDateParser.php +++ /dev/null @@ -1,264 +0,0 @@ -load(realpath($file),$lang); - - $this->translator = new Translator($lang); - $this->translator->addLoader('pofile', $loader); - $this->translator->addResource('pofile',realpath($file),$lang); - } - } - - if (is_null($this->translator)) { - throw new BadMethodCallException('No translation file for this language available.'); - } - } - - public function translate($string) { - return $this->translator->trans($string); - } -} - - -abstract class RelativeDateType { - abstract public function getNext(DateTime $now); - abstract public function getCurrent(DateTime $now); -} - -class RelativeDateType1 { - private $ordinal = -1; - private $day = -1; - private $month_or_year = -1; - - public static function getRegex() { - return DateTranslator::getInstance()->translate('/([Erster|Zweiter|Dritter|Letzter]+) ([Montag|Dienstag|Mittwoch|Donnerstag|Freitag|Samstag|Sonntag]+) des ([Monats|Jahres]+)/im'); - } - - private function getOrdinal() { - switch($this->ordinal) { - case 0: - return 'first'; - case 1: - return 'second'; - case 2: - return 'third'; - case 3: - return 'last'; - default: - return null; - } - } - - private function getDay() { - switch($this->day) { - case 0: - return 'mon'; - case 1; - return 'tue'; - case 2: - return 'wed'; - case 3: - return 'thu'; - case 4: - return 'fri'; - case 5: - return 'sat'; - case 6: - return 'sun'; - default: - return null; - } - } - - public function getCurrent(DateTime $now) { - $date = new DateTime(); - $date->setTimestamp (strtotime (sprintf ('%s %s %s %d', $this->getOrdinal (), $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) { - switch($data[1]) { - case DateTranslator::getInstance()->translate('Erster'): - $this->ordinal = 0; - break; - case DateTranslator::getInstance()->translate('Zweiter'): - $this->ordinal = 1; - break; - case DateTranslator::getInstance()->translate('Dritter'): - $this->ordinal = 2; - break; - case DateTranslator::getInstance()->translate('Letzter'): - $this->ordinal = 3; - break; - default: - throw new BadMethodCallException("Ordinal is wrong or missing"); - } - - switch ($data[2]) { - case DateTranslator::getInstance()->translate('Montag'): - $this->day = 0; - break; - case DateTranslator::getInstance()->translate('Dienstag'): - $this->day = 1; - break; - case DateTranslator::getInstance()->translate('Mittwoch'): - $this->day = 2; - break; - case DateTranslator::getInstance()->translate('Donnerstag'): - $this->day = 3; - break; - case DateTranslator::getInstance()->translate('Freitag'): - $this->day = 4; - break; - case DateTranslator::getInstance()->translate('Samstag'): - $this->day = 5; - break; - case DateTranslator::getInstance()->translate('Sonntag'): - $this->day = 6; - break; - default: - throw new BadMethodCallException("Day is wrong or missing"); - } - - switch($data[3]) { - case DateTranslator::getInstance()->translate('Monats'): - $this->month_or_year = 0; - break; - case DateTranslator::getInstance()->translate('Jahres'): - $this->month_or_year = 1; - break; - default: - throw new BadMethodCallException("month or year is wrong or missing"); - } - } -} - -class RelativeDateType2 { - private $interval = -1; - private $days_or_week = -1; - - public static function getRegex() { - return DateTranslator::getInstance()->translate('/Alle (\d+) ([Wochen|Tage]+)/im'); - } - - public function getCurrent(DateTime $now) { - return $now; - } - - public function getNext(DateTime $now) { - $date = clone $now; - if ($this->days_or_week == 0) { - $date->add(DateInterval::createFromDateString(sprintf('+%s days',$this->interval))); - } else { - $date->add(DateInterval::createFromDateString(sprintf('+%s weeks',$this->interval))); - } - return $date; - } - - function __construct($data) { - $interval = intval($data[1]); - if (is_integer($interval)) { - $this->interval = $data[1]; - } else { - throw new BadMethodCallException("interval is wrong or missing"); - } - - switch($data[2]) { - case DateTranslator::getInstance()->translate('Tage'): - $this->days_or_week = 0; - break; - case DateTranslator::getInstance()->translate('Wochen'): - $this->days_or_week = 1; - break; - default: - throw new BadMethodCallException("days or weeks are missing"); - } - } -} - -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; - } - - 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; - } -} \ No newline at end of file diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index cefc28f..da4d7cc --- a/composer.json +++ b/composer.json @@ -5,6 +5,9 @@ "symfony/Config" : "~2.5" }, "license": "MIT", + "autoload": { + "psr-0": { "": "src/" } + }, "authors": [ { "name": "Tim Schumacher", diff --git a/src/enko/RelativeDateParser/DateTranslator.php b/src/enko/RelativeDateParser/DateTranslator.php new file mode 100755 index 0000000..99c4f1e --- /dev/null +++ b/src/enko/RelativeDateParser/DateTranslator.php @@ -0,0 +1,49 @@ +load(realpath($file),$lang); + + $this->translator = new Translator($lang); + $this->translator->addLoader('pofile', $loader); + $this->translator->addResource('pofile',realpath($file),$lang); + } + } + + if (is_null($this->translator)) { + throw new \BadMethodCallException('No translation file for this language available.'); + } + } + + public function translate($string) { + return $this->translator->trans($string); + } +} \ No newline at end of file diff --git a/src/enko/RelativeDateParser/RelativeDateParser.php b/src/enko/RelativeDateParser/RelativeDateParser.php new file mode 100755 index 0000000..d62146a --- /dev/null +++ b/src/enko/RelativeDateParser/RelativeDateParser.php @@ -0,0 +1,52 @@ +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; + } + + 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; + } +} \ No newline at end of file diff --git a/src/enko/RelativeDateParser/RelativeDateType.php b/src/enko/RelativeDateParser/RelativeDateType.php new file mode 100755 index 0000000..1c1ea1c --- /dev/null +++ b/src/enko/RelativeDateParser/RelativeDateType.php @@ -0,0 +1,8 @@ +translate('/([Erster|Zweiter|Dritter|Letzter]+) ([Montag|Dienstag|Mittwoch|Donnerstag|Freitag|Samstag|Sonntag]+) des ([Monats|Jahres]+)/im'); + } + + private function getOrdinal() { + switch($this->ordinal) { + case 0: + return 'first'; + case 1: + return 'second'; + case 2: + return 'third'; + case 3: + return 'last'; + default: + return null; + } + } + + private function getDay() { + switch($this->day) { + case 0: + return 'mon'; + case 1; + return 'tue'; + case 2: + return 'wed'; + case 3: + return 'thu'; + case 4: + return 'fri'; + case 5: + return 'sat'; + case 6: + return 'sun'; + default: + return null; + } + } + + public function getCurrent(\DateTime $now) { + $date = new \DateTime(); + $date->setTimestamp (strtotime (sprintf ('%s %s %s %d', $this->getOrdinal (), $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) { + switch($data[1]) { + case DateTranslator::getInstance()->translate('Erster'): + $this->ordinal = 0; + break; + case DateTranslator::getInstance()->translate('Zweiter'): + $this->ordinal = 1; + break; + case DateTranslator::getInstance()->translate('Dritter'): + $this->ordinal = 2; + break; + case DateTranslator::getInstance()->translate('Letzter'): + $this->ordinal = 3; + break; + default: + throw new \BadMethodCallException("Ordinal is wrong or missing"); + } + + switch ($data[2]) { + case DateTranslator::getInstance()->translate('Montag'): + $this->day = 0; + break; + case DateTranslator::getInstance()->translate('Dienstag'): + $this->day = 1; + break; + case DateTranslator::getInstance()->translate('Mittwoch'): + $this->day = 2; + break; + case DateTranslator::getInstance()->translate('Donnerstag'): + $this->day = 3; + break; + case DateTranslator::getInstance()->translate('Freitag'): + $this->day = 4; + break; + case DateTranslator::getInstance()->translate('Samstag'): + $this->day = 5; + break; + case DateTranslator::getInstance()->translate('Sonntag'): + $this->day = 6; + break; + default: + throw new \BadMethodCallException("Day is wrong or missing"); + } + + switch($data[3]) { + case DateTranslator::getInstance()->translate('Monats'): + $this->month_or_year = 0; + break; + case DateTranslator::getInstance()->translate('Jahres'): + $this->month_or_year = 1; + break; + default: + throw new \BadMethodCallException("month or year is wrong or missing"); + } + } +} \ No newline at end of file diff --git a/src/enko/RelativeDateParser/RelativeDateType2.php b/src/enko/RelativeDateParser/RelativeDateType2.php new file mode 100755 index 0000000..8aaa6ce --- /dev/null +++ b/src/enko/RelativeDateParser/RelativeDateType2.php @@ -0,0 +1,47 @@ +translate('/Alle (\d+) ([Wochen|Tage]+)/im'); + } + + public function getCurrent(\DateTime $now) { + return $now; + } + + public function getNext(\DateTime $now) { + $date = clone $now; + if ($this->days_or_week == 0) { + $date->add(\DateInterval::createFromDateString(sprintf('+%s days',$this->interval))); + } else { + $date->add(\DateInterval::createFromDateString(sprintf('+%s weeks',$this->interval))); + } + return $date; + } + + function __construct($data) { + $interval = intval($data[1]); + if (is_integer($interval)) { + $this->interval = $data[1]; + } else { + throw new \BadMethodCallException("interval is wrong or missing"); + } + + switch($data[2]) { + case DateTranslator::getInstance()->translate('Tage'): + $this->days_or_week = 0; + break; + case DateTranslator::getInstance()->translate('Wochen'): + $this->days_or_week = 1; + break; + default: + throw new \BadMethodCallException("days or weeks are missing"); + } + } +} \ No newline at end of file