]> Raphaël G. Git Repositories - packbundle/blob - Util/IntlUtil.php
Version 0.6.4
[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\Environment;
15 use Twig\Error\SyntaxError;
16 use Twig\Extension\CoreExtension;
17
18 /**
19 * Manages intl conversions
20 */
21 class IntlUtil {
22 /**
23 * Format currency
24 */
25 public function currency(int|float $number, string $currency, ?string $locale = null) {
26 //Get formatter
27 $formatter = $this->getNumberFormatter($locale, 'currency');
28
29 //Return formatted currency
30 return $formatter->formatCurrency($number, $currency);
31 }
32
33 /**
34 * Format date
35 */
36 public function date(Environment $env, \DateTimeInterface $date, string $dateFormat = 'medium', string $timeFormat = 'medium', ?string $locale = null, \IntlTimeZone|\DateTimeZone|string|null $timezone = null, ?string $calendar = null, ?string $pattern = null) {
37 //Get converted date
38 //XXX: was twig_date_converter($env, $date, $timezone) before it went deprecated in twig 3.9
39 $date = $env->getExtension(CoreExtension::class)->convertDate($date, $timezone);
40
41 //Set date and time formatters
42 $formatters = [
43 'none' => \IntlDateFormatter::NONE,
44 'short' => \IntlDateFormatter::SHORT,
45 'medium' => \IntlDateFormatter::MEDIUM,
46 'long' => \IntlDateFormatter::LONG,
47 'full' => \IntlDateFormatter::FULL,
48 ];
49
50 //Get formatter
51 $formatter = \IntlDateFormatter::create(
52 $locale,
53 $formatters[$dateFormat],
54 $formatters[$timeFormat],
55 \IntlTimeZone::createTimeZone($date->getTimezone()->getName()),
56 'traditional' === $calendar ? \IntlDateFormatter::TRADITIONAL : \IntlDateFormatter::GREGORIAN,
57 $pattern
58 );
59
60 //Return formatted date
61 return $formatter->format($date->getTimestamp());
62 }
63
64 /**
65 * Compute eastern for selected year
66 *
67 * @param string $year The eastern year
68 *
69 * @return DateTime The eastern date
70 */
71 public function getEastern(string $year): \DateTime {
72 //Set static results
73 static $results = [];
74
75 //Check if already computed
76 if (isset($results[$year])) {
77 //Return computed eastern
78 return $results[$year];
79 }
80
81 $d = (19 * ($year % 19) + 24) % 30;
82
83 $e = (2 * ($year % 4) + 4 * ($year % 7) + 6 * $d + 5) % 7;
84
85 $day = 22 + $d + $e;
86
87 $month = 3;
88
89 if ($day > 31) {
90 $day = $d + $e - 9;
91 $month = 4;
92 } elseif ($d == 29 && $e == 6) {
93 $day = 10;
94 $month = 4;
95 } elseif ($d == 28 && $e == 6) {
96 $day = 18;
97 $month = 4;
98 }
99
100 //Store eastern in data
101 return ($results[$year] = new \DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
102 }
103
104 /**
105 * Gets number formatter instance matching locale and style.
106 *
107 * @param ?string $locale Locale in which the number would be formatted
108 * @param string $style Style of the formatting
109 *
110 * @return NumberFormatter A NumberFormatter instance
111 */
112 protected function getNumberFormatter(?string $locale, string $style): \NumberFormatter {
113 //Set static formatters
114 static $formatters = [];
115
116 //Set locale
117 $locale = null !== $locale ? $locale : \Locale::getDefault();
118
119 //With existing formatter
120 if (isset($formatters[$locale][$style])) {
121 //Return the instance from previous call
122 return $formatters[$locale][$style];
123 }
124
125 //Set styles
126 static $styles = [
127 'decimal' => \NumberFormatter::DECIMAL,
128 #'currency' => \NumberFormatter::CURRENCY,
129 'percent' => \NumberFormatter::PERCENT,
130 'scientific' => \NumberFormatter::SCIENTIFIC,
131 'spellout' => \NumberFormatter::SPELLOUT,
132 'ordinal' => \NumberFormatter::ORDINAL,
133 'duration' => \NumberFormatter::DURATION
134 ];
135
136 //Without styles
137 if (!isset($styles[$style])) {
138 throw new SyntaxError(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
139 }
140
141 //Return number formatter
142 return ($formatters[$locale][$style] = \NumberFormatter::create($locale, $styles[$style]));
143 }
144
145 /**
146 * Format number
147 */
148 public function number(int|float $number, $style = 'decimal', $type = 'default', ?string $locale = null) {
149 //Set types
150 static $types = [
151 'default' => \NumberFormatter::TYPE_DEFAULT,
152 'int32' => \NumberFormatter::TYPE_INT32,
153 'int64' => \NumberFormatter::TYPE_INT64,
154 'double' => \NumberFormatter::TYPE_DOUBLE,
155 #'currency' => \NumberFormatter::TYPE_CURRENCY
156 ];
157
158 //Get formatter
159 $formatter = $this->getNumberFormatter($locale, $style);
160
161 //Without type
162 if (!isset($types[$type])) {
163 throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($types))));
164 }
165
166 //Return formatted number
167 return $formatter->format($number, $types[$type]);
168 }
169 }