Commit b23c9e15 authored by Tino Goratsch's avatar Tino Goratsch

reworked the Date class a little bit

parent 3242e1f5
......@@ -2,7 +2,7 @@
namespace ACP3\Core;
use ACP3\Core\I18n\Translator;
use ACP3\Core\Date\DateTranslator;
/**
* Class Date
......@@ -28,35 +28,37 @@ class Date
protected $dateTimeZone;
/**
* @var \ACP3\Core\I18n\Translator
* @var \ACP3\Core\Date\DateTranslator
*/
protected $translator;
protected $dateTranslator;
/**
* @var \ACP3\Core\Config
*/
protected $config;
/**
* @param \ACP3\Core\User $user
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Config $config
* Date constructor.
*
* @param \ACP3\Core\User $user
* @param \ACP3\Core\Date\DateTranslator $dateTranslator
* @param \ACP3\Core\Config $config
*/
public function __construct(
User $user,
Translator $translator,
DateTranslator $dateTranslator,
Config $config
)
{
$this->translator = $translator;
$this->dateTranslator = $dateTranslator;
$this->config = $config;
$this->_setFormatAndTimeZone($user->getUserInfo());
$this->setFormatAndTimeZone($user->getUserInfo());
}
/**
* @param array $userInfo
*/
protected function _setFormatAndTimeZone(array $userInfo = [])
protected function setFormatAndTimeZone(array $userInfo = [])
{
if (!empty($userInfo)) {
$this->dateFormatLong = $userInfo['date_format_long'];
......@@ -146,106 +148,7 @@ class Date
$dateTime->setTimestamp($dateTime->getTimestamp() - $dateTime->getOffset());
}
}
return strtr($dateTime->format($format), $this->localizeDaysAndMonths($format));
}
/**
* @param string $format
*
* @return array
*/
protected function localizeDaysAndMonths($format)
{
$replace = [];
// Localize days
if (strpos($format, 'D') !== false) {
$replace = $this->localizeDaysAbbr();
} elseif (strpos($format, 'l') !== false) {
$replace = $this->localizeDays();
}
// Localize months
if (strpos($format, 'M') !== false) {
$replace = array_merge($replace, $this->localizeMonthsAbbr());
} elseif (strpos($format, 'F') !== false) {
$replace = array_merge($replace, $this->localizeMonths());
}
return $replace;
}
/**
* @return array
*/
protected function localizeDaysAbbr()
{
return [
'Mon' => $this->translator->t('system', 'date_mon'),
'Tue' => $this->translator->t('system', 'date_tue'),
'Wed' => $this->translator->t('system', 'date_wed'),
'Thu' => $this->translator->t('system', 'date_thu'),
'Fri' => $this->translator->t('system', 'date_fri'),
'Sat' => $this->translator->t('system', 'date_sat'),
'Sun' => $this->translator->t('system', 'date_sun')
];
}
/**
* @return array
*/
protected function localizeDays()
{
return [
'Monday' => $this->translator->t('system', 'date_monday'),
'Tuesday' => $this->translator->t('system', 'date_tuesday'),
'Wednesday' => $this->translator->t('system', 'date_wednesday'),
'Thursday' => $this->translator->t('system', 'date_thursday'),
'Friday' => $this->translator->t('system', 'date_friday'),
'Saturday' => $this->translator->t('system', 'date_saturday'),
'Sunday' => $this->translator->t('system', 'date_sunday')
];
}
/**
* @return array
*/
protected function localizeMonthsAbbr()
{
return [
'Jan' => $this->translator->t('system', 'date_jan'),
'Feb' => $this->translator->t('system', 'date_feb'),
'Mar' => $this->translator->t('system', 'date_mar'),
'Apr' => $this->translator->t('system', 'date_apr'),
'May' => $this->translator->t('system', 'date_may_abbr'),
'Jun' => $this->translator->t('system', 'date_jun'),
'Jul' => $this->translator->t('system', 'date_jul'),
'Aug' => $this->translator->t('system', 'date_aug'),
'Sep' => $this->translator->t('system', 'date_sep'),
'Oct' => $this->translator->t('system', 'date_oct'),
'Nov' => $this->translator->t('system', 'date_nov'),
'Dec' => $this->translator->t('system', 'date_dec')
];
}
/**
* @return array
*/
protected function localizeMonths()
{
return [
'January' => $this->translator->t('system', 'date_january'),
'February' => $this->translator->t('system', 'date_february'),
'March' => $this->translator->t('system', 'date_march'),
'April' => $this->translator->t('system', 'date_april'),
'May' => $this->translator->t('system', 'date_may_full'),
'June' => $this->translator->t('system', 'date_june'),
'July' => $this->translator->t('system', 'date_july'),
'August' => $this->translator->t('system', 'date_august'),
'September' => $this->translator->t('system', 'date_september'),
'October' => $this->translator->t('system', 'date_october'),
'November' => $this->translator->t('system', 'date_november'),
'December' => $this->translator->t('system', 'date_december')
];
return strtr($dateTime->format($format), $this->dateTranslator->localize($format));
}
/**
......
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers. See the LICENCE file at the top-level module directory for licencing
* details.
*/
namespace ACP3\Core\Date;
use ACP3\Core\I18n\Translator;
/**
* Class DateTranslator
* @package ACP3\Core\Date
*/
class DateTranslator
{
/**
* @var \ACP3\Core\I18n\Translator
*/
protected $translator;
/**
* @var array
*/
protected $cache = [];
/**
* @var array
*/
protected $daysAbbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
/**
* @var array
*/
protected $daysFull = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
/**
* @var array
*/
protected $dateAbbr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
/**
* @var array
*/
protected $dateFull = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
/**
* DateTranslator constructor.
*
* @param \ACP3\Core\I18n\Translator $translator
*/
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
/**
* @param string $dateFormat
*
* @return array
*/
public function localize($dateFormat)
{
$replace = [];
// Localize days
if (strpos($dateFormat, 'D') !== false) {
$replace = $this->localizeDaysAbbr();
} elseif (strpos($dateFormat, 'l') !== false) {
$replace = $this->localizeDays();
}
// Localize months
if (strpos($dateFormat, 'M') !== false) {
$replace = array_merge($replace, $this->localizeMonthsAbbr());
} elseif (strpos($dateFormat, 'F') !== false) {
$replace = array_merge($replace, $this->localizeMonths());
}
return $replace;
}
/**
* @param array $search
* @param string $translatorPrefix
*/
protected function cacheLocalizedDate(array $search, $translatorPrefix)
{
if (!isset($this->cache[$translatorPrefix])) {
$buffer = [];
foreach ($search as $key) {
$buffer[$key] = $this->translator->t('system', $translatorPrefix . '_' . strtolower($key));
}
$this->cache[$translatorPrefix] = $buffer;
}
return $this->cache[$translatorPrefix];
}
/**
* @return array
*/
protected function localizeDaysAbbr()
{
return $this->cacheLocalizedDate($this->daysAbbr, 'day_abbr');
}
/**
* @return array
*/
protected function localizeDays()
{
return $this->cacheLocalizedDate($this->daysFull, 'day_full');
}
/**
* @return array
*/
protected function localizeMonthsAbbr()
{
return $this->cacheLocalizedDate($this->dateAbbr, 'date_abbr');
}
/**
* @return array
*/
protected function localizeMonths()
{
return $this->cacheLocalizedDate($this->dateFull, 'date_full');
}
}
\ No newline at end of file
<?php
namespace ACP3\Core;
use Monolog\Handler\AbstractHandler;
use Monolog\Logger as MonologLogger;
use Psr\Log\LogLevel;
use Monolog\Handler\AbstractHandler;
/**
* Monolog error handler
......
......@@ -5,8 +5,8 @@ namespace ACP3\Core\Modules;
use ACP3\Core;
use ACP3\Core\Modules\Installer\MigrationInterface;
use ACP3\Core\Modules\Installer\SchemaInterface;
use ACP3\Modules\ACP3\System;
use ACP3\Modules\ACP3\Permissions;
use ACP3\Modules\ACP3\System;
/**
* Class SchemaUpdater
......
services:
core.date:
class: ACP3\Core\Date
arguments:
- '@core.user'
- '@core.date.date_translator'
- '@core.config'
core.date.date_translator:
class: ACP3\Core\Date\DateTranslator
arguments:
- '@core.lang'
......@@ -4,6 +4,7 @@ imports:
- { resource: components/assets.yml }
- { resource: components/authentication.yml }
- { resource: components/datagrid.yml }
- { resource: components/date.yml }
- { resource: components/helpers.yml }
- { resource: components/http.yml }
- { resource: components/language.yml }
......@@ -34,10 +35,6 @@ services:
class: ACP3\Core\Config
arguments: ['@core.cache.system', '@system.model.modulerepository', @system.model.settingsrepository]
core.date:
class: ACP3\Core\Date
arguments: ['@core.user', '@core.lang', '@core.config']
core.db:
class: ACP3\Core\DB
arguments:
......
......@@ -64,51 +64,51 @@
<item key="data_table_zero_records">0 Datensätze.</item>
<item key="database">Datenbank</item>
<item key="date">Datum</item>
<item key="date_apr">Apr</item>
<item key="date_april">April</item>
<item key="date_aug">Aug</item>
<item key="date_august">August</item>
<item key="date_dec">Dez</item>
<item key="date_december">Dezember</item>
<item key="date_abbr_apr">Apr</item>
<item key="date_abbr_aug">Aug</item>
<item key="date_abbr_dec">Dez</item>
<item key="date_abbr_feb">Feb</item>
<item key="date_abbr_jan">Jan</item>
<item key="date_abbr_jul">Jul</item>
<item key="date_abbr_jun">Jun</item>
<item key="date_abbr_may">Mai</item>
<item key="date_abbr_mar">Mär</item>
<item key="date_abbr_nov">Nov</item>
<item key="date_abbr_oct">Okt</item>
<item key="date_abbr_sep">Sep</item>
<item key="date_description">Wenn Sie kein Enddatum festlegen möchten, so setzen Sie bitte das Veröffentlichungsdatum und das Enddatum auf die gleichen Werte.</item>
<item key="date_feb">Feb</item>
<item key="date_february">Februar</item>
<item key="date_format">Datumsformat</item>
<item key="date_format_long">Langes Datumsformat</item>
<item key="date_format_short">Kurzes Datumsformat</item>
<item key="date_fri">Fr</item>
<item key="date_friday">Freitag</item>
<item key="date_jan">Jan</item>
<item key="date_january">Januar</item>
<item key="date_jul">Jul</item>
<item key="date_july">Juli</item>
<item key="date_jun">Jun</item>
<item key="date_june">Juni</item>
<item key="date_may_abbr">Mai</item>
<item key="date_may_full">Mai</item>
<item key="date_mar">Mär</item>
<item key="date_march">März</item>
<item key="date_mon">Mo</item>
<item key="date_monday">Montag</item>
<item key="date_nov">Nov</item>
<item key="date_november">November</item>
<item key="date_oct">Okt</item>
<item key="date_october">Oktober</item>
<item key="date_full_april">April</item>
<item key="date_full_august">August</item>
<item key="date_full_december">Dezember</item>
<item key="date_full_february">Februar</item>
<item key="date_full_january">Januar</item>
<item key="date_full_july">Juli</item>
<item key="date_full_june">Juni</item>
<item key="date_full_may">Mai</item>
<item key="date_full_march">März</item>
<item key="date_full_november">November</item>
<item key="date_full_october">Oktober</item>
<item key="date_full_september">September</item>
<item key="date_published_since">Seit %date%</item>
<item key="date_sat">Sa</item>
<item key="date_saturday">Samstag</item>
<item key="date_sep">Sep</item>
<item key="date_september">September</item>
<item key="date_sun">So</item>
<item key="date_sunday">Sonntag</item>
<item key="date_thu">Do</item>
<item key="date_thursday">Donnerstag</item>
<item key="date_time_range"><![CDATA[%1$s&ndash;%2$s]]></item>
<item key="date_tue">Di</item>
<item key="date_tuesday">Dienstag</item>
<item key="date_wed">Mi</item>
<item key="date_wednesday">Mittwoch</item>
<item key="day">Tag</item>
<item key="day_abbr_fri">Fr</item>
<item key="day_abbr_mon">Mo</item>
<item key="day_abbr_sat">Sa</item>
<item key="day_abbr_sun">So</item>
<item key="day_abbr_thu">Do</item>
<item key="day_abbr_tue">Di</item>
<item key="day_abbr_wed">Mi</item>
<item key="day_full_friday">Freitag</item>
<item key="day_full_monday">Montag</item>
<item key="day_full_saturday">Samstag</item>
<item key="day_full_sunday">Sonntag</item>
<item key="day_full_thursday">Donnerstag</item>
<item key="day_full_tuesday">Dienstag</item>
<item key="day_full_wednesday">Mittwoch</item>
<item key="delete">Löschen</item>
<item key="delete_caches_description">Auf dieser Seite haben Sie die Möglichkeit die vom ACP3 erstellten Caches zu leeren.</item>
<item key="delete_error">Beim Löschen der ausgewählten Einträge ist ein Fehler aufgetreten.</item>
......
......@@ -64,51 +64,51 @@
<item key="data_table_zero_records">0 records.</item>
<item key="database">Database</item>
<item key="date">Date</item>
<item key="date_apr">Apr</item>
<item key="date_april">April</item>
<item key="date_aug">Aug</item>
<item key="date_august">August</item>
<item key="date_dec">Dec</item>
<item key="date_december">December</item>
<item key="date_abbr_apr">Apr</item>
<item key="date_abbr_aug">Aug</item>
<item key="date_abbr_dec">Dec</item>
<item key="date_abbr_feb">Feb</item>
<item key="date_abbr_jan">Jan</item>
<item key="date_abbr_jul">Jul</item>
<item key="date_abbr_jun">Jun</item>
<item key="date_abbr_may">May</item>
<item key="date_abbr_mar">Mar</item>
<item key="date_abbr_nov">Nov</item>
<item key="date_abbr_oct">Oct</item>
<item key="date_abbr_sep">Sep</item>
<item key="date_description">If you wouldn't like to set a enddate, just set the release date and the end date to the same values.</item>
<item key="date_feb">Feb</item>
<item key="date_february">February</item>
<item key="date_format">Date format</item>
<item key="date_format_long">Long date format</item>
<item key="date_format_short">Short date format</item>
<item key="date_fri">Fri</item>
<item key="date_friday">Friday</item>
<item key="date_jan">Jan</item>
<item key="date_january">January</item>
<item key="date_jul">Jul</item>
<item key="date_july">July</item>
<item key="date_jun">Jun</item>
<item key="date_june">June</item>
<item key="date_may_abbr">May</item>
<item key="date_may_full">May</item>
<item key="date_mar">Mar</item>
<item key="date_march">March</item>
<item key="date_mon">Mon</item>
<item key="date_monday">Monday</item>
<item key="date_nov">Nov</item>
<item key="date_november">November</item>
<item key="date_oct">Oct</item>
<item key="date_october">October</item>
<item key="date_full_april">April</item>
<item key="date_full_august">August</item>
<item key="date_full_december">December</item>
<item key="date_full_february">February</item>
<item key="date_full_january">January</item>
<item key="date_full_july">July</item>
<item key="date_full_june">June</item>
<item key="date_full_may">May</item>
<item key="date_full_march">March</item>
<item key="date_full_november">November</item>
<item key="date_full_october">October</item>
<item key="date_full_september">September</item>
<item key="date_published_since">Since %date%</item>
<item key="date_sat">Sat</item>
<item key="date_saturday">Saturday</item>
<item key="date_sep">Sep</item>
<item key="date_september">September</item>
<item key="date_sun">Sun</item>
<item key="date_sunday">Sunday</item>
<item key="date_thu">Thu</item>
<item key="date_thursday">Thursday</item>
<item key="date_time_range"><![CDATA[%1$s&ndash;%2$s]]></item>
<item key="date_tue">Tue</item>
<item key="date_tuesday">Tuesday</item>
<item key="date_wed">Wed</item>
<item key="date_wednesday">Wednesday</item>
<item key="day">Day</item>
<item key="day_abbr_fri">Fri</item>
<item key="day_abbr_mon">Mon</item>
<item key="day_abbr_sat">Sat</item>
<item key="day_abbr_sun">Sun</item>
<item key="day_abbr_thu">Thu</item>
<item key="day_abbr_tue">Tue</item>
<item key="day_abbr_wed">Wed</item>
<item key="day_full_friday">Friday</item>
<item key="day_full_monday">Monday</item>
<item key="day_full_saturday">Saturday</item>
<item key="day_full_sunday">Sunday</item>
<item key="day_full_thursday">Thursday</item>
<item key="day_full_tuesday">Tuesday</item>
<item key="day_full_wednesday">Wednesday</item>
<item key="delete">Delete</item>
<item key="delete_caches_description">On this page you can refresh the automatically generated caches of the ACP3.</item>
<item key="delete_error">While deleting the marked entries an error has occured.</item>
......
<?php
namespace ACP3\Installer\Core;
use ACP3\Installer\Core\I18n\Translator;
use ACP3\Core\Date\DateTranslator;
/**
* Class Date
......@@ -10,13 +10,15 @@ use ACP3\Installer\Core\I18n\Translator;
class Date extends \ACP3\Core\Date
{
/**
* @param \ACP3\Installer\Core\I18n\Translator $translator
* Date constructor.
*
* @param \ACP3\Core\Date\DateTranslator $dateTranslator
*/
public function __construct(
Translator $translator
DateTranslator $dateTranslator
)
{
$this->translator = $translator;
$this->dateTranslator = $dateTranslator;
$defaultTimeZone = date_default_timezone_get();
......@@ -25,6 +27,6 @@ class Date extends \ACP3\Core\Date
'date_format_short' => 'd.m.y',
'time_zone' => !empty($defaultTimeZone) ? $defaultTimeZone : 'UTC',
];
$this->_setFormatAndTimeZone($settings);
$this->setFormatAndTimeZone($settings);
}
}
......@@ -21,6 +21,10 @@ services:
core.date:
class: ACP3\Installer\Core\Date
arguments: ['@core.date.date_translator']
core.date.date_translator:
class: ACP3\Core\Date\DateTranslator
arguments: ['@core.lang']
core.helpers.alerts:
......
......@@ -14,6 +14,10 @@ class DateColumnRendererTest extends AbstractColumnRendererTest
* @var \ACP3\Core\Config|PHPUnit_Framework_MockObject_MockObject
*/
protected $configMock;
/**
* @var \ACP3\Core\Date\DateTranslator
*/
protected $dateTranslator;
/**
* @var \ACP3\Core\Date
*/
......@@ -29,6 +33,7 @@ class DateColumnRendererTest extends AbstractColumnRendererTest
->disableOriginalConstructor()
->setMethods(['t'])
->getMock();
$this->dateTranslator = new \ACP3\Core\Date\DateTranslator($this->langMock);
$this->userMock = $this->getMockBuilder(\ACP3\Core\User::class)
->disableOriginalConstructor()
->getMock();
......@@ -46,7 +51,7 @@ class DateColumnRendererTest extends AbstractColumnRendererTest
$this->date = new \ACP3\Core\Date(
$this->userMock,
$this->langMock,
$this->dateTranslator,
$this->configMock
);
$this->dateRange = new \ACP3\Core\Helpers\Formatter\DateRange(
......
......@@ -6,6 +6,10 @@ class DateRangeTest extends PHPUnit_Framework_TestCase
* @var \ACP3\Core\Config|PHPUnit_Framework_MockObject_MockObject
*/
private $configMock;
/**
* @var \ACP3\Core\Date\DateTranslator
*/
private $dateTranslator;
/**
* @var \ACP3\Core\Date
*/
......@@ -45,9 +49,11 @@ class DateRangeTest extends PHPUnit_Framework_TestCase
'time_zone' => 'Europe/Berlin',
]);
$this->dateTranslator = new \ACP3\Core\Date\DateTranslator($this->langMock);
$this->date = new \ACP3\Core\Date(
$this->userMock,
$this->langMock,
$this->dateTranslator,
$this->configMock
);
$this->dateRange = new \ACP3\Core\Helpers\Formatter\DateRange(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment