]> Raphaël G. Git Repositories - packbundle/blob - Util/IntlUtil.php
Add twig intl date, number and currency filters
[packbundle] / Util / IntlUtil.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Util;
13
14 use Twig\Error\SyntaxError;
15 use Twig\Environment;
16
17 /**
18 * Helps manage intl conversions
19 */
20 class IntlUtil {
21 /**
22 * Construct intl util
23 */
24 public function __construct() {
25 }
26
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);
29
30 $formatValues = array(
31 'none' => IntlDateFormatter::NONE,
32 'short' => IntlDateFormatter::SHORT,
33 'medium' => IntlDateFormatter::MEDIUM,
34 'long' => IntlDateFormatter::LONG,
35 'full' => IntlDateFormatter::FULL,
36 );
37
38 $formatter = IntlDateFormatter::create(
39 $locale,
40 $formatValues[$dateFormat],
41 $formatValues[$timeFormat],
42 IntlTimeZone::createTimeZone($date->getTimezone()->getName()),
43 'gregorian' === $calendar ? IntlDateFormatter::GREGORIAN : IntlDateFormatter::TRADITIONAL,
44 $format
45 );
46
47 return $formatter->format($date->getTimestamp());
48 }
49
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,
57 );
58
59 $formatter = $this->getNumberFormatter($locale, $style);
60
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))));
63 }
64
65 return $formatter->format($number, $typeValues[$type]);
66 }
67
68 public function currency($number, $currency = null, $locale = null) {
69 $formatter = $this->getNumberFormatter($locale, 'currency');
70
71 return $formatter->formatCurrency($number, $currency);
72 }
73
74 /**
75 * Gets a number formatter instance according to given locale and formatter.
76 *
77 * @param string $locale Locale in which the number would be formatted
78 * @param int $style Style of the formatting
79 *
80 * @return NumberFormatter A NumberFormatter instance
81 */
82 protected function getNumberFormatter($locale, $style): NumberFormatter {
83 static $formatter, $currentStyle;
84
85 $locale = null !== $locale ? $locale : Locale::getDefault();
86
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
90 return $formatter;
91 }
92
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,
101 );
102
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))));
105 }
106
107 $currentStyle = $style;
108
109 $formatter = NumberFormatter::create($locale, $styleValues[$style]);
110
111 return $formatter;
112 }
113 }