]>
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) { 
  26                 $date = twig_date_converter($env, $date, $timezone); 
  28                 //Set date and time formatters 
  30                         'none' => \IntlDateFormatter
::NONE
, 
  31                         'short' => \IntlDateFormatter
::SHORT
, 
  32                         'medium' => \IntlDateFormatter
::MEDIUM
, 
  33                         'long' => \IntlDateFormatter
::LONG
, 
  34                         'full' => \IntlDateFormatter
::FULL
, 
  38                 $formatter = \IntlDateFormatter
::create( 
  40                         $formatters[$dateFormat], 
  41                         $formatters[$timeFormat], 
  42                         \IntlTimeZone
::createTimeZone($date->getTimezone()->getName()), 
  43                         'traditional' === $calendar ? \IntlDateFormatter
::TRADITIONAL 
: \IntlDateFormatter
::GREGORIAN
, 
  47                 //Return formatted date 
  48                 return $formatter->format($date->getTimestamp()); 
  54         public function number(int|float $number, $style = 'decimal', $type = 'default', ?string $locale = null) { 
  57                         'default' => NumberFormatter
::TYPE_DEFAULT
, 
  58                         'int32' => NumberFormatter
::TYPE_INT32
, 
  59                         'int64' => NumberFormatter
::TYPE_INT64
, 
  60                         'double' => NumberFormatter
::TYPE_DOUBLE
, 
  61                         'currency' => NumberFormatter
::TYPE_CURRENCY
, 
  65                 $formatter = $this->getNumberFormatter($locale, $style); 
  68                 if (!isset($types[$type])) { 
  69                         throw new SyntaxError(sprintf('The type "%s" does not exist. Known types are: "%s"', $type, implode('", "', array_keys($types)))); 
  72                 //Return formatted number 
  73                 return $formatter->format($number, $types[$type]); 
  79         public function currency(int|float $number, string $currency, ?string $locale = null) { 
  81                 $formatter = $this->getNumberFormatter($locale, 'currency'); 
  83                 //Return formatted currency 
  84                 return $formatter->formatCurrency($number, $currency); 
  88          * Compute eastern for selected year 
  90          * @param string $year The eastern year 
  92          * @return DateTime The eastern date 
  94         public function getEastern(string $year): \DateTime 
{ 
  98                 //Check if already computed 
  99                 if (isset($results[$year])) { 
 100                         //Return computed eastern 
 101                         return $results[$year]; 
 104                 $d = (19 * ($year % 
19) + 
24) % 
30; 
 106                 $e = (2 * ($year % 
4) + 
4 * ($year % 
7) + 
6 * $d + 
5) % 
7; 
 115                 } elseif ($d == 29 && $e == 6) { 
 118                 } elseif ($d == 28 && $e == 6) { 
 123                 //Store eastern in data 
 124                 return ($results[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day))); 
 128          * Gets number formatter instance matching locale and style. 
 130          * @param ?string $locale Locale in which the number would be formatted 
 131          * @param string $style Style of the formatting 
 133          * @return NumberFormatter A NumberFormatter instance 
 135         protected function getNumberFormatter(?string $locale, string $style): \NumberFormatter 
{ 
 136                 //Set static formatters 
 137                 static $formatters = []; 
 140                 $locale = null !== $locale ? $locale : Locale
::getDefault(); 
 142                 //With existing formatter 
 143                 if (isset($formatters[$locale][$style])) { 
 144                         //Return the instance from previous call 
 145                         return $formatters[$locale][$style]; 
 150                         'decimal' => \NumberFormatter
::DECIMAL
, 
 151                         'currency' => \NumberFormatter
::CURRENCY
, 
 152                         'percent' => \NumberFormatter
::PERCENT
, 
 153                         'scientific' => \NumberFormatter
::SCIENTIFIC
, 
 154                         'spellout' => \NumberFormatter
::SPELLOUT
, 
 155                         'ordinal' => \NumberFormatter
::ORDINAL
, 
 156                         'duration' => \NumberFormatter
::DURATION
, 
 160                 if (!isset($styles[$style])) { 
 161                         throw new SyntaxError(sprintf('The style "%s" does not exist. Known styles are: "%s"', $style, implode('", "', array_keys($styleValues)))); 
 164                 //Return number formatter 
 165                 return ($formatters[$locale][$style] = \NumberFormatter
::create($locale, $styles[$style]));