Skip to content

Intl: Add a new IntlListFormatter class #18519

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

Closed
Closed
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions ext/intl/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ if test "$PHP_INTL" != "no"; then
locale/locale_class.c
locale/locale_methods.c
locale/locale.c
listformatter/listformatter_class.c
listformatter/listformatter_data.c
msgformat/msgformat_attr.c
msgformat/msgformat_class.c
msgformat/msgformat_data.c
Expand Down Expand Up @@ -119,6 +121,7 @@ if test "$PHP_INTL" != "no"; then
$ext_builddir/grapheme
$ext_builddir/idn
$ext_builddir/locale
$ext_builddir/listformatter
$ext_builddir/msgformat
$ext_builddir/normalizer
$ext_builddir/resourcebundle
Expand Down
50 changes: 50 additions & 0 deletions ext/intl/listformatter/listformatter.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/** @generate-class-entries */

/** @not-serializable */
/** @strict-properties */
final class IntlListFormatter {

/** @cvalue ULISTFMT_TYPE_OR */
public const int TYPE_OR = UNKNOWN;

/** @cvalue ULISTFMT_TYPE_UNITS */
public const int TYPE_UNITS = UNKNOWN;

/** @cvalue ULISTFMT_TYPE_AND */
public const int TYPE_AND = UNKNOWN;

/** @cvalue ULISTFMT_WIDTH_WIDE */
public const int WIDTH_WIDE = UNKNOWN;

/** @cvalue ULISTFMT_WIDTH_SHORT */
public const int WIDTH_SHORT = UNKNOWN;

/** @cvalue ULISTFMT_WIDTH_NARROW */
public const int WIDTH_NARROW = UNKNOWN;

/**
* @param string $locale
* @param int $type
* @param int $width
*/
public function __construct(string $locale, int $type = IntlListFormatter::TYPE_AND, int $width = IntlListFormatter::WIDTH_WIDE) {}

/**
* @param array $strings
*
* @return string|false
*/
public function format(array $strings): string|false {}

/**
* @return int
*/
public function getErrorCode(): int {}

/**
* @return string
*/
public function getErrorMessage(): string {}
}
77 changes: 77 additions & 0 deletions ext/intl/listformatter/listformatter_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

221 changes: 221 additions & 0 deletions ext/intl/listformatter/listformatter_class.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/*
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Bogdan Ungureanu <[email protected]> |
+----------------------------------------------------------------------+
*/

#include "php.h"
#include "php_intl.h"
#include <unicode/ulistformatter.h>
#include "listformatter_arginfo.h"
#include "listformatter_class.h"
#include "intl_convert.h"


static zend_object_handlers listformatter_handlers;

/* {{{ listformatter_free_obj */
static void listformatter_free_obj(zend_object *object)
{
ListFormatter_object *obj = php_intl_listformatter_fetch_object(object);
listformatter_data_free(&obj->lf_data);
zend_object_std_dtor(&obj->zo);
}
/* }}} */

/* {{{ listformatter_create_object */
static zend_object *listformatter_create_object(zend_class_entry *class_type)
{
ListFormatter_object *obj;
obj = zend_object_alloc(sizeof(ListFormatter_object), class_type);
listformatter_data_init(&obj->lf_data);
zend_object_std_init(&obj->zo, class_type);
object_properties_init(&obj->zo, class_type);
obj->zo.handlers = &listformatter_handlers;
return &obj->zo;
}
/* }}} */

/* {{{ listformatter_create_object */
PHP_METHOD(IntlListFormatter, __construct)
{
ListFormatter_object *obj = Z_INTL_LISTFORMATTER_P(ZEND_THIS);
zend_string *locale;
zend_long type = ULISTFMT_TYPE_AND;
zend_long width = ULISTFMT_WIDTH_WIDE;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STR(locale)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(type)
Z_PARAM_LONG(width)
ZEND_PARSE_PARAMETERS_END();

if (strlen(uloc_getISO3Language(ZSTR_VAL(locale))) == 0) {
zend_argument_value_error(1, "\"%s\" is invalid", ZSTR_VAL(locale));
RETURN_THROWS();
}

UErrorCode status = U_ZERO_ERROR;
if (U_ICU_VERSION_MAJOR_NUM >= 67) {

LISTFORMATTER_OBJECT(obj) = ulistfmt_openForType(ZSTR_VAL(locale), type, width, &status);
} else {
if (type != ULISTFMT_TYPE_AND) {
zend_argument_value_error(2, "ICU 66 and below only support IntlListFormatter::TYPE_AND");
RETURN_THROWS();
}

if (width != ULISTFMT_WIDTH_WIDE) {
zend_argument_value_error(3, "ICU 66 and below only support IntlListFormatter::WIDTH_WIDE");
RETURN_THROWS();
}

LISTFORMATTER_OBJECT(obj) = ulistfmt_open(ZSTR_VAL(locale), &status);
}
if (U_FAILURE(status)) {
intl_error_set(NULL, status, "Constructor failed", 0);
zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
RETURN_THROWS();
}
}
/* }}} */

