]>
Raphaël G. Git Repositories - packbundle/blob - Util/IntlUtil.php
   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
; 
  14 use Twig\Error\SyntaxError
; 
  18  * Manages intl conversions 
  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); 
  27                 //Set date and time formatters 
  29                         'none' => \IntlDateFormatter
::NONE
, 
  30                         'short' => \IntlDateFormatter
::SHORT
, 
  31                         'medium' => \IntlDateFormatter
::MEDIUM
, 
  32                         'long' => \IntlDateFormatter
::LONG
, 
  33                         'full' => \IntlDateFormatter
::FULL
, 
  36                 $formatter = \IntlDateFormatter
::create( 
  38                         $formatters[$dateFormat], 
  39                         $formatters[$timeFormat], 
  40                         \IntlTimeZone
::createTimeZone($date->getTimezone()->getName()), 
  41                         'traditional' === $calendar ? \IntlDateFormatter
::TRADITIONAL 
: \IntlDateFormatter
::GREGORIAN
, 
  45                 return $formatter->format($date->getTimestamp()); 
  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
, 
  60                 $formatter = $this->getNumberFormatter($locale, $style); 
  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)))); 
  66                 return $formatter->format($number, $typeValues[$type]); 
  72         public function currency(int|float $number, string $currency, ?string $locale = null) { 
  73                 $formatter = $this->getNumberFormatter($locale, 'currency'); 
  75                 return $formatter->formatCurrency($number, $currency); 
  79          * Gets number formatter instance matching locale and style. 
  81          * @param ?string $locale Locale in which the number would be formatted 
  82          * @param string $style Style of the formatting 
  84          * @return NumberFormatter A NumberFormatter instance 
  86         protected function getNumberFormatter(?string $locale, string $style): \NumberFormatter 
{ 
  87                 //Set static formatters 
  88                 static $formatters = []; 
  91                 $locale = null !== $locale ? $locale : Locale
::getDefault(); 
  93                 //With existing formatter 
  94                 if (isset($formatters[$locale][$style])) { 
  95                         //Return the instance from previous call 
  96                         return $formatters[$locale][$style]; 
 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
, 
 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)))); 
 115                 //Return number formatter 
 116                 return ($formatters[$locale][$style] = \NumberFormatter
::create($locale, $styles[$style]));