Fix #18674: Added more user-friendly exception messages for yii\i18n\Formatter

This commit is contained in:
Bizley
2021-08-09 09:26:10 +02:00
committed by GitHub
parent 13f27e4d92
commit c8e4e0727a
3 changed files with 203 additions and 11 deletions

View File

@ -26,6 +26,7 @@ Yii Framework 2 Change Log
- Enh #18789: Added JSONP support in `yii\web\JsonParser::parse()` (WinterSilence)
- Bug #18274: Fix `yii\log\Logger` to calculate profile timings no matter the value of the flush interval (bizley)
- Bug #18807: Fix replacing source whitespaces and optimize code of `yii\helpers\BaseStringHelper::mb_ucwords()` (WinterSilence)
- Enh #18674: Added more user-friendly exception messages for `yii\i18n\Formatter` (bizley)
2.0.42.1 May 06, 2021

View File

@ -1732,7 +1732,7 @@ class Formatter extends Component
$oldThousandSeparator = $this->thousandSeparator;
$this->thousandSeparator = '';
if ($this->_intlLoaded && !isset($options[NumberFormatter::GROUPING_USED])) {
$options[NumberFormatter::GROUPING_USED] = false;
$options[NumberFormatter::GROUPING_USED] = 0;
}
// format the size value
$params = [
@ -1793,19 +1793,19 @@ class Formatter extends Component
$formatter = new NumberFormatter($this->locale, $style);
// set text attributes
foreach ($this->numberFormatterTextOptions as $name => $attribute) {
$formatter->setTextAttribute($name, $attribute);
foreach ($this->numberFormatterTextOptions as $attribute => $value) {
$this->setFormatterTextAttribute($formatter, $attribute, $value, 'numberFormatterTextOptions', 'numberFormatterOptions');
}
foreach ($textOptions as $name => $attribute) {
$formatter->setTextAttribute($name, $attribute);
foreach ($textOptions as $attribute => $value) {
$this->setFormatterTextAttribute($formatter, $attribute, $value, '$textOptions', '$options');
}
// set attributes
foreach ($this->numberFormatterOptions as $name => $value) {
$formatter->setAttribute($name, $value);
foreach ($this->numberFormatterOptions as $attribute => $value) {
$this->setFormatterIntAttribute($formatter, $attribute, $value, 'numberFormatterOptions', 'numberFormatterTextOptions');
}
foreach ($options as $name => $value) {
$formatter->setAttribute($name, $value);
foreach ($options as $attribute => $value) {
$this->setFormatterIntAttribute($formatter, $attribute, $value, '$options', '$textOptions');
}
if ($decimals !== null) {
$formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals);
@ -1823,13 +1823,91 @@ class Formatter extends Component
$formatter->setSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL, $this->thousandSeparator);
$formatter->setSymbol(NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL, $this->thousandSeparator);
}
foreach ($this->numberFormatterSymbols as $name => $symbol) {
$formatter->setSymbol($name, $symbol);
foreach ($this->numberFormatterSymbols as $symbol => $value) {
$this->setFormatterSymbol($formatter, $symbol, $value, 'numberFormatterSymbols');
}
return $formatter;
}
/**
* @param NumberFormatter $formatter
* @param mixed $attribute
* @param mixed $value
* @param string $source
* @param string $alternative
*/
private function setFormatterTextAttribute($formatter, $attribute, $value, $source, $alternative)
{
if (!is_int($attribute)) {
throw new InvalidArgumentException(
"The $source array keys must be integers recognizable by NumberFormatter::setTextAttribute(). \""
. gettype($attribute) . '" provided instead.'
);
}
if (!is_string($value)) {
if (is_int($value)) {
throw new InvalidArgumentException(
"The $source array values must be strings. Did you mean to use $alternative?"
);
}
throw new InvalidArgumentException(
"The $source array values must be strings. \"" . gettype($value) . '" provided instead.'
);
}
$formatter->setTextAttribute($attribute, $value);
}
/**
* @param NumberFormatter $formatter
* @param mixed $symbol
* @param mixed $value
* @param string $source
*/
private function setFormatterSymbol($formatter, $symbol, $value, $source)
{
if (!is_int($symbol)) {
throw new InvalidArgumentException(
"The $source array keys must be integers recognizable by NumberFormatter::setSymbol(). \""
. gettype($symbol) . '" provided instead.'
);
}
if (!is_string($value)) {
throw new InvalidArgumentException(
"The $source array values must be strings. \"" . gettype($value) . '" provided instead.'
);
}
$formatter->setSymbol($symbol, $value);
}
/**
* @param NumberFormatter $formatter
* @param mixed $attribute
* @param mixed $value
* @param string $source
* @param string $alternative
*/
private function setFormatterIntAttribute($formatter, $attribute, $value, $source, $alternative)
{
if (!is_int($attribute)) {
throw new InvalidArgumentException(
"The $source array keys must be integers recognizable by NumberFormatter::setAttribute(). \""
. gettype($attribute) . '" provided instead.'
);
}
if (!is_int($value)) {
if (is_string($value)) {
throw new InvalidArgumentException(
"The $source array values must be integers. Did you mean to use $alternative?"
);
}
throw new InvalidArgumentException(
"The $source array values must be integers. \"" . gettype($value) . '" provided instead.'
);
}
$formatter->setAttribute($attribute, $value);
}
/**
* Checks if string representations of given value and its normalized version are different.
* @param string|float|int $value