added configurable formats for duration (#287)
This commit is contained in:
@@ -156,6 +156,8 @@ class Configuration implements ConfigurationInterface
|
||||
->arrayPrototype()
|
||||
->children()
|
||||
->scalarNode('date_short')->end()
|
||||
->scalarNode('duration')->end()
|
||||
->scalarNode('duration_short')->end()
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
@@ -67,7 +67,7 @@ class DurationType extends AbstractType
|
||||
$builder->addModelTransformer(new CallbackTransformer(
|
||||
function ($intToFormat) use ($formatter) {
|
||||
try {
|
||||
return $formatter->format($intToFormat, true);
|
||||
return $formatter->format($intToFormat);
|
||||
} catch (\Exception $e) {
|
||||
throw new TransformationFailedException($e->getMessage());
|
||||
}
|
||||
|
||||
@@ -85,14 +85,13 @@ class Extensions extends \Twig_Extension
|
||||
];
|
||||
|
||||
/**
|
||||
* Extensions constructor.
|
||||
* @param string $locales
|
||||
* @param string $locale
|
||||
* @param RequestStack $requestStack
|
||||
* @param array $languages
|
||||
*/
|
||||
public function __construct(RequestStack $requestStack, $locales)
|
||||
public function __construct(RequestStack $requestStack, array $languages)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->locales = explode('|', $locales);
|
||||
$this->locales = $languages;
|
||||
$this->durationFormatter = new Duration();
|
||||
}
|
||||
|
||||
@@ -157,10 +156,10 @@ class Extensions extends \Twig_Extension
|
||||
* Transforms seconds into a duration string.
|
||||
*
|
||||
* @param int|Timesheet $duration
|
||||
* @param bool $includeSeconds
|
||||
* @param string $format
|
||||
* @return string
|
||||
*/
|
||||
public function duration($duration, $includeSeconds = false)
|
||||
public function duration($duration, $format = null)
|
||||
{
|
||||
$seconds = $duration;
|
||||
if ($duration instanceof Timesheet) {
|
||||
@@ -170,7 +169,22 @@ class Extensions extends \Twig_Extension
|
||||
}
|
||||
}
|
||||
|
||||
return $this->durationFormatter->format($seconds, $includeSeconds) . ' h';
|
||||
$locale = $this->getLocale();
|
||||
switch ($format) {
|
||||
case 'full':
|
||||
$format = isset($this->locales[$locale]) ? $this->locales[$locale]['duration'] : null;
|
||||
break;
|
||||
case null:
|
||||
case 'short':
|
||||
$format = isset($this->locales[$locale]) ? $this->locales[$locale]['duration_short'] : null;
|
||||
break;
|
||||
}
|
||||
|
||||
if (null === $format) {
|
||||
$format = '%h:%m h';
|
||||
}
|
||||
|
||||
return $this->durationFormatter->format($seconds, $format);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,7 +258,7 @@ class Extensions extends \Twig_Extension
|
||||
public function getLocales()
|
||||
{
|
||||
$locales = [];
|
||||
foreach ($this->locales as $locale) {
|
||||
foreach (array_keys($this->locales) as $locale) {
|
||||
$locales[] = ['code' => $locale, 'name' => Intl::getLocaleBundle()->getLocaleName($locale, $locale)];
|
||||
}
|
||||
|
||||
|
||||
@@ -18,14 +18,17 @@ class Duration
|
||||
public const FORMAT_NATURAL = 'natural';
|
||||
public const FORMAT_SECONDS = 'seconds';
|
||||
|
||||
public const FORMAT_WITH_SECONDS = '%h:%m:%s';
|
||||
public const FORMAT_NO_SECONDS = '%h:%m';
|
||||
|
||||
/**
|
||||
* Transforms seconds into a duration string.
|
||||
*
|
||||
* @param $seconds
|
||||
* @param bool $includeSeconds
|
||||
* @param string $format
|
||||
* @return string
|
||||
*/
|
||||
public function format($seconds, $includeSeconds = false)
|
||||
public function format($seconds, $format = self::FORMAT_NO_SECONDS)
|
||||
{
|
||||
$hour = floor($seconds / 3600);
|
||||
$minute = floor(($seconds / 60) % 60);
|
||||
@@ -33,14 +36,13 @@ class Duration
|
||||
$hour = $hour > 9 ? $hour : '0' . $hour;
|
||||
$minute = $minute > 9 ? $minute : '0' . $minute;
|
||||
|
||||
if (!$includeSeconds) {
|
||||
return $hour . ':' . $minute;
|
||||
}
|
||||
|
||||
$second = $seconds % 60;
|
||||
$second = $second > 9 ? $second : '0' . $second;
|
||||
|
||||
return $hour . ':' . $minute . ':' . $second;
|
||||
$formatted = str_replace('%h', $hour, $format);
|
||||
$formatted = str_replace('%m', $minute, $formatted);
|
||||
|
||||
return str_replace('%s', $second, $formatted);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user