Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/easycorp/easyadmin-bundle/src/Intl/IntlFormatter.php line 108

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Intl;
  3. use Twig\Error\RuntimeError;
  4. /**
  5.  * Copied from https://github.com/twigphp/intl-extra/blob/2.x/src/IntlExtension.php
  6.  * (c) Fabien Potencier - MIT License.
  7.  *
  8.  * @author Fabien Potencier <fabien@symfony.com>
  9.  */
  10. final class IntlFormatter
  11. {
  12.     private const DATE_FORMATS = [
  13.         'none' => \IntlDateFormatter::NONE,
  14.         'short' => \IntlDateFormatter::SHORT,
  15.         'medium' => \IntlDateFormatter::MEDIUM,
  16.         'long' => \IntlDateFormatter::LONG,
  17.         'full' => \IntlDateFormatter::FULL,
  18.     ];
  19.     private const NUMBER_TYPES = [
  20.         'default' => \NumberFormatter::TYPE_DEFAULT,
  21.         'int32' => \NumberFormatter::TYPE_INT32,
  22.         'int64' => \NumberFormatter::TYPE_INT64,
  23.         'double' => \NumberFormatter::TYPE_DOUBLE,
  24.         'currency' => \NumberFormatter::TYPE_CURRENCY,
  25.     ];
  26.     private const NUMBER_STYLES = [
  27.         'decimal' => \NumberFormatter::DECIMAL,
  28.         'currency' => \NumberFormatter::CURRENCY,
  29.         'percent' => \NumberFormatter::PERCENT,
  30.         'scientific' => \NumberFormatter::SCIENTIFIC,
  31.         'spellout' => \NumberFormatter::SPELLOUT,
  32.         'ordinal' => \NumberFormatter::ORDINAL,
  33.         'duration' => \NumberFormatter::DURATION,
  34.     ];
  35.     private const NUMBER_ATTRIBUTES = [
  36.         'grouping_used' => \NumberFormatter::GROUPING_USED,
  37.         'decimal_always_shown' => \NumberFormatter::DECIMAL_ALWAYS_SHOWN,
  38.         'max_integer_digit' => \NumberFormatter::MAX_INTEGER_DIGITS,
  39.         'min_integer_digit' => \NumberFormatter::MIN_INTEGER_DIGITS,
  40.         'integer_digit' => \NumberFormatter::INTEGER_DIGITS,
  41.         'max_fraction_digit' => \NumberFormatter::MAX_FRACTION_DIGITS,
  42.         'min_fraction_digit' => \NumberFormatter::MIN_FRACTION_DIGITS,
  43.         'fraction_digit' => \NumberFormatter::FRACTION_DIGITS,
  44.         'multiplier' => \NumberFormatter::MULTIPLIER,
  45.         'grouping_size' => \NumberFormatter::GROUPING_SIZE,
  46.         'rounding_mode' => \NumberFormatter::ROUNDING_MODE,
  47.         'rounding_increment' => \NumberFormatter::ROUNDING_INCREMENT,
  48.         'format_width' => \NumberFormatter::FORMAT_WIDTH,
  49.         'padding_position' => \NumberFormatter::PADDING_POSITION,
  50.         'secondary_grouping_size' => \NumberFormatter::SECONDARY_GROUPING_SIZE,
  51.         'significant_digits_used' => \NumberFormatter::SIGNIFICANT_DIGITS_USED,
  52.         'min_significant_digits_used' => \NumberFormatter::MIN_SIGNIFICANT_DIGITS,
  53.         'max_significant_digits_used' => \NumberFormatter::MAX_SIGNIFICANT_DIGITS,
  54.         'lenient_parse' => \NumberFormatter::LENIENT_PARSE,
  55.     ];
  56.     private const NUMBER_ROUNDING_ATTRIBUTES = [
  57.         'ceiling' => \NumberFormatter::ROUND_CEILING,
  58.         'floor' => \NumberFormatter::ROUND_FLOOR,
  59.         'down' => \NumberFormatter::ROUND_DOWN,
  60.         'up' => \NumberFormatter::ROUND_UP,
  61.         'halfeven' => \NumberFormatter::ROUND_HALFEVEN,
  62.         'halfdown' => \NumberFormatter::ROUND_HALFDOWN,
  63.         'halfup' => \NumberFormatter::ROUND_HALFUP,
  64.     ];
  65.     private const NUMBER_PADDING_ATTRIBUTES = [
  66.         'before_prefix' => \NumberFormatter::PAD_BEFORE_PREFIX,
  67.         'after_prefix' => \NumberFormatter::PAD_AFTER_PREFIX,
  68.         'before_suffix' => \NumberFormatter::PAD_BEFORE_SUFFIX,
  69.         'after_suffix' => \NumberFormatter::PAD_AFTER_SUFFIX,
  70.     ];
  71.     private array $dateFormatters = [];
  72.     private array $numberFormatters = [];
  73.     public function formatCurrency($amountstring $currency, array $attrs = [], string $locale null): string
  74.     {
  75.         $formatter $this->createNumberFormatter($locale'currency'$attrs);
  76.         /** @var string|false $formattedCurrency */
  77.         $formattedCurrency $formatter->formatCurrency($amount$currency);
  78.         if (false === $formattedCurrency) {
  79.             throw new RuntimeError('Unable to format the given number as a currency.');
  80.         }
  81.         return $formattedCurrency;
  82.     }
  83.     public function formatNumber($number, array $attrs = [], string $style 'decimal'string $type 'default'string $locale null): string
  84.     {
  85.         if (!isset(self::NUMBER_TYPES[$type])) {
  86.             throw new RuntimeError(sprintf('The type "%s" does not exist, known types are: "%s".'$typeimplode('", "'array_keys(self::NUMBER_TYPES))));
  87.         }
  88.         $formatter $this->createNumberFormatter($locale$style$attrs);
  89.         if (false === $ret $formatter->format($numberself::NUMBER_TYPES[$type])) {
  90.             throw new RuntimeError('Unable to format the given number.');
  91.         }
  92.         return $ret;
  93.     }
  94.     /**
  95.      * @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
  96.      */
  97.     public function formatDateTime(?\DateTimeInterface $date, ?string $dateFormat 'medium', ?string $timeFormat 'medium'string $pattern ''$timezone nullstring $calendar 'gregorian'string $locale null): ?string
  98.     {
  99.         if (null === $date $this->convertDate($date$timezone)) {
  100.             return null;
  101.         }
  102.         $formatter $this->createDateFormatter($locale$dateFormat$timeFormat$pattern$date->getTimezone(), $calendar);
  103.         $formattedDateTime $formatter->format($date);
  104.         return false !== $formattedDateTime $formattedDateTime null;
  105.     }
  106.     /**
  107.      * @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
  108.      */
  109.     public function formatDate(?\DateTimeInterface $date, ?string $dateFormat 'medium'string $pattern ''$timezone nullstring $calendar 'gregorian'string $locale null): ?string
  110.     {
  111.         return $this->formatDateTime($date$dateFormat'none'$pattern$timezone$calendar$locale);
  112.     }
  113.     /**
  114.      * @param \DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged
  115.      */
  116.     public function formatTime(?\DateTimeInterface $date, ?string $timeFormat 'medium'string $pattern ''$timezone nullstring $calendar 'gregorian'string $locale null): ?string
  117.     {
  118.         return $this->formatDateTime($date'none'$timeFormat$pattern$timezone$calendar$locale);
  119.     }
  120.     private function createDateFormatter(?string $locale, ?string $dateFormat, ?string $timeFormatstring $pattern ''\DateTimeZone $timezone nullstring $calendarName 'gregorian'): \IntlDateFormatter
  121.     {
  122.         if (null !== $dateFormat && !isset(self::DATE_FORMATS[$dateFormat])) {
  123.             throw new RuntimeError(sprintf('The date format "%s" does not exist, known formats are: "%s".'$dateFormatimplode('", "'array_keys(self::DATE_FORMATS))));
  124.         }
  125.         if (null !== $timeFormat && !isset(self::DATE_FORMATS[$timeFormat])) {
  126.             throw new RuntimeError(sprintf('The time format "%s" does not exist, known formats are: "%s".'$timeFormatimplode('", "'array_keys(self::DATE_FORMATS))));
  127.         }
  128.         if (null === $locale) {
  129.             $locale \Locale::getDefault();
  130.         }
  131.         $calendar 'gregorian' === $calendarName \IntlDateFormatter::GREGORIAN \IntlDateFormatter::TRADITIONAL;
  132.         $dateFormatValue self::DATE_FORMATS[$dateFormat] ?? self::DATE_FORMATS['full'];
  133.         $timeFormatValue self::DATE_FORMATS[$timeFormat] ?? self::DATE_FORMATS['full'];
  134.         $hash $locale.'|'.$dateFormatValue.'|'.$timeFormatValue.'|'.$timezone->getName().'|'.$calendar.'|'.$pattern;
  135.         if (!isset($this->dateFormatters[$hash])) {
  136.             $this->dateFormatters[$hash] = new \IntlDateFormatter($locale$dateFormatValue$timeFormatValue$timezone$calendar$pattern);
  137.         }
  138.         return $this->dateFormatters[$hash];
  139.     }
  140.     private function createNumberFormatter(?string $localestring $style, array $attrs = []): \NumberFormatter
  141.     {
  142.         if (!isset(self::NUMBER_STYLES[$style])) {
  143.             throw new RuntimeError(sprintf('The style "%s" does not exist, known styles are: "%s".'$styleimplode('", "'array_keys(self::NUMBER_STYLES))));
  144.         }
  145.         if (null === $locale) {
  146.             $locale \Locale::getDefault();
  147.         }
  148.         ksort($attrs);
  149.         $hash sprintf('%s|%s|%s'$locale$stylejson_encode($attrs\JSON_THROW_ON_ERROR));
  150.         if (!isset($this->numberFormatters[$hash])) {
  151.             $this->numberFormatters[$hash] = new \NumberFormatter($localeself::NUMBER_STYLES[$style]);
  152.         }
  153.         foreach ($attrs as $name => $value) {
  154.             if (!isset(self::NUMBER_ATTRIBUTES[$name])) {
  155.                 throw new RuntimeError(sprintf('The number formatter attribute "%s" does not exist, known attributes are: "%s".'$nameimplode('", "'array_keys(self::NUMBER_ATTRIBUTES))));
  156.             }
  157.             if ('rounding_mode' === $name) {
  158.                 if (!isset(self::NUMBER_ROUNDING_ATTRIBUTES[$value])) {
  159.                     throw new RuntimeError(sprintf('The number formatter rounding mode "%s" does not exist, known modes are: "%s".'$valueimplode('", "'array_keys(self::NUMBER_ROUNDING_ATTRIBUTES))));
  160.                 }
  161.                 $value self::NUMBER_ROUNDING_ATTRIBUTES[$value];
  162.             } elseif ('padding_position' === $name) {
  163.                 if (!isset(self::NUMBER_PADDING_ATTRIBUTES[$value])) {
  164.                     throw new RuntimeError(sprintf('The number formatter padding position "%s" does not exist, known positions are: "%s".'$valueimplode('", "'array_keys(self::NUMBER_PADDING_ATTRIBUTES))));
  165.                 }
  166.                 $value self::NUMBER_PADDING_ATTRIBUTES[$value];
  167.             }
  168.             $this->numberFormatters[$hash]->setAttribute(self::NUMBER_ATTRIBUTES[$name], $value);
  169.         }
  170.         return $this->numberFormatters[$hash];
  171.     }
  172.     private function convertDate(?\DateTimeInterface $date$timezone null): ?\DateTimeInterface
  173.     {
  174.         if (null === $date) {
  175.             return null;
  176.         }
  177.         if (null === $timezone) {
  178.             $timezone = new \DateTimeZone(date_default_timezone_get());
  179.         } elseif (!$timezone instanceof \DateTimeZone) {
  180.             $timezone = new \DateTimeZone($timezone);
  181.         }
  182.         if ($date instanceof \DateTimeImmutable) {
  183.             return $date->setTimezone($timezone);
  184.         }
  185.         $date = clone $date;
  186.         $date->setTimezone($timezone);
  187.         return $date;
  188.     }
  189. }