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; } }