reduce config complexity by converting php format to js format (#734)

This commit is contained in:
Kevin Papst
2019-04-29 00:24:23 +02:00
committed by GitHub
parent 1b0b45649d
commit b8811924c3
7 changed files with 89 additions and 62 deletions

View File

@@ -9,12 +9,18 @@
namespace App\Configuration;
use App\Utils\MomentFormatConverter;
class LanguageFormattings
{
/**
* @var array
*/
protected $settings;
/**
* @var MomentFormatConverter
*/
protected $momentFormatter;
/**
* @param array $languageSettings
@@ -22,6 +28,7 @@ class LanguageFormattings
public function __construct(array $languageSettings)
{
$this->settings = $languageSettings;
$this->momentFormatter = new MomentFormatConverter();
}
/**
@@ -53,7 +60,7 @@ class LanguageFormattings
*/
public function getDatePickerFormat(string $locale): string
{
return $this->getConfig('date_picker', $locale);
return $this->momentFormatter->convert($this->getDateTypeFormat($locale));
}
/**
@@ -75,7 +82,7 @@ class LanguageFormattings
*/
public function getDateTimePickerFormat(string $locale): string
{
return $this->getConfig('date_time_picker', $locale);
return $this->momentFormatter->convert($this->getDateTimeTypeFormat($locale));
}
/**

View File

@@ -226,9 +226,7 @@ class Configuration implements ConfigurationInterface
->arrayPrototype()
->children()
->scalarNode('date_time_type')->defaultValue('yyyy-MM-dd HH:mm')->end() // for DateTimeType
->scalarNode('date_time_picker')->defaultValue('YYYY-MM-DD HH:mm')->end() // for DateTimeType JS component
->scalarNode('date_type')->defaultValue('yyyy-MM-dd')->end() // for DateType
->scalarNode('date_picker')->defaultValue('YYYY-MM-DD')->end() // for DateType JS component
->scalarNode('date')->defaultValue('Y-m-d')->end() // for display via twig
->scalarNode('date_time')->defaultValue('m-d H:i')->end() // for display via twig
->scalarNode('duration')->defaultValue('%%h:%%m h')->end() // for display via twig

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Utils;
/**
* This class is used to convert PHP date format to moment.js format.
*
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class MomentFormatConverter
{
/**
* This defines the mapping between PHP ICU date format (key) and moment.js date format (value)
* For ICU formats see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
* For Moment formats see http://momentjs.com/docs/#/displaying/format/.
*
* @var array
*/
private static $formatConvertRules = [
// year
'yyyy' => 'YYYY', 'yy' => 'YY', 'y' => 'YYYY',
// day
'dd' => 'DD', 'd' => 'D',
// day of week
'EE' => 'ddd', 'EEEEEE' => 'dd',
// timezone
'ZZZZZ' => 'Z', 'ZZZ' => 'ZZ',
// letter 'T'
'\'T\'' => 'T',
];
/**
* Returns associated moment.js format.
*
* @param string $format
* @return string
*/
public function convert(string $format): string
{
return strtr($format, self::$formatConvertRules);
}
}