]> Raphaël G. Git Repositories - packbundle/blob - Util/IntlUtil.php
Add captcha option
[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 * Compute eastern for selected year
89 *
90 * @param string $year The eastern year
91 *
92 * @return DateTime The eastern date
93 */
94 public function getEastern(string $year): \DateTime {
95 //Set static results
96 static $results = [];
97
98 //Check if already computed
99 if (isset($results[$year])) {
100 //Return computed eastern
101 return $results[$year];
102 }
103
104 $d = (19 * ($year % 19) + 24) % 30;
105
106 $e = (2 * ($year % 4) + 4 * ($year % 7) + 6 * $d + 5) % 7;
107
108 $day = 22 + $d + $e;
109
110 $month = 3;
111
112 if ($day > 31) {
113 $day = $d + $e - 9;
114 $month = 4;
115 } elseif ($d == 29 && $e == 6) {
116 $day = 10;
117 $month = 4;
118 } elseif ($d == 28 && $e == 6) {
119 $day = 18;
120 $month = 4;
121 }
122
123 //Store eastern in data
124 return ($results[$year] = new \DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
125 }
126
127 /**
128 * Gets number formatter instance matching locale and style.
129 *
130 * @param ?string $locale Locale in which the number would be formatted
131 * @param string $style Style of the formatting
132 *
133 * @return NumberFormatter A NumberFormatter instance
134 */
135 protected function getNumberFormatter(?string $locale, string $style): \NumberFormatter {
136 //Set static formatters
137 static $formatters = [];
138
139 //Set locale
140 $locale = null !== $locale ? $locale : Locale::getDefault();
141
142 //With existing formatter
143 if (isset($formatters[$locale][$style])) {
144 //Return the instance from previous call
145 return $formatters[$locale][$style];
146 }
147
148 //Set styles
149 static $styles = [
150 'decimal' => \NumberFormatter::DECIMAL,
151 'currency' => \NumberFormatter::CURRENCY,
152 'percent' => \NumberFormatter::PERCENT,
153 'scientific' => \NumberFormatter::SCIENTIFIC,
154 'spellout' => \NumberFormatter::SPELLOUT,
155 'ordinal' => \NumberFormatter::ORDINAL,
156 'duration' => \NumberFormatter::DURATION,
157 ];
158
159 //Without styles
160 if (!isset($styles[$style])) {
161 throw new SyntaxError(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
162 }
163
164 //Return number formatter
165 return ($formatters[$locale][$style] = \NumberFormatter::create($locale, $styles[$style]));
166 }
167 }