Skip to content

Commit

Permalink
Update NumberHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jun 6, 2020
1 parent 721434e commit f1fa92e
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 32 deletions.
159 changes: 156 additions & 3 deletions src/Templating/Helper/NumberHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ public function __construct($charset, LocaleDetectorInterface $localeDetector, a
public function formatPercent($number, array $attributes = [], array $textAttributes = [], $locale = null)
{
if ($this->intlExtension) {
return $this->fixCharset($this->intlExtension->formatNumberStyle($locale, $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale()));
$attributes = self::processLegacyAttributes($attributes);

return $this->fixCharset($this->intlExtension->formatNumberStyle('percent', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale()));
}

// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 6 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception since version 3.x.',
.' and will throw an exception in version 3.x.',
IntlExtension::class,
__CLASS__
));
Expand All @@ -107,6 +111,21 @@ public function formatPercent($number, array $attributes = [], array $textAttrib
*/
public function formatDuration($number, array $attributes = [], array $textAttributes = [], $locale = null)
{
if ($this->intlExtension) {
$attributes = self::processLegacyAttributes($attributes);

return $this->fixCharset($this->intlExtension->formatNumberStyle('duration', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale()));
}

// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 6 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception in version 3.x.',
IntlExtension::class,
__CLASS__
));

$methodArgs = array_pad(\func_get_args(), 5, null);

[$locale, $symbols] = $this->normalizeMethodSignature($methodArgs[3], $methodArgs[4]);
Expand All @@ -127,6 +146,21 @@ public function formatDuration($number, array $attributes = [], array $textAttri
*/
public function formatDecimal($number, array $attributes = [], array $textAttributes = [], $locale = null)
{
if ($this->intlExtension) {
$attributes = self::processLegacyAttributes($attributes);

return $this->fixCharset($this->intlExtension->formatNumberStyle('decimal', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale()));
}

// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 6 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception in version 3.x.',
IntlExtension::class,
__CLASS__
));

$methodArgs = array_pad(\func_get_args(), 5, null);

[$locale, $symbols] = $this->normalizeMethodSignature($methodArgs[3], $methodArgs[4]);
Expand All @@ -147,6 +181,21 @@ public function formatDecimal($number, array $attributes = [], array $textAttrib
*/
public function formatSpellout($number, array $attributes = [], array $textAttributes = [], $locale = null)
{
if ($this->intlExtension) {
$attributes = self::processLegacyAttributes($attributes);

return $this->fixCharset($this->intlExtension->formatNumberStyle('spellout', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale()));
}

// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 6 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception in version 3.x.',
IntlExtension::class,
__CLASS__
));

$methodArgs = array_pad(\func_get_args(), 5, null);

[$locale, $symbols] = $this->normalizeMethodSignature($methodArgs[3], $methodArgs[4]);
Expand All @@ -169,12 +218,16 @@ public function formatSpellout($number, array $attributes = [], array $textAttri
public function formatCurrency($number, $currency, array $attributes = [], array $textAttributes = [], $locale = null)
{
if ($this->intlExtension) {
$attributes = self::processLegacyAttributes($attributes);

return $this->fixCharset($this->intlExtension->formatCurrency($number, $currency, $attributes, $locale ?: $this->localeDetector->getLocale()));
}

// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 6 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception since version 3.x.',
.' and will throw an exception in version 3.x.',
IntlExtension::class,
__CLASS__
));
Expand Down Expand Up @@ -206,6 +259,21 @@ public function formatCurrency($number, $currency, array $attributes = [], array
*/
public function formatScientific($number, array $attributes = [], array $textAttributes = [], $locale = null)
{
if ($this->intlExtension) {
$attributes = self::processLegacyAttributes($attributes);

return $this->fixCharset($this->intlExtension->formatNumberStyle('scientific', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale()));
}

// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 6 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception in version 3.x.',
IntlExtension::class,
__CLASS__
));

$methodArgs = array_pad(\func_get_args(), 5, null);

[$locale, $symbols] = $this->normalizeMethodSignature($methodArgs[3], $methodArgs[4]);
Expand All @@ -226,6 +294,21 @@ public function formatScientific($number, array $attributes = [], array $textAtt
*/
public function formatOrdinal($number, array $attributes = [], array $textAttributes = [], $locale = null)
{
if ($this->intlExtension) {
$attributes = self::processLegacyAttributes($attributes);

return $this->fixCharset($this->intlExtension->formatNumberStyle('ordinal', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale()));
}

// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 6 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception in version 3.x.',
IntlExtension::class,
__CLASS__
));

$methodArgs = array_pad(\func_get_args(), 5, null);

[$locale, $symbols] = $this->normalizeMethodSignature($methodArgs[3], $methodArgs[4]);
Expand All @@ -247,6 +330,21 @@ public function formatOrdinal($number, array $attributes = [], array $textAttrib
*/
public function format($number, $style, array $attributes = [], array $textAttributes = [], $locale = null)
{
if ($this->intlExtension) {
$attributes = self::processLegacyAttributes($attributes);

return $this->fixCharset($this->intlExtension->formatNumber($number, $attributes, $style, 'default', $locale ?: $this->localeDetector->getLocale()));
}

// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 6 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception in version 3.x.',
IntlExtension::class,
__CLASS__
));

$methodArgs = array_pad(\func_get_args(), 6, null);

[$locale, $symbols] = $this->normalizeMethodSignature($methodArgs[4], $methodArgs[5]);
Expand All @@ -257,10 +355,14 @@ public function format($number, $style, array $attributes = [], array $textAttri
}

/**
* NEXT_MAJOR: Remove this method.
*
* Normalizes the given arguments according to the new function signature.
* It asserts if neither the new nor old signature matches. This function
* is public just to prevent code duplication inside the Twig Extension.
*
* @deprecated since sonata-project/intl-bundle 2.x
*
* @param mixed $symbols The symbols used by the formatter
* @param mixed $locale The locale
*
Expand All @@ -272,6 +374,12 @@ public function format($number, $style, array $attributes = [], array $textAttri
*/
public function normalizeMethodSignature($symbols, $locale)
{
@trigger_error(sprintf(
'Method "%s()" is deprecated since sonata-project/intl-bundle 2.x.'
.' and will be removed in version 3.x.',
__METHOD__
));

$oldSignature = (null === $symbols || \is_string($symbols)) && null === $locale;
$newSignature = \is_array($symbols) && (\is_string($locale) || null === $locale);

Expand All @@ -298,9 +406,13 @@ public function getName()
}

/**
* NEXT_MAJOR: Remove this method.
*
* Gets an instance of \NumberFormatter set with the given attributes and
* style.
*
* @deprecated since sonata-project/intl-bundle 2.x
*
* @param string $culture The culture used by \NumberFormatter
* @param string $style The style used by \NumberFormatter
* @param array $attributes The attributes used by \NumberFormatter
Expand All @@ -311,6 +423,12 @@ public function getName()
*/
protected function getFormatter($culture, $style, $attributes = [], $textAttributes = [], $symbols = [])
{
@trigger_error(sprintf(
'Method "%s()" is deprecated since sonata-project/intl-bundle 2.x.'
.' and will be removed in version 3.x.',
__METHOD__
));

$attributes = $this->parseAttributes(array_merge($this->attributes, $attributes));
$textAttributes = $this->parseAttributes(array_merge($this->textAttributes, $textAttributes));
$symbols = $this->parseAttributes(array_merge($this->symbols, $symbols));
Expand All @@ -337,8 +455,12 @@ protected function getFormatter($culture, $style, $attributes = [], $textAttribu
}

/**
* NEXT_MAJOR: Remove this method.
*
* Converts keys of attributes array to values of \NumberFormatter constants.
*
* @deprecated since sonata-project/intl-bundle 2.x
*
* @param array $attributes The list of attributes
*
* @throws \InvalidArgumentException If any attribute does not match any constant
Expand All @@ -347,6 +469,12 @@ protected function getFormatter($culture, $style, $attributes = [], $textAttribu
*/
protected function parseAttributes(array $attributes)
{
@trigger_error(sprintf(
'Method "%s()" is deprecated since sonata-project/intl-bundle 2.x.'
.' and will be removed in version 3.x.',
__METHOD__
));

$result = [];

foreach ($attributes as $attribute => $value) {
Expand All @@ -357,8 +485,12 @@ protected function parseAttributes(array $attributes)
}

/**
* NEXT_MAJOR: Remove this method.
*
* Parse the given value trying to get a match with a \NumberFormatter constant.
*
* @deprecated since sonata-project/intl-bundle 2.x
*
* @param string $attribute The constant's name
*
* @throws \InvalidArgumentException If the value does not match any constant
Expand All @@ -367,6 +499,12 @@ protected function parseAttributes(array $attributes)
*/
protected function parseConstantValue($attribute)
{
@trigger_error(sprintf(
'Method "%s()" is deprecated since sonata-project/intl-bundle 2.x.'
.' and will be removed in version 3.x.',
__METHOD__
));

$attribute = strtoupper($attribute);
$constantName = 'NumberFormatter::'.$attribute;

Expand All @@ -376,4 +514,19 @@ protected function parseConstantValue($attribute)

return \constant($constantName);
}

/**
* NEXT_MAJOR: Remove this method.
*
* Replaces legacy attribute names with its new variants.
*/
private static function processLegacyAttributes(array $attributes): array
{
if (isset($attributes['fraction_digits'])) {
$curatedAttributes['fraction_digit'] = $attributes['fraction_digits'];
unset($attributes['fraction_digits']);
}

return $attributes;
}
}
Loading

0 comments on commit f1fa92e

Please sign in to comment.