Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Add a way to mark a `Twig\Markup` instance as safe for some escaping strategies only
* Make `Environment::getExtension()` return the runtime of an extension class registered with `AttributeExtension`
* Make `Environment::getRuntime()` fall back to the registered extension of the same class
* Speed up the number formatting filters of `twig/intl-extra`

# 3.30.0 (2026-09-25)

Expand Down
80 changes: 53 additions & 27 deletions extra/intl-extra/IntlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ private static function availableListWidths(): array
private array $collators = [];
private $dateFormatterPrototype;
private $numberFormatterPrototype;
/** @var array{array<string, int|float>, array<string, string>, array<string, string>, string}|null */
private ?array $numberFormatterPrototypeState = null;
/**
* @var array<string, string>
*/
Expand Down Expand Up @@ -612,40 +614,33 @@ private function createNumberFormatter(?string $locale, string $style, array $at

// textAttrs and symbols can only be set on the prototype as there is probably no
// use case for setting it on each call.
$textAttrs = [];
$symbols = [];
if ($this->numberFormatterPrototype) {
foreach (self::NUMBER_ATTRIBUTES as $name => $const) {
if (!isset($attrs[$name])) {
$value = $this->numberFormatterPrototype->getAttribute($const);
if ('rounding_mode' === $name) {
$value = array_flip(self::NUMBER_ROUNDING_ATTRIBUTES)[$value];
} elseif ('padding_position' === $name) {
$value = array_flip(self::NUMBER_PADDING_ATTRIBUTES)[$value];
}
$attrs[$name] = $value;
}
}
[$prototypeAttrs, $textAttrs, $symbols, $prototypeHash] = $this->numberFormatterPrototypeState ??= $this->readNumberFormatterPrototype();

foreach (self::NUMBER_TEXT_ATTRIBUTES as $name => $const) {
$textAttrs[$name] = $this->numberFormatterPrototype->getTextAttribute($const);
foreach ($prototypeAttrs as $name => $value) {
if (isset($attrs[$name])) {
continue;
}

foreach (self::NUMBER_SYMBOLS as $name => $const) {
$symbols[$name] = $this->numberFormatterPrototype->getSymbol($const);
if ('rounding_mode' === $name) {
$value = array_flip(self::NUMBER_ROUNDING_ATTRIBUTES)[$value];
} elseif ('padding_position' === $name) {
$value = array_flip(self::NUMBER_PADDING_ATTRIBUTES)[$value];
}
$attrs[$name] = $value;
}

ksort($attrs);
$hash = $locale.'|'.$style.'|'.json_encode($attrs).'|'.json_encode($textAttrs).'|'.json_encode($symbols);
$hash = $locale.'|'.$style.'|'.json_encode($attrs).'|'.$prototypeHash;

if (!isset($this->numberFormatters[$hash])) {
$isNew = !isset($this->numberFormatters[$hash]);
if ($isNew) {
if (\count($this->numberFormatters) >= self::MAX_CACHED_FORMATTERS) {
array_shift($this->numberFormatters);
}
$this->numberFormatters[$hash] = new \NumberFormatter($locale, self::NUMBER_STYLES[$style]);
}

$formatter = $this->numberFormatters[$hash];

foreach ($attrs as $name => $value) {
if (!isset(self::NUMBER_ATTRIBUTES[$name])) {
throw new RuntimeError(\sprintf('The number formatter attribute "%s" does not exist, known attributes are: "%s".', $name, implode('", "', array_keys(self::NUMBER_ATTRIBUTES))));
Expand All @@ -665,18 +660,49 @@ private function createNumberFormatter(?string $locale, string $style, array $at
$value = self::NUMBER_PADDING_ATTRIBUTES[$value];
}

$this->numberFormatters[$hash]->setAttribute(self::NUMBER_ATTRIBUTES[$name], $value);
// the attributes are part of the cache key, so a reused formatter already has them
if ($isNew) {
$formatter->setAttribute(self::NUMBER_ATTRIBUTES[$name], $value);
}
}

foreach ($textAttrs as $name => $value) {
$this->numberFormatters[$hash]->setTextAttribute(self::NUMBER_TEXT_ATTRIBUTES[$name], $value);
if ($isNew) {
foreach ($textAttrs as $name => $value) {
$formatter->setTextAttribute(self::NUMBER_TEXT_ATTRIBUTES[$name], $value);
}

foreach ($symbols as $name => $value) {
$formatter->setSymbol(self::NUMBER_SYMBOLS[$name], $value);
}
}

foreach ($symbols as $name => $value) {
$this->numberFormatters[$hash]->setSymbol(self::NUMBER_SYMBOLS[$name], $value);
return $formatter;
}

/**
* @return array{array<string, int|float>, array<string, string>, array<string, string>, string}
*/
private function readNumberFormatterPrototype(): array
{
$attrs = [];
$textAttrs = [];
$symbols = [];

if ($this->numberFormatterPrototype) {
foreach (self::NUMBER_ATTRIBUTES as $name => $const) {
$attrs[$name] = $this->numberFormatterPrototype->getAttribute($const);
}

foreach (self::NUMBER_TEXT_ATTRIBUTES as $name => $const) {
$textAttrs[$name] = $this->numberFormatterPrototype->getTextAttribute($const);
}

foreach (self::NUMBER_SYMBOLS as $name => $const) {
$symbols[$name] = $this->numberFormatterPrototype->getSymbol($const);
}
}

return $this->numberFormatters[$hash];
return [$attrs, $textAttrs, $symbols, json_encode($textAttrs).'|'.json_encode($symbols)];
}

private function createListFormatter(?string $locale, string $type, string $width): \IntlListFormatter
Expand Down
23 changes: 23 additions & 0 deletions extra/intl-extra/Tests/IntlExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ public function testFormatterProto(): void
);
}

public function testFormatterProtoIsAppliedOnEveryCallIncludingCachedOnes(): void
{
$numberFormatterProto = new \NumberFormatter('en_US', \NumberFormatter::DECIMAL);
$numberFormatterProto->setTextAttribute(\NumberFormatter::POSITIVE_PREFIX, '++');
$numberFormatterProto->setTextAttribute(\NumberFormatter::NEGATIVE_PREFIX, '--');
$numberFormatterProto->setAttribute(\NumberFormatter::FRACTION_DIGITS, 1);
$numberFormatterProto->setAttribute(\NumberFormatter::ROUNDING_MODE, \NumberFormatter::ROUND_FLOOR);
$ext = new IntlExtension(null, $numberFormatterProto);

$this->assertSame('++12.3', $ext->formatNumber('12.3456', [], 'decimal', 'default', 'en_US'));
$this->assertSame('++12.3', $ext->formatNumber('12.3456', [], 'decimal', 'default', 'en_US'));
$this->assertSame('--12.4', $ext->formatNumber('-12.3456', [], 'decimal', 'default', 'en_US'));

$explicitDigits = ['max_fraction_digit' => 3, 'min_fraction_digit' => 3];
$this->assertSame('++12.345', $ext->formatNumber('12.3456', $explicitDigits, 'decimal', 'default', 'en_US'));
$this->assertSame('++12.345', $ext->formatNumber('12.3456', $explicitDigits, 'decimal', 'default', 'en_US'));

$this->assertSame('++12.4', $ext->formatNumber('12.3556', ['rounding_mode' => 'halfup'], 'decimal', 'default', 'en_US'));
$this->assertSame('++12.3', $ext->formatNumber('12.3556', [], 'decimal', 'default', 'en_US'));

$this->assertSame('++12.3', $ext->formatNumber('12.3456', [], 'decimal', 'default', 'fr'));
}

public function testFormatterOverridenProto(): void
{
$dateFormatterProto = new \IntlDateFormatter('fr', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, new \DateTimeZone('Europe/Paris'));
Expand Down
Loading