reduce config complexity by converting php format to js format (#734)
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
|
||||
49
src/Utils/MomentFormatConverter.php
Normal file
49
src/Utils/MomentFormatConverter.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user