/* {{{ listformatter_format */
PHP_METHOD(IntlListFormatter, format)
{
ListFormatter_object *obj = Z_INTL_LISTFORMATTER_P(ZEND_THIS);
zval *strings;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(strings)
ZEND_PARSE_PARAMETERS_END();

if (!LISTFORMATTER_OBJECT(obj)) {
intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR, "ListFormatter not properly constructed", 0);
RETURN_FALSE;
}

HashTable *ht = Z_ARRVAL_P(strings);
uint32_t count = zend_array_count(ht);
if (count == 0) {
RETURN_EMPTY_STRING();
}

const UChar **items = (const UChar **)safe_emalloc(count, sizeof(const UChar *), 0);
int32_t *itemLengths = (int32_t *)safe_emalloc(count, sizeof(int32_t), 0);
uint32_t i = 0;
zval *val;

ZEND_HASH_FOREACH_VAL(ht, val) {
zend_string *str_val;

str_val = zval_get_string(val);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One optimization that you could do (optional) is using zval_get_tmp_string instead. Although it probably doesn't matter too much here


// Convert PHP string to UTF-16
UChar *ustr = NULL;
int32_t ustr_len = 0;
UErrorCode status = U_ZERO_ERROR;

intl_convert_utf8_to_utf16(&ustr, &ustr_len, ZSTR_VAL(str_val), ZSTR_LEN(str_val), &status);
if (U_FAILURE(status)) {
efree(items);
efree(itemLengths);
zend_string_release(str_val);
intl_error_set(NULL, status, "Failed to convert string to UTF-16", 0);
RETURN_FALSE;
}

items[i] = ustr;
itemLengths[i] = ustr_len;
zend_string_release(str_val);
i++;
} ZEND_HASH_FOREACH_END();

UErrorCode status = U_ZERO_ERROR;
int32_t resultLength;
UChar *result = NULL;

resultLength = ulistfmt_format(LISTFORMATTER_OBJECT(obj), items, itemLengths, count, NULL, 0, &status);
Copy link
Member

@devnexen devnexen May 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick question, do you think there is a benefit of using ulistfmt_formatStringsToResult beside the "no need to calculate the buffer first" ? any drawback ? Think more of use case to apply in php rather than api conveniency. Cheers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note I m not holding the PR merge at all, just asking out of curiosity/trying to see a possible future improvement :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what would be the advantage (besides not calculating the buffer). The drawback is that it's supported since ICU 64 and PHP Intl supports 57 and up. I guess we could conditionally use it for >= ICU 64, but personally I would keep the code simple :)

Copy link
Member

@devnexen devnexen May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say this ... ulistfmt_formatStringsToResult is not necessarily interesting by itself but what you can get out of it however is what is called field positions via a UFieldPositionIterator, going through it with ufieldpositer_next ... you can get nice additional infos ... but again my question was more to give food for thoughts ;)


if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) {
intl_error_set(NULL, status, "Failed to format list", 0);
RETURN_FALSE;
}

// Allocate buffer and try again
status = U_ZERO_ERROR;
result = (UChar *)emalloc((resultLength + 1) * sizeof(UChar));
ulistfmt_format(LISTFORMATTER_OBJECT(obj), items, itemLengths, count, result, resultLength, &status);

// Clean up input strings
for (i = 0; i < count; i++) {
efree((void *)items[i]);
}
efree(items);
efree(itemLengths);

if (U_FAILURE(status)) {
if (result) {
efree(result);
}
intl_error_set(NULL, status, "Failed to format list", 0);
RETURN_FALSE;
}

// Convert result back to UTF-8
zend_string *ret = intl_convert_utf16_to_utf8(result, resultLength, &status);
efree(result);

if (!ret) {
intl_error_set(NULL, status, "Failed to convert result to UTF-8", 0);
RETURN_FALSE;
}

RETURN_STR(ret);
}
/* }}} */

/* {{{ listformatter_getErrorCode */
PHP_METHOD(IntlListFormatter, getErrorCode)
{
ZEND_PARSE_PARAMETERS_NONE();

ListFormatter_object *obj = Z_INTL_LISTFORMATTER_P(ZEND_THIS);

UErrorCode status = intl_error_get_code(LISTFORMATTER_ERROR_P(obj));

RETURN_LONG(status);
}
/* }}} */

/* {{{ listformatter_getErrorMessage */
PHP_METHOD(IntlListFormatter, getErrorMessage)
{
ZEND_PARSE_PARAMETERS_NONE();

ListFormatter_object *obj = Z_INTL_LISTFORMATTER_P(ZEND_THIS);

zend_string *message = intl_error_get_message(LISTFORMATTER_ERROR_P(obj));
RETURN_STR(message);
}
/* }}} */

/* {{{ listformatter_register_class */
void listformatter_register_class(void)
{
zend_class_entry *class_entry = register_class_IntlListFormatter();
class_entry->create_object = listformatter_create_object;

memcpy(&listformatter_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
listformatter_handlers.offset = XtOffsetOf(ListFormatter_object, zo);
listformatter_handlers.free_obj = listformatter_free_obj;
}
/* }}} */
Loading
Loading