commit 3a16813fc87ad7f9889fb9c507e86d1132432303 Author: Tim Schumacher Date: Sun Sep 21 16:14:57 2014 +0200 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a725465 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor/ \ No newline at end of file diff --git a/RelativeDateParser.php b/RelativeDateParser.php new file mode 100644 index 0000000..0a8a42c --- /dev/null +++ b/RelativeDateParser.php @@ -0,0 +1,260 @@ +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(DateTime $now) { + return $this->datetype->getCurrent($now); + } + + public function getNext(DateTime $now) { + return $this->datetype->getNext($now); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cefc28f --- /dev/null +++ b/composer.json @@ -0,0 +1,14 @@ +{ + "name": "enko/relativedateparser", + "require": { + "symfony/Translation" : "~2.5", + "symfony/Config" : "~2.5" + }, + "license": "MIT", + "authors": [ + { + "name": "Tim Schumacher", + "email": "tim@bandenkrieg.hacked.jp" + } + ] +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..56dc5d2 --- /dev/null +++ b/composer.lock @@ -0,0 +1,167 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "980f698facbe00b4fb22c09427c7dc0e", + "packages": [ + { + "name": "symfony/config", + "version": "v2.5.4", + "target-dir": "Symfony/Component/Config", + "source": { + "type": "git", + "url": "https://github.com/symfony/Config.git", + "reference": "080eabdc256c1d7a3a7cf6296271edb68eb1ab2b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/Config/zipball/080eabdc256c1d7a3a7cf6296271edb68eb1ab2b", + "reference": "080eabdc256c1d7a3a7cf6296271edb68eb1ab2b", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "symfony/filesystem": "~2.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.5-dev" + } + }, + "autoload": { + "psr-0": { + "Symfony\\Component\\Config\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Symfony Config Component", + "homepage": "http://symfony.com", + "time": "2014-08-31 03:22:04" + }, + { + "name": "symfony/filesystem", + "version": "v2.5.4", + "target-dir": "Symfony/Component/Filesystem", + "source": { + "type": "git", + "url": "https://github.com/symfony/Filesystem.git", + "reference": "a765efd199e02ff4001c115c318e219030be9364" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/Filesystem/zipball/a765efd199e02ff4001c115c318e219030be9364", + "reference": "a765efd199e02ff4001c115c318e219030be9364", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.5-dev" + } + }, + "autoload": { + "psr-0": { + "Symfony\\Component\\Filesystem\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Symfony Filesystem Component", + "homepage": "http://symfony.com", + "time": "2014-09-03 09:00:14" + }, + { + "name": "symfony/translation", + "version": "v2.5.4", + "target-dir": "Symfony/Component/Translation", + "source": { + "type": "git", + "url": "https://github.com/symfony/Translation.git", + "reference": "7526ad65f1961b2422ab33e4d3b05f92be16e5e2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/Translation/zipball/7526ad65f1961b2422ab33e4d3b05f92be16e5e2", + "reference": "7526ad65f1961b2422ab33e4d3b05f92be16e5e2", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "symfony/config": "~2.0", + "symfony/yaml": "~2.2" + }, + "suggest": { + "symfony/config": "", + "symfony/yaml": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.5-dev" + } + }, + "autoload": { + "psr-0": { + "Symfony\\Component\\Translation\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + }, + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Symfony Translation Component", + "homepage": "http://symfony.com", + "time": "2014-09-03 09:00:14" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "platform": [], + "platform-dev": [] +} diff --git a/translations/de.mo b/translations/de.mo new file mode 100644 index 0000000..48fdc1f Binary files /dev/null and b/translations/de.mo differ diff --git a/translations/de.po b/translations/de.po new file mode 100644 index 0000000..6ef9aa5 --- /dev/null +++ b/translations/de.po @@ -0,0 +1,87 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: \n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-09-21 15:24+0200\n" +"PO-Revision-Date: 2014-09-21 15:31+0100\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 1.6.9\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"Language: de\n" + +#: RelativeDateParser.php:48 +msgid "/([Erster|Zweiter|Dritter|Letzter]+) ([Montag|Dienstag|Mittwoch|Donnerstag|Freitag|Samstag|Sonntag]+) des ([Monats|Jahres]+)/im" +msgstr "/([Erster|Zweiter|Dritter|Letzter]+) ([Montag|Dienstag|Mittwoch|Donnerstag|Freitag|Samstag|Sonntag]+) des ([Monats|Jahres]+)/im" + +#: RelativeDateParser.php:106 +msgid "Erster" +msgstr "Erster" + +#: RelativeDateParser.php:109 +msgid "Zweiter" +msgstr "Zweiter" + +#: RelativeDateParser.php:112 +msgid "Dritter" +msgstr "Dritter" + +#: RelativeDateParser.php:115 +msgid "Letzter" +msgstr "Letzter" + +#: RelativeDateParser.php:123 +msgid "Montag" +msgstr "Montag" + +#: RelativeDateParser.php:126 +msgid "Dienstag" +msgstr "Dienstag" + +#: RelativeDateParser.php:129 +msgid "Mittwoch" +msgstr "Mittwoch" + +#: RelativeDateParser.php:132 +msgid "Donnerstag" +msgstr "Donnerstag" + +#: RelativeDateParser.php:135 +msgid "Freitag" +msgstr "Freitag" + +#: RelativeDateParser.php:138 +msgid "Samstag" +msgstr "Samstag" + +#: RelativeDateParser.php:141 +msgid "Sonntag" +msgstr "Sonntag" + +#: RelativeDateParser.php:149 +msgid "Monats" +msgstr "Monats" + +#: RelativeDateParser.php:152 +msgid "Jahres" +msgstr "Jahres" + +#: RelativeDateParser.php:166 +msgid "/Alle (\\d+) ([Wochen|Tage]+)/im" +msgstr "/Alle (\\d+) ([Wochen|Tage]+)/im" + +#: RelativeDateParser.php:192 +msgid "Tage" +msgstr "Tage" + +#: RelativeDateParser.php:195 +msgid "Wochen" +msgstr "Wochen" diff --git a/translations/en.mo b/translations/en.mo new file mode 100644 index 0000000..cd3c4e9 Binary files /dev/null and b/translations/en.mo differ diff --git a/translations/en.po b/translations/en.po new file mode 100644 index 0000000..caf5473 --- /dev/null +++ b/translations/en.po @@ -0,0 +1,87 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: \n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-09-21 15:24+0200\n" +"PO-Revision-Date: 2014-09-21 15:29+0100\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 1.6.9\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"Language: en\n" + +#: RelativeDateParser.php:48 +msgid "/([Erster|Zweiter|Dritter|Letzter]+) ([Montag|Dienstag|Mittwoch|Donnerstag|Freitag|Samstag|Sonntag]+) des ([Monats|Jahres]+)/im" +msgstr "/([First|Second|Third|Last]+) ([monday|tuesday|wednesday|thursday|friday|saturday|sunday]+) of the ([month|year]+)/im" + +#: RelativeDateParser.php:106 +msgid "Erster" +msgstr "First" + +#: RelativeDateParser.php:109 +msgid "Zweiter" +msgstr "Second" + +#: RelativeDateParser.php:112 +msgid "Dritter" +msgstr "Third" + +#: RelativeDateParser.php:115 +msgid "Letzter" +msgstr "Last" + +#: RelativeDateParser.php:123 +msgid "Montag" +msgstr "monday" + +#: RelativeDateParser.php:126 +msgid "Dienstag" +msgstr "tuesday" + +#: RelativeDateParser.php:129 +msgid "Mittwoch" +msgstr "wednesday" + +#: RelativeDateParser.php:132 +msgid "Donnerstag" +msgstr "thursday" + +#: RelativeDateParser.php:135 +msgid "Freitag" +msgstr "friday" + +#: RelativeDateParser.php:138 +msgid "Samstag" +msgstr "saturday" + +#: RelativeDateParser.php:141 +msgid "Sonntag" +msgstr "sunday" + +#: RelativeDateParser.php:149 +msgid "Monats" +msgstr "month" + +#: RelativeDateParser.php:152 +msgid "Jahres" +msgstr "year" + +#: RelativeDateParser.php:166 +msgid "/Alle (\\d+) ([Wochen|Tage]+)/im" +msgstr "/Every (\\d+) ([weeks|days]+)/im" + +#: RelativeDateParser.php:192 +msgid "Tage" +msgstr "days" + +#: RelativeDateParser.php:195 +msgid "Wochen" +msgstr "weeks" diff --git a/translations/messages.pot b/translations/messages.pot new file mode 100644 index 0000000..38f3014 --- /dev/null +++ b/translations/messages.pot @@ -0,0 +1,86 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-09-21 15:24+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=CHARSET\n" +"Content-Transfer-Encoding: 8bit\n" + +#: RelativeDateParser.php:48 +msgid "/([Erster|Zweiter|Dritter|Letzter]+) ([Montag|Dienstag|Mittwoch|Donnerstag|Freitag|Samstag|Sonntag]+) des ([Monats|Jahres]+)/im" +msgstr "" + +#: RelativeDateParser.php:106 +msgid "Erster" +msgstr "" + +#: RelativeDateParser.php:109 +msgid "Zweiter" +msgstr "" + +#: RelativeDateParser.php:112 +msgid "Dritter" +msgstr "" + +#: RelativeDateParser.php:115 +msgid "Letzter" +msgstr "" + +#: RelativeDateParser.php:123 +msgid "Montag" +msgstr "" + +#: RelativeDateParser.php:126 +msgid "Dienstag" +msgstr "" + +#: RelativeDateParser.php:129 +msgid "Mittwoch" +msgstr "" + +#: RelativeDateParser.php:132 +msgid "Donnerstag" +msgstr "" + +#: RelativeDateParser.php:135 +msgid "Freitag" +msgstr "" + +#: RelativeDateParser.php:138 +msgid "Samstag" +msgstr "" + +#: RelativeDateParser.php:141 +msgid "Sonntag" +msgstr "" + +#: RelativeDateParser.php:149 +msgid "Monats" +msgstr "" + +#: RelativeDateParser.php:152 +msgid "Jahres" +msgstr "" + +#: RelativeDateParser.php:166 +msgid "/Alle (\\d+) ([Wochen|Tage]+)/im" +msgstr "" + +#: RelativeDateParser.php:192 +msgid "Tage" +msgstr "" + +#: RelativeDateParser.php:195 +msgid "Wochen" +msgstr ""