]> Raphaël G. Git Repositories - packbundle/blob - Util/IntlUtil.php
Remove config array for RAPSYSPACK_AGENT, RAPSYSPACK_REDIRECT, RAPSYSPACK_SCHEME...
[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 $date = twig_date_converter($env, $date, $timezone);
26
27 //Set date and time formatters
28 $formatters = [
29 'none' => \IntlDateFormatter::NONE,
30 'short' => \IntlDateFormatter::SHORT,
31 'medium' => \IntlDateFormatter::MEDIUM,
32 'long' => \IntlDateFormatter::LONG,
33 'full' => \IntlDateFormatter::FULL,
34 ];
35
36 $formatter = \IntlDateFormatter::create(
37 $locale,
38 $formatters[$dateFormat],
39 $formatters[$timeFormat],
40 \IntlTimeZone::createTimeZone($date->getTimezone()->getName()),
41 'traditional' === $calendar ? \IntlDateFormatter::TRADITIONAL : \IntlDateFormatter::GREGORIAN,
42 $pattern
43 );
44
45 return $formatter->format($date->getTimestamp());
46 }
47
48 /**
49 * Format number
50 */
51 public function number(int|float $number, $style = 'decimal', $type = 'default', ?string $locale = null) {
52 static $typeValues = array(
53 'default' => NumberFormatter::TYPE_DEFAULT,
54 'int32' => NumberFormatter::TYPE_INT32,
55 'int64' => NumberFormatter::TYPE_INT64,
56 'double' => NumberFormatter::TYPE_DOUBLE,
57 'currency' => NumberFormatter::TYPE_CURRENCY,
58 );
59
60 $formatter = $this->getNumberFormatter($locale, $style);
61
62 if (!isset($typeValues[$type])) {
63 throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($typeValues))));
64 }
65
66 return $formatter->format($number, $typeValues[$type]);
67 }
68
69 /**
70 * Format currency
71 */
72 public function currency(int|float $number, string $currency, ?string $locale = null) {
73 $formatter = $this->getNumberFormatter($locale, 'currency');
74
75 return $formatter->formatCurrency($number, $currency);
76 }
77
78 /**
79 * Gets number formatter instance matching locale and style.
80 *
81 * @param ?string $locale Locale in which the number would be formatted
82 * @param string $style Style of the formatting
83 *
84 * @return NumberFormatter A NumberFormatter instance
85 */
86 protected function getNumberFormatter(?string $locale, string $style): \NumberFormatter {
87 //Set static formatters
88 static $formatters = [];
89
90 //Set locale
91 $locale = null !== $locale ? $locale : Locale::getDefault();
92
93 //With existing formatter
94 if (isset($formatters[$locale][$style])) {
95 //Return the instance from previous call
96 return $formatters[$locale][$style];
97 }
98
99 //Set styles
100 static $styles = [
101 'decimal' => \NumberFormatter::DECIMAL,
102 'currency' => \NumberFormatter::CURRENCY,
103 'percent' => \NumberFormatter::PERCENT,
104 'scientific' => \NumberFormatter::SCIENTIFIC,
105 'spellout' => \NumberFormatter::SPELLOUT,
106 'ordinal' => \NumberFormatter::ORDINAL,
107 'duration' => \NumberFormatter::DURATION,
108 ];
109
110 //Without styles
111 if (!isset($styles[$style])) {
112 throw new SyntaxError(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues))));
113 }
114
115 //Return number formatter
116 return ($formatters[$locale][$style] = \NumberFormatter::create($locale, $styles[$style]));
117 }
118 }