* use php-cs-fixer instead of phpcodesniffer * updated scrutinizer config * adjusted code yules to all files * updated CONTRIBUTING guidelines
135 lines
3.1 KiB
PHP
135 lines
3.1 KiB
PHP
<?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\Twig;
|
|
|
|
use App\Utils\Duration;
|
|
use Symfony\Component\Intl\Intl;
|
|
use App\Entity\Timesheet;
|
|
use Twig\TwigFilter;
|
|
|
|
/**
|
|
* Multiple Twig extensions: filters and functions
|
|
*/
|
|
class Extensions extends \Twig_Extension
|
|
{
|
|
/**
|
|
* @var string[]
|
|
*/
|
|
private $locales;
|
|
|
|
/**
|
|
* @var Duration
|
|
*/
|
|
protected $durationFormatter;
|
|
|
|
/**
|
|
* Extensions constructor.
|
|
* @param string $locales
|
|
*/
|
|
public function __construct($locales)
|
|
{
|
|
$this->locales = explode('|', $locales);
|
|
$this->durationFormatter = new Duration();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getFilters()
|
|
{
|
|
return [
|
|
new TwigFilter('duration', [$this, 'duration']),
|
|
new TwigFilter('money', [$this, 'money']),
|
|
new TwigFilter('currency', [$this, 'currency']),
|
|
new TwigFilter('country', [$this, 'country']),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getFunctions()
|
|
{
|
|
return [
|
|
new \Twig_SimpleFunction('locales', [$this, 'getLocales']),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Transforms seconds into a duration string.
|
|
*
|
|
* @param int|Timesheet $duration
|
|
* @param bool $includeSeconds
|
|
* @return string
|
|
*/
|
|
public function duration($duration, $includeSeconds = false)
|
|
{
|
|
$seconds = $duration;
|
|
if ($duration instanceof Timesheet) {
|
|
$seconds = $duration->getDuration();
|
|
if (null === $duration->getEnd()) {
|
|
$seconds = time() - $duration->getBegin()->getTimestamp();
|
|
}
|
|
}
|
|
|
|
return $this->durationFormatter->format($seconds, $includeSeconds) . ' h';
|
|
}
|
|
|
|
/**
|
|
* @param string $currency
|
|
* @return string
|
|
*/
|
|
public function currency($currency)
|
|
{
|
|
return Intl::getCurrencyBundle()->getCurrencySymbol($currency);
|
|
}
|
|
|
|
/**
|
|
* @param string $country
|
|
* @return string
|
|
*/
|
|
public function country($country)
|
|
{
|
|
return Intl::getRegionBundle()->getCountryName($country);
|
|
}
|
|
|
|
/**
|
|
* @param float $amount
|
|
* @param string $currency
|
|
* @return string
|
|
*/
|
|
public function money($amount, $currency = null)
|
|
{
|
|
$result = number_format(round($amount, 2), 2);
|
|
if (null !== $currency) {
|
|
$result .= ' ' . Intl::getCurrencyBundle()->getCurrencySymbol($currency);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Takes the list of codes of the locales (languages) enabled in the
|
|
* application and returns an array with the name of each locale written
|
|
* in its own language (e.g. English, Français, Español, etc.)
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getLocales()
|
|
{
|
|
$locales = [];
|
|
foreach ($this->locales as $locale) {
|
|
$locales[] = ['code' => $locale, 'name' => Intl::getLocaleBundle()->getLocaleName($locale, $locale)];
|
|
}
|
|
|
|
return $locales;
|
|
}
|
|
}
|