8000 bug #36149 [Form] Support customized intl php.ini settings (jorrit) · symfony/symfony@5b5b61f · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b5b61f

Browse files
bug #36149 [Form] Support customized intl php.ini settings (jorrit)
This PR was merged into the 3.4 branch. Discussion ---------- [Form] Support customized intl php.ini settings | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | no | License | MIT | Doc PR | no `IntlDateParser->parse()` behaves differently when `intl.error_level` and/or `intl.use_exceptions` are not 0. This change makes sure `\IntlException` is caught when `intl.use_exceptions` is 1 and warnings thrown when `intl.error_level` is not 0 are ignored. Commits ------- 61025d1 [Form] Support customized intl php.ini settings
2 parents 0e2d5e9 + 61025d1 commit 5b5b61f

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,16 @@ public function reverseTransform($value)
117117
// date-only patterns require parsing to be done in UTC, as midnight might not exist in the local timezone due
118118
// to DST changes
119119
$dateOnly = $this->isPatternDateOnly();
120+
$dateFormatter = $this->getIntlDateFormatter($dateOnly);
120121

121-
$timestamp = $this->getIntlDateFormatter($dateOnly)->parse($value);
122+
try {
123+
$timestamp = @$dateFormatter->parse($value);
124+
} catch (\IntlException $e) {
125+
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
126+
}
122127

123128
if (0 != intl_get_error_code()) {
124-
throw new TransformationFailedException(intl_get_error_message());
129+
throw new TransformationFailedException(intl_get_error_message(), intl_get_error_code());
125130
} elseif ($timestamp > 253402214400) {
126131
// This timestamp represents UTC midnight of 9999-12-31 to prevent 5+ digit years
127132
throw new TransformationFailedException('Years beyond 9999 are not supported.');

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ protected function setUp()
2727
{
2828
parent::setUp();
2929

30+
// Normalize intl. configuration settings.
31+
if (\extension_loaded('intl')) {
32+
$this->iniSet('intl.use_exceptions', 0);
33+
$this->iniSet('intl.error_level', 0);
34+
}
35+
3036
// Since we test against "de_AT", we need the full implementation
3137
IntlTestHelper::requireFullIntl($this, '57.1');
3238

@@ -334,4 +340,44 @@ public function testReverseTransformFiveDigitYearsWithTimestamp()
334340
$transformer = new DateTimeToLocalizedStringTransformer('UTC', 'UTC', null, null, \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd HH:mm:ss');
335341
$transformer->reverseTransform('20107-03-21 12:34:56');
336342
}
343+
344+
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
345+
{
346+
if (!\extension_loaded('intl')) {
347+
$this->markTestSkipped('intl extension is not loaded');
348+
}
349+
350+
$this->iniSet('intl.error_level', E_WARNING);
351+
352+
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
353+
$transformer = new DateTimeToLocalizedStringTransformer();
354+
$transformer->reverseTransform('12345');
355+
}
356+
357+
public function testReverseTransformWrapsIntlErrorsWithExceptions()
358+
{
359+
if (!\extension_loaded('intl')) {
360+
$this->markTestSkipped('intl extension is not loaded');
361+
}
362+
363+
$this->iniSet('intl.use_exceptions', 1);
364+
365+
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
366+
$transformer = new DateTimeToLocalizedStringTransformer();
367+
$transformer->reverseTransform('12345');
368+
}
369+
370+
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
371+
{
372+
if (!\extension_loaded('intl')) {
373+
$this->markTestSkipped('intl extension is not loaded');
374+
}
375+
376+
$this->iniSet('intl.use_exceptions', 1);
377+
$this->iniSet('intl.error_level', E_WARNING);
378+
379+
$this->expectException('Symfony\Component\Form\Exception\TransformationFailedException');
380+
$transformer = new DateTimeToLocalizedStringTransformer();
381+
$transformer->reverseTransform('12345');
382+
}
337383
}

0 commit comments

Comments
 (0)
0