]>
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 * Manages intl conversions
24 public function date(Environment
$env, \DateTime
$date, string $dateFormat = 'medium', string $timeFormat = 'medium', ?string $locale = null, \IntlTimeZone
|\DateTimeZone
|string|null $timezone = null, ?string $calendar = null, ?string $pattern = null) {
26 $date = twig_date_converter($env, $date, $timezone);
28 //Set date and time formatters
30 'none' => \IntlDateFormatter
::NONE
,
31 'short' => \IntlDateFormatter
::SHORT
,
32 'medium' => \IntlDateFormatter
::MEDIUM
,
33 'long' => \IntlDateFormatter
::LONG
,
34 'full' => \IntlDateFormatter
::FULL
,
38 $formatter = \IntlDateFormatter
::create(
40 $formatters[$dateFormat],
41 $formatters[$timeFormat],
42 \IntlTimeZone
::createTimeZone($date->getTimezone()->getName()),
43 'traditional' === $calendar ? \IntlDateFormatter
::TRADITIONAL
: \IntlDateFormatter
::GREGORIAN
,
47 //Return formatted date
48 return $formatter->format($date->getTimestamp());
54 public function number(int|float $number, $style = 'decimal', $type = 'default', ?string $locale = null) {
57 'default' => NumberFormatter
::TYPE_DEFAULT
,
58 'int32' => NumberFormatter
::TYPE_INT32
,
59 'int64' => NumberFormatter
::TYPE_INT64
,
60 'double' => NumberFormatter
::TYPE_DOUBLE
,
61 'currency' => NumberFormatter
::TYPE_CURRENCY
,
65 $formatter = $this->getNumberFormatter($locale, $style);
68 if (!isset($types[$type])) {
69 throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($types))));
72 //Return formatted number
73 return $formatter->format($number, $types[$type]);
79 public function currency(int|float $number, string $currency, ?string $locale = null) {
81 $formatter = $this->getNumberFormatter($locale, 'currency');
83 //Return formatted currency
84 return $formatter->formatCurrency($number, $currency);
88 * Gets number formatter instance matching locale and style.
90 * @param ?string $locale Locale in which the number would be formatted
91 * @param string $style Style of the formatting
93 * @return NumberFormatter A NumberFormatter instance
95 protected function getNumberFormatter(?string $locale, string $style): \NumberFormatter
{
96 //Set static formatters
97 static $formatters = [];
100 $locale = null !== $locale ? $locale : Locale
::getDefault();
102 //With existing formatter
103 if (isset($formatters[$locale][$style])) {
104 //Return the instance from previous call
105 return $formatters[$locale][$style];
110 'decimal' => \NumberFormatter
::DECIMAL
,
111 'currency' => \NumberFormatter
::CURRENCY
,
112 'percent' => \NumberFormatter
::PERCENT
,
113 'scientific' => \NumberFormatter
::SCIENTIFIC
,
114 'spellout' => \NumberFormatter
::SPELLOUT
,
115 'ordinal' => \NumberFormatter
::ORDINAL
,
116 'duration' => \NumberFormatter
::DURATION
,
120 if (!isset($styles[$style])) {
121 throw new SyntaxError(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
124 //Return number formatter
125 return ($formatters[$locale][$style] = \NumberFormatter
::create($locale, $styles[$style]));