Throw when list formatting fails

This commit is contained in:
Fabien Potencier
2026-08-27 13:09:34 +02:00
parent 9a8a76c86d
commit 87b930ae67
2 changed files with 25 additions and 1 deletions
+6 -1
View File
@@ -451,7 +451,12 @@ final class IntlExtension extends AbstractExtension
throw new RuntimeError('The "format_list" filter requires the "IntlListFormatter" class, which is available since PHP 8.5.');
}
return $this->createListFormatter($locale, $type, $width)->format($strings);
$formatter = $this->createListFormatter($locale, $type, $width);
if (false === $ret = $formatter->format($strings)) {
throw new RuntimeError(\sprintf('Unable to format the given list: %s', $formatter->getErrorMessage()));
}
return $ret;
}
private function createDateFormatter(?string $locale, ?string $dateFormat, ?string $timeFormat, string $pattern, ?\DateTimeZone $timezone, ?string $calendar): \IntlDateFormatter
@@ -13,6 +13,7 @@ namespace Twig\Extra\Intl\Tests;
use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Error\RuntimeError;
use Twig\Extension\CoreExtension;
use Twig\Extra\Intl\IntlExtension;
use Twig\Loader\ArrayLoader;
@@ -223,4 +224,22 @@ class IntlExtensionTest extends TestCase
$this->assertLessThanOrEqual(100, \count($cache));
$this->assertGreaterThan(1, \count($cache));
}
public function testFormatListThrowsOnFailure(): void
{
if (!class_exists('IntlListFormatter')) {
$this->markTestSkipped('IntlListFormatter is not available.');
}
$strings = ['Alice', "\xB1\x31"];
$formatter = new \IntlListFormatter('en', \IntlListFormatter::TYPE_AND, \IntlListFormatter::WIDTH_WIDE);
if (false !== $formatter->format($strings)) {
$this->markTestSkipped('IntlListFormatter accepts the malformed UTF-8 input.');
}
$this->expectException(RuntimeError::class);
$this->expectExceptionMessage('Unable to format the given list: '.$formatter->getErrorMessage());
(new IntlExtension())->formatList($strings, locale: 'en');
}
}