]>
Raphaël G. Git Repositories - packbundle/blob - Util/IntlUtil.php
1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys PackBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\PackBundle\Util
;
14 use Twig\Error\SyntaxError
;
18 * Helps manage intl conversions
24 public function __construct() {
27 public function date(Environment
$env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null, $calendar = 'gregorian') {
28 $date = twig_date_converter($env, $date, $timezone);
30 $formatValues = array(
31 'none' => IntlDateFormatter
::NONE
,
32 'short' => IntlDateFormatter
::SHORT
,
33 'medium' => IntlDateFormatter
::MEDIUM
,
34 'long' => IntlDateFormatter
::LONG
,
35 'full' => IntlDateFormatter
::FULL
,
38 $formatter = IntlDateFormatter
::create(
40 $formatValues[$dateFormat],
41 $formatValues[$timeFormat],
42 IntlTimeZone
::createTimeZone($date->getTimezone()->getName()),
43 'gregorian' === $calendar ? IntlDateFormatter
::GREGORIAN
: IntlDateFormatter
::TRADITIONAL
,
47 return $formatter->format($date->getTimestamp());
50 public function number($number, $style = 'decimal', $type = 'default', $locale = null) {
51 static $typeValues = array(
52 'default' => NumberFormatter
::TYPE_DEFAULT
,
53 'int32' => NumberFormatter
::TYPE_INT32
,
54 'int64' => NumberFormatter
::TYPE_INT64
,
55 'double' => NumberFormatter
::TYPE_DOUBLE
,
56 'currency' => NumberFormatter
::TYPE_CURRENCY
,
59 $formatter = $this->getNumberFormatter($locale, $style);
61 if (!isset($typeValues[$type])) {
62 throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($typeValues))));
65 return $formatter->format($number, $typeValues[$type]);
68 public function currency($number, $currency = null, $locale = null) {
69 $formatter = $this->getNumberFormatter($locale, 'currency');
71 return $formatter->formatCurrency($number, $currency);
75 * Gets a number formatter instance according to given locale and formatter.
77 * @param string $locale Locale in which the number would be formatted
78 * @param int $style Style of the formatting
80 * @return NumberFormatter A NumberFormatter instance
82 protected function getNumberFormatter($locale, $style): NumberFormatter
{
83 static $formatter, $currentStyle;
85 $locale = null !== $locale ? $locale : Locale
::getDefault();
87 if ($formatter && $formatter->getLocale() === $locale && $currentStyle === $style) {
88 // Return same instance of NumberFormatter if parameters are the same
89 // to those in previous call
93 static $styleValues = array(
94 'decimal' => NumberFormatter
::DECIMAL
,
95 'currency' => NumberFormatter
::CURRENCY
,
96 'percent' => NumberFormatter
::PERCENT
,
97 'scientific' => NumberFormatter
::SCIENTIFIC
,
98 'spellout' => NumberFormatter
::SPELLOUT
,
99 'ordinal' => NumberFormatter
::ORDINAL
,
100 'duration' => NumberFormatter
::DURATION
,
103 if (!isset($styleValues[$style])) {
104 throw new SyntaxError(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
107 $currentStyle = $style;
109 $formatter = NumberFormatter
::create($locale, $styleValues[$style]);