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