Initial commit.

This commit is contained in:
Tim Schumacher 2014-09-21 16:14:57 +02:00
commit 3a16813fc8
9 changed files with 702 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
vendor/

260
RelativeDateParser.php Normal file
View file

@ -0,0 +1,260 @@
<?php
require "vendor/autoload.php";
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\Loader\PoFileLoader;
class DateTranslator {
/** @var DateTranslator */
private static $instance = null;
private $translator = null;
public static function getInstance($lang = '') {
if (is_null(static::$instance)) {
static::$instance = new static($lang);
}
return static::$instance;
}
private function __construct($lang) {
if ($lang == '') {
throw new BadMethodCallException('Language is missing');
}
$dir_iterator = new RecursiveDirectoryIterator("translations");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if (basename($file,'.po') == $lang) {
$loader = new PoFileLoader();
$loader->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);
}
}

14
composer.json Normal file
View file

@ -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"
}
]
}

167
composer.lock generated Normal file
View file

@ -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": []
}

BIN
translations/de.mo Normal file

Binary file not shown.

87
translations/de.po Normal file
View file

@ -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 <EMAIL@ADDRESS>, 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"

BIN
translations/en.mo Normal file

Binary file not shown.

87
translations/en.po Normal file
View file

@ -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 <EMAIL@ADDRESS>, 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"

86
translations/messages.pot Normal file
View file

@ -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 <EMAIL@ADDRESS>, 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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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 ""