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
;
15 use Twig\Error\SyntaxError
;
16 use Twig\Extension\CoreExtension
;
19 * Manages intl conversions
25 public function currency(int|float $number, string $currency, ?string $locale = null) {
27 $formatter = $this->getNumberFormatter($locale, 'currency');
29 //Return formatted currency
30 return $formatter->formatCurrency($number, $currency);
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) {
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);
41 //Set date and time formatters
43 'none' => \IntlDateFormatter
::NONE
,
44 'short' => \IntlDateFormatter
::SHORT
,
45 'medium' => \IntlDateFormatter
::MEDIUM
,
46 'long' => \IntlDateFormatter
::LONG
,
47 'full' => \IntlDateFormatter
::FULL
,
51 $formatter = \IntlDateFormatter
::create(
53 $formatters[$dateFormat],
54 $formatters[$timeFormat],
55 \IntlTimeZone
::createTimeZone($date->getTimezone()->getName()),
56 'traditional' === $calendar ? \IntlDateFormatter
::TRADITIONAL
: \IntlDateFormatter
::GREGORIAN
,
60 //Return formatted date
61 return $formatter->format($date->getTimestamp());
65 * Compute eastern for selected year
67 * @param string $year The eastern year
69 * @return DateTime The eastern date
71 public function getEastern(string $year): \DateTime
{
75 //Check if already computed
76 if (isset($results[$year])) {
77 //Return computed eastern
78 return $results[$year];
81 $d = (19 * ($year %
19) +
24) %
30;
83 $e = (2 * ($year %
4) +
4 * ($year %
7) +
6 * $d +
5) %
7;
92 } elseif ($d == 29 && $e == 6) {
95 } elseif ($d == 28 && $e == 6) {
100 //Store eastern in data
101 return ($results[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
105 * Gets number formatter instance matching locale and style.
107 * @param ?string $locale Locale in which the number would be formatted
108 * @param string $style Style of the formatting
110 * @return NumberFormatter A NumberFormatter instance
112 protected function getNumberFormatter(?string $locale, string $style): \NumberFormatter
{
113 //Set static formatters
114 static $formatters = [];
117 $locale = null !== $locale ? $locale : \Locale
::getDefault();
119 //With existing formatter
120 if (isset($formatters[$locale][$style])) {
121 //Return the instance from previous call
122 return $formatters[$locale][$style];
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
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))));
141 //Return number formatter
142 return ($formatters[$locale][$style] = \NumberFormatter
::create($locale, $styles[$style]));
148 public function number(int|float $number, $style = 'decimal', $type = 'default', ?string $locale = null) {
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
159 $formatter = $this->getNumberFormatter($locale, $style);
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))));
166 //Return formatted number
167 return $formatter->format($number, $types[$type]);