[php-src] PR #19232: WIP - Intl: Add a new IntlNumberRangeFormatter class
| From: | BogdanUngureanu | Date: | Thu, 24 Jul 2025 23:14:24 +0000 |
| Subject: | [php-src] PR #19232: WIP - Intl: Add a new IntlNumberRangeFormatter class | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-32590@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/19232
Author: BogdanUngureanu
closes https://github.com/php/php-src/issues/18599
Adds support for ICU's NumberRangeFormatter that returns a locale-aware string interval given 2
numbers. Instead of using the C API, I've opted for the C++ one because it offers a few more
possibilities in the future (it could combined with a new NumberFormatter ICU class) while the C
version only allows support for skeleton strings.
Since the new NumberFormatter class hasn't been added to PHP Intl yet, the implementation uses
skeletons for now.
The implementation uses a factory method that returns a new IntlNumberRangeFormatter object, while
the constructor is private. I went with this approach because it would allow the class to have a
second factory method in the future for different number formatting (e.g. NumberFormatter).
Something like
```
createFromSkeleton(...)
createFromNumberFormatter()
```
**Drawbacks of the current PHP API:**
The C++ version allows to configure the NumberFormatter/skeleton for $start and $end individually
(using a fluent interface). The PHP API I propose sets it for both numbers
C++ example code:
```
NumberRangeFormatter::with()
.identityFallback(UNUM_IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE)
.numberFormatterFirst(NumberFormatter::with().adoptUnit(MeasureUnit::createMeter()))
.numberFormatterSecond(NumberFormatter::with().adoptUnit(MeasureUnit::createKilometer()))
.locale("en-GB")
.formatFormattableRange(750, 1.2, status)
.toString(status);
// => "750 m - 1.2 km"
```
and a C example
```
// Setup:
UErrorCode ec = U_ZERO_ERROR;
UNumberRangeFormatter* uformatter =
unumrf_openForSkeletonCollapseIdentityFallbackAndLocaleWithError(
u"currency/USD precision-integer",
-1,
UNUM_RANGE_COLLAPSE_AUTO,
UNUM_IDENTITY_FALLBACK_APPROXIMATELY,
"en-US",
NULL,
&ec);
UFormattedNumberRange* uresult = unumrf_openResult(&ec);
if (U_FAILURE(ec)) { return; }
// Format a double range:
unumrf_formatDoubleRange(uformatter, 3.0, 5.0, uresult, &ec);
if (U_FAILURE(ec)) { return; }
// Get the result string:
int32_t len;
const UChar* str = ufmtval_getString(unumrf_resultAsValue(uresult, &ec), &len, &ec);
if (U_FAILURE(ec)) { return; }
// str should equal "$3 – $5"
```
While the code works, the work is still in progress - I still have to implement a proper error
handling. In the meantime, I would like to hear your opinions about the PHP interface I'm
proposing.