Skip to content

[Form] Support intl.use_exceptions/error_level in NumberToLocalizedStringTransformer #58449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,14 @@ public function reverseTransform($value)
: \NumberFormatter::TYPE_INT32;
}

$result = $formatter->parse($value, $type, $position);
try {
$result = @$formatter->parse($value, $type, $position);
} catch (\IntlException $e) {
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
}

if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
throw new TransformationFailedException($formatter->getErrorMessage(), $formatter->getErrorCode());
}

if ($result >= \PHP_INT_MAX || $result <= -\PHP_INT_MAX) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,15 @@ public function reverseTransform($value)
$type = \PHP_INT_SIZE === 8 ? \NumberFormatter::TYPE_INT64 : \NumberFormatter::TYPE_INT32;
}

// replace normal spaces so that the formatter can read them
$result = $formatter->parse(str_replace(' ', "\xc2\xa0", $value), $type, $position);
try {
// replace normal spaces so that the formatter can read them
$result = @$formatter->parse(str_replace(' ', "\xc2\xa0", $value), $type, $position);
} catch (\IntlException $e) {
throw new TransformationFailedException($e->getMessage(), 0, $e);
}

if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
throw new TransformationFailedException($formatter->getErrorMessage(), $formatter->getErrorCode());
}

if (self::FRACTIONAL == $this->type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function tearDown(): void

if (\extension_loaded('intl')) {
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
ini_set('intl.error_level', $this->initialTestCaseUseException);
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
}
}

Expand Down Expand Up @@ -341,12 +341,11 @@ public function testReverseTransformFiveDigitYearsWithTimestamp()
$transformer->reverseTransform('20107-03-21 12:34:56');
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
{
if (!\extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not loaded');
}

$errorLevel = ini_set('intl.error_level', \E_WARNING);

try {
Expand All @@ -358,12 +357,11 @@ public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptions()
{
if (!\extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not loaded');
}

$initialUseExceptions = ini_set('intl.use_exceptions', 1);

try {
Expand All @@ -375,12 +373,11 @@ public function testReverseTransformWrapsIntlErrorsWithExceptions()
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
{
if (!\extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not loaded');
}

$initialUseExceptions = ini_set('intl.use_exceptions', 1);
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,29 @@ class NumberToLocalizedStringTransformerTest extends TestCase
{
private $defaultLocale;

private $initialTestCaseUseException;
private $initialTestCaseErrorLevel;

protected function setUp(): void
{
// Normalize intl. configuration settings.
if (\extension_loaded('intl')) {
$this->initialTestCaseUseException = ini_set('intl.use_exceptions', 0);
$this->initialTestCaseErrorLevel = ini_set('intl.error_level', 0);
}

$this->defaultLocale = \Locale::getDefault();
\Locale::setDefault('en');
}

protected function tearDown(): void
{
\Locale::setDefault($this->defaultLocale);

if (\extension_loaded('intl')) {
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
}
}

public static function provideTransformations()
Expand Down Expand Up @@ -647,6 +661,56 @@ public function testReverseTransformENotation($output, $input)
$this->assertSame($output, $transformer->reverseTransform($input));
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
{
$errorLevel = ini_set('intl.error_level', \E_WARNING);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new NumberToLocalizedStringTransformer();
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.error_level', $errorLevel);
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptions()
{
$initialUseExceptions = ini_set('intl.use_exceptions', 1);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new NumberToLocalizedStringTransformer();
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.use_exceptions', $initialUseExceptions);
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
{
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new NumberToLocalizedStringTransformer();
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.use_exceptions', $initialUseExceptions);
ini_set('intl.error_level', $initialErrorLevel);
}
}

public static function eNotationProvider(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,29 @@ class PercentToLocalizedStringTransformerTest extends TestCase

private $defaultLocale;

private $initialTestCaseUseException;
private $initialTestCaseErrorLevel;

protected function setUp(): void
{
// Normalize intl. configuration settings.
if (\extension_loaded('intl')) {
$this->initialTestCaseUseException = ini_set('intl.use_exceptions', 0);
$this->initialTestCaseErrorLevel = ini_set('intl.error_level', 0);
}

$this->defaultLocale = \Locale::getDefault();
\Locale::setDefault('en');
}

protected function tearDown(): void
{
\Locale::setDefault($this->defaultLocale);

if (\extension_loaded('intl')) {
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
}
}

public function testTransform()
Expand Down Expand Up @@ -483,6 +497,56 @@ public function testReverseTransformForHtml5FormatWithScale()

$this->assertEquals(0.1234, $transformer->reverseTransform('12.34'));
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
{
$errorLevel = ini_set('intl.error_level', \E_WARNING);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.error_level', $errorLevel);
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptions()
{
$initialUseExceptions = ini_set('intl.use_exceptions', 1);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.use_exceptions', $initialUseExceptions);
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
{
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.use_exceptions', $initialUseExceptions);
ini_set('intl.error_level', $initialErrorLevel);
}
}
}

class PercentToLocalizedStringTransformerWithoutGrouping extends PercentToLocalizedStringTransformer
Expand Down