]> Raphaël G. Git Repositories - packbundle/blob - Util/IntlUtil.php
Readd getAlias member function
[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 * Manages intl conversions
19 */
20 class IntlUtil {
21 /**
22 * Format date
23 */
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) {
25 //Get converted date
26 $date = twig_date_converter($env, $date, $timezone);
27
28 //Set date and time formatters
29 $formatters = [
30 'none' => \IntlDateFormatter::NONE,
31 'short' => \IntlDateFormatter::SHORT,
32 'medium' => \IntlDateFormatter::MEDIUM,
33 'long' => \IntlDateFormatter::LONG,
34 'full' => \IntlDateFormatter::FULL,
35 ];
36
37 //Get formatter
38 $formatter = \IntlDateFormatter::create(
39 $locale,
40 $formatters[$dateFormat],
41 $formatters[$timeFormat],
42 \IntlTimeZone::createTimeZone($date->getTimezone()->getName()),
43 'traditional' === $calendar ? \IntlDateFormatter::TRADITIONAL : \IntlDateFormatter::GREGORIAN,
44 $pattern
45 );
46
47 //Return formatted date
48 return $formatter->format($date->getTimestamp());
49 }
50
51 /**
52 * Format number
53 */
54 public function number(int|float $number, $style = 'decimal', $type = 'default', ?string $locale = null) {
55 //Set types
56 static $types = [
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,
62 ];
63
64 //Get formatter
65 $formatter = $this->getNumberFormatter($locale, $style);
66
67 //Without type
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))));
70 }
71
72 //Return formatted number
73 return $formatter->format($number, $types[$type]);
74 }
75
76 /**
77 * Format currency
78 */
79 public function currency(int|float $number, string $currency, ?string $locale = null) {
80 //Get formatter
81 $formatter = $this->getNumberFormatter($locale, 'currency');
82
83 //Return formatted currency
84 return $formatter->formatCurrency($number, $currency);
85 }
86
87 /**
88 * Gets number formatter instance matching locale and style.
89 *
90 * @param ?string $locale Locale in which the number would be formatted
91 * @param string $style Style of the formatting
92 *
93 * @return NumberFormatter A NumberFormatter instance
94 */
95 protected function getNumberFormatter(?string $locale, string $style): \NumberFormatter {
96 //Set static formatters
97 static $formatters = [];
98
99 //Set locale
100 $locale = null !== $locale ? $locale : Locale::getDefault();
101
102 //With existing formatter
103 if (isset($formatters[$locale][$style])) {
104 //Return the instance from previous call
105 return $formatters[$locale][$style];
106 }
107
108 //Set styles
109 static $styles = [
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,
117 ];
118
119 //Without styles
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))));
122 }
123
124 //Return number formatter
125 return ($formatters[$locale][$style] = \NumberFormatter::create($locale, $styles[$style]));
126 }
127 }