]>
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 currency(int|float $number, string $currency, ?string $locale = null) { 
  26                 $formatter = $this->getNumberFormatter($locale, 'currency'); 
  28                 //Return formatted currency 
  29                 return $formatter->formatCurrency($number, $currency); 
  35         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) { 
  37                 $date = twig_date_converter($env, $date, $timezone); 
  39                 //Set date and time formatters 
  41                         'none' => \IntlDateFormatter
::NONE
, 
  42                         'short' => \IntlDateFormatter
::SHORT
, 
  43                         'medium' => \IntlDateFormatter
::MEDIUM
, 
  44                         'long' => \IntlDateFormatter
::LONG
, 
  45                         'full' => \IntlDateFormatter
::FULL
, 
  49                 $formatter = \IntlDateFormatter
::create( 
  51                         $formatters[$dateFormat], 
  52                         $formatters[$timeFormat], 
  53                         \IntlTimeZone
::createTimeZone($date->getTimezone()->getName()), 
  54                         'traditional' === $calendar ? \IntlDateFormatter
::TRADITIONAL 
: \IntlDateFormatter
::GREGORIAN
, 
  58                 //Return formatted date 
  59                 return $formatter->format($date->getTimestamp()); 
  63          * Compute eastern for selected year 
  65          * @param string $year The eastern year 
  67          * @return DateTime The eastern date 
  69         public function getEastern(string $year): \DateTime 
{ 
  73                 //Check if already computed 
  74                 if (isset($results[$year])) { 
  75                         //Return computed eastern 
  76                         return $results[$year]; 
  79                 $d = (19 * ($year % 
19) + 
24) % 
30; 
  81                 $e = (2 * ($year % 
4) + 
4 * ($year % 
7) + 
6 * $d + 
5) % 
7; 
  90                 } elseif ($d == 29 && $e == 6) { 
  93                 } elseif ($d == 28 && $e == 6) { 
  98                 //Store eastern in data 
  99                 return ($results[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day))); 
 103          * Gets number formatter instance matching locale and style. 
 105          * @param ?string $locale Locale in which the number would be formatted 
 106          * @param string $style Style of the formatting 
 108          * @return NumberFormatter A NumberFormatter instance 
 110         protected function getNumberFormatter(?string $locale, string $style): \NumberFormatter 
{ 
 111                 //Set static formatters 
 112                 static $formatters = []; 
 115                 $locale = null !== $locale ? $locale : \Locale
::getDefault(); 
 117                 //With existing formatter 
 118                 if (isset($formatters[$locale][$style])) { 
 119                         //Return the instance from previous call 
 120                         return $formatters[$locale][$style]; 
 125                         'decimal' => \NumberFormatter
::DECIMAL
, 
 126                         'currency' => \NumberFormatter
::CURRENCY
, 
 127                         'percent' => \NumberFormatter
::PERCENT
, 
 128                         'scientific' => \NumberFormatter
::SCIENTIFIC
, 
 129                         'spellout' => \NumberFormatter
::SPELLOUT
, 
 130                         'ordinal' => \NumberFormatter
::ORDINAL
, 
 131                         'duration' => \NumberFormatter
::DURATION
 
 135                 if (!isset($styles[$style])) { 
 136                         throw new SyntaxError(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues)))); 
 139                 //Return number formatter 
 140                 return ($formatters[$locale][$style] = \NumberFormatter
::create($locale, $styles[$style])); 
 146         public function number(int|float $number, $style = 'decimal', $type = 'default', ?string $locale = null) { 
 149                         'default' => \NumberFormatter
::TYPE_DEFAULT
, 
 150                         'int32' => \NumberFormatter
::TYPE_INT32
, 
 151                         'int64' => \NumberFormatter
::TYPE_INT64
, 
 152                         'double' => \NumberFormatter
::TYPE_DOUBLE
, 
 153                         'currency' => \NumberFormatter
::TYPE_CURRENCY
 
 157                 $formatter = $this->getNumberFormatter($locale, $style); 
 160                 if (!isset($types[$type])) { 
 161                         throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($types)))); 
 164                 //Return formatted number 
 165                 return $formatter->format($number, $types[$type]); 
 171          * @TODO: @XXX: add unit translation kB, MB, GiB, etc ? 
 173         public function size(int|float $number, $si = true, $style = 'decimal', $type = 'default', ?string $locale = null) { 
 176                         'default' => \NumberFormatter
::TYPE_DEFAULT
, 
 177                         'int32' => \NumberFormatter
::TYPE_INT32
, 
 178                         'int64' => \NumberFormatter
::TYPE_INT64
, 
 179                         'double' => \NumberFormatter
::TYPE_DOUBLE
, 
 180                         'currency' => \NumberFormatter
::TYPE_CURRENCY
 
 184                 $formatter = $this->getNumberFormatter($locale, $style); 
 187                 if (!isset($types[$type])) { 
 188                         throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($types)))); 
 192                 $unit = $si ? 1000 : 1024; 
 195                 $index = [ '', $si ? 'k' : 'K', 'M', 'G', 'T', 'P', 'E' ]; 
 198                 $exp = intval((log($number) / log($unit))); 
 201                 $number = round($number / pow($unit, $exp), 2); 
 203                 //Return formatted number 
 204                 return $formatter->format($number, $types[$type]).' '.$index[$exp].($si ? '' : 'i').'B';