]>
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
20 * @TODO Makes this class strict !!!
26 public function __construct() {
29 public function date(Environment
$env, $date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null, $timezone = null, $format = null, $calendar = 'gregorian') {
30 $date = twig_date_converter($env, $date, $timezone);
32 $formatValues = array(
33 'none' => \IntlDateFormatter
::NONE
,
34 'short' => \IntlDateFormatter
::SHORT
,
35 'medium' => \IntlDateFormatter
::MEDIUM
,
36 'long' => \IntlDateFormatter
::LONG
,
37 'full' => \IntlDateFormatter
::FULL
,
40 $formatter = \IntlDateFormatter
::create(
42 $formatValues[$dateFormat],
43 $formatValues[$timeFormat],
44 \IntlTimeZone
::createTimeZone($date->getTimezone()->getName()),
45 'gregorian' === $calendar ? \IntlDateFormatter
::GREGORIAN
: \IntlDateFormatter
::TRADITIONAL
,
49 return $formatter->format($date->getTimestamp());
52 public function number($number, $style = 'decimal', $type = 'default', $locale = null) {
53 static $typeValues = array(
54 'default' => NumberFormatter
::TYPE_DEFAULT
,
55 'int32' => NumberFormatter
::TYPE_INT32
,
56 'int64' => NumberFormatter
::TYPE_INT64
,
57 'double' => NumberFormatter
::TYPE_DOUBLE
,
58 'currency' => NumberFormatter
::TYPE_CURRENCY
,
61 $formatter = $this->getNumberFormatter($locale, $style);
63 if (!isset($typeValues[$type])) {
64 throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($typeValues))));
67 return $formatter->format($number, $typeValues[$type]);
70 public function currency($number, $currency = null, $locale = null) {
71 $formatter = $this->getNumberFormatter($locale, 'currency');
73 return $formatter->formatCurrency($number, $currency);
77 * Gets a number formatter instance according to given locale and formatter.
79 * @param string $locale Locale in which the number would be formatted
80 * @param int $style Style of the formatting
82 * @return NumberFormatter A NumberFormatter instance
84 protected function getNumberFormatter($locale, $style): \NumberFormatter
{
85 static $formatter, $currentStyle;
87 $locale = null !== $locale ? $locale : Locale
::getDefault();
89 if ($formatter && $formatter->getLocale() === $locale && $currentStyle === $style) {
90 // Return same instance of NumberFormatter if parameters are the same
91 // to those in previous call
95 static $styleValues = array(
96 'decimal' => \NumberFormatter
::DECIMAL
,
97 'currency' => \NumberFormatter
::CURRENCY
,
98 'percent' => \NumberFormatter
::PERCENT
,
99 'scientific' => \NumberFormatter
::SCIENTIFIC
,
100 'spellout' => \NumberFormatter
::SPELLOUT
,
101 'ordinal' => \NumberFormatter
::ORDINAL
,
102 'duration' => \NumberFormatter
::DURATION
,
105 if (!isset($styleValues[$style])) {
106 throw new SyntaxError(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
109 $currentStyle = $style;
111 $formatter = \NumberFormatter
::create($locale, $styleValues[$style]);