php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #61871
Patch intl-named-arguments-2.diff revision 2012-05-03 18:06 UTC by web-php-bugs at sklar dot com
Patch intl-named-arguments revision 2012-04-27 19:03 UTC by web-php-bugs at sklar dot com

Patch intl-named-arguments-2.diff for intl Bug #61871

Patch version 2012-05-03 18:06 UTC

Return to Bug #61871 | Download this patch
Patch Revisions:

Developer: web-php-bugs@sklar.com

diff --git a/msgformat/msgformat_format.c b/msgformat/msgformat_format.c
index b664c83..9d51c36 100755
--- a/msgformat/msgformat_format.c
+++ b/msgformat/msgformat_format.c
@@ -32,17 +32,21 @@
 #endif
 
 /* {{{ */
-static void msgfmt_do_format(MessageFormatter_object *mfo, zval *args, zval *return_value TSRMLS_DC) 
+static void msgfmt_do_format(MessageFormatter_object *mfo, zval *args, zval *return_value TSRMLS_DC)
 {
 	zval **fargs;
 	int count;
 	UChar* formatted = NULL;
+    char **farg_names = NULL;
 	int formatted_len = 0;
 	HashPosition pos;
 	int i;
 
 	count = zend_hash_num_elements(Z_ARRVAL_P(args));
 
+    /* umsg_format_arg_count() always returns 0 for named argument patterns,
+     * so this check is ignored and un-substituted {name} strings
+     * in a pattern are returned unmodified. */
 	if(count < umsg_format_arg_count(MSG_FORMAT_OBJECT(mfo))) {
 		/* Not enough aguments for format! */
 		intl_error_set( INTL_DATA_ERROR_P(mfo), U_ILLEGAL_ARGUMENT_ERROR,
@@ -51,26 +55,51 @@ static void msgfmt_do_format(MessageFormatter_object *mfo, zval *args, zval *ret
 		return;
 	}
 
+	zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(args), &pos);
 	fargs = safe_emalloc(count, sizeof(zval *), 0);
+    /* If the first key is a string, then treat everything as a named argument */
+    if (HASH_KEY_IS_STRING == zend_hash_get_current_key_type_ex(Z_ARRVAL_P(args), &pos)) {
+        farg_names = safe_emalloc(count, sizeof(char *), 0);
+    }
 
-	zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(args), &pos);
 	for(i=0;i<count;i++) {
 		zval **val;
 		zend_hash_get_current_data_ex(Z_ARRVAL_P(args), (void **)&val, &pos);
 		fargs[i] = *val;
 		Z_ADDREF_P(fargs[i]);
 		/* TODO: needs refcount increase here? */
+        if (NULL != farg_names) {
+            char *key;
+            uint key_len;
+            ulong index;
+            int key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(args), &key, &key_len, &index, 0, &pos);
+            if (HASH_KEY_IS_STRING == key_type) {
+                farg_names[i] = estrndup(key, key_len);
+            }
+            else if (HASH_KEY_IS_LONG == key_type) {
+                farg_names[i] = emalloc(sizeof("18446744073709551616")); // 2^64
+                snprintf(farg_names[i], sizeof("18446744073709551616"), "%lu", (unsigned long) index);
+            }
+            else {
+                farg_names[i] = estrndup("", 0);
+            }
+        }
 		zend_hash_move_forward_ex(Z_ARRVAL_P(args), &pos);
 	}
 
-	umsg_format_helper(MSG_FORMAT_OBJECT(mfo), count, fargs, &formatted, &formatted_len, &INTL_DATA_ERROR_CODE(mfo) TSRMLS_CC);
+	umsg_format_helper(MSG_FORMAT_OBJECT(mfo), count, fargs, farg_names, &formatted, &formatted_len, &INTL_DATA_ERROR_CODE(mfo) TSRMLS_CC);
 
 	for(i=0;i<count;i++) {
 		zval_ptr_dtor(&fargs[i]);
 	}
 
 	efree(fargs);
-
+    if (farg_names) {
+        for (i = 0; i < count; i++) {
+            efree(farg_names[i]);
+        }
+        efree(farg_names);
+    }
 	if (formatted && U_FAILURE( INTL_DATA_ERROR_CODE(mfo) ) ) {
 			efree(formatted);
 	}
diff --git a/msgformat/msgformat_helpers.cpp b/msgformat/msgformat_helpers.cpp
index 1895de2..7b04cef 100755
--- a/msgformat/msgformat_helpers.cpp
+++ b/msgformat/msgformat_helpers.cpp
@@ -21,6 +21,9 @@
 #include <math.h>
 #include <unicode/msgfmt.h>
 #include <unicode/chariter.h>
+#include <unicode/messagepattern.h>
+
+#include <map>
 
 extern "C" {
 #include "php_intl.h"
@@ -28,6 +31,7 @@ extern "C" {
 #include "msgformat_format.h"
 #include "msgformat_helpers.h"
 #include "intl_convert.h"
+#include "ext/date/php_date.h"
 }
 
 U_NAMESPACE_BEGIN
@@ -40,87 +44,301 @@ class MessageFormatAdapter {
 public:
     static const Formattable::Type* getArgTypeList(const MessageFormat& m,
                                                    int32_t& count);
+    static const MessagePattern getMessagePattern(MessageFormat* m);
 };
 const Formattable::Type*
 MessageFormatAdapter::getArgTypeList(const MessageFormat& m,
                                      int32_t& count) {
     return m.getArgTypeList(count);
 }
+const MessagePattern
+MessageFormatAdapter::getMessagePattern(MessageFormat* m) {
+    return m->msgPattern;
+}
 U_NAMESPACE_END
 
-U_CFUNC int32_t umsg_format_arg_count(UMessageFormat *fmt) 
+U_CFUNC int32_t umsg_format_arg_count(UMessageFormat *fmt)
 {
 	int32_t fmt_count = 0;
 	MessageFormatAdapter::getArgTypeList(*(const MessageFormat*)fmt, fmt_count);
 	return fmt_count;
 }
 
-U_CFUNC void umsg_format_helper(UMessageFormat *fmt, int arg_count, zval **args, UChar **formatted, int *formatted_len, UErrorCode *status TSRMLS_DC)
+double umsg_helper_zval_to_millis(zval *z, UErrorCode *status TSRMLS_DC) {
+    double rv = 0.0;
+    if (Z_TYPE_P(z) == IS_DOUBLE) {
+        rv = U_MILLIS_PER_SECOND * Z_DVAL_P(z);
+    }
+    else if (Z_TYPE_P(z) == IS_LONG) {
+        rv = U_MILLIS_PER_SECOND * (double) Z_LVAL_P(z);
+    }
+    else if (Z_TYPE_P(z) == IS_OBJECT) {
+        /* Borrowed from datefmt_format() in intl/dateformat/dateformat_format.c */
+        if (instanceof_function(Z_OBJCE_P(z), php_date_get_date_ce() TSRMLS_CC)) {
+            zval retval;
+            zval *zfuncname;
+            INIT_ZVAL(retval);
+            MAKE_STD_ZVAL(zfuncname);
+            ZVAL_STRING(zfuncname, "getTimestamp", 1);
+            if (call_user_function(NULL, &(z), zfuncname, &retval, 0, NULL TSRMLS_CC) != SUCCESS || Z_TYPE(retval) != IS_LONG) {
+                *status = U_RESOURCE_TYPE_MISMATCH;
+            } else {
+                rv = U_MILLIS_PER_SECOND * (double) Z_LVAL(retval);
+            }
+            zval_ptr_dtor(&zfuncname);
+        } else {
+            *status = U_ILLEGAL_ARGUMENT_ERROR;
+        }
+    }
+    return rv;
+}
+
+U_CFUNC void umsg_format_helper(UMessageFormat *fmt, int arg_count, zval **args, char **arg_names, UChar **formatted, int *formatted_len, UErrorCode *status TSRMLS_DC)
 {
-	int fmt_count = 0;
-    const Formattable::Type* argTypes =
-		MessageFormatAdapter::getArgTypeList(*(const MessageFormat*)fmt, fmt_count);
-	Formattable* fargs = new Formattable[fmt_count ? fmt_count : 1];
-
-	for(int32_t i = 0; i < fmt_count; ++i) {
-        UChar  *stringVal = NULL;
-		int     stringLen = 0;
-		int64_t tInt64 = 0;
-
-		switch(argTypes[i]) {
-			case Formattable::kDate:
-				convert_to_long_ex(&args[i]);
-				fargs[i].setDate(U_MILLIS_PER_SECOND * (double)Z_LVAL_P(args[i]));
-				break;
-
-			case Formattable::kDouble:
-				convert_to_double_ex(&args[i]);
-			    fargs[i].setDouble(Z_DVAL_P(args[i]));
-				break;
-            
-	        case Formattable::kLong:
-				convert_to_long_ex(&args[i]);
-			    fargs[i].setLong(Z_LVAL_P(args[i]));
-				break;
-
-	        case Formattable::kInt64:
-				if(Z_TYPE_P(args[i]) == IS_DOUBLE) {
-					tInt64 = (int64_t)Z_DVAL_P(args[i]);
-				} else if(Z_TYPE_P(args[i]) == IS_LONG) {
-					tInt64 = (int64_t)Z_LVAL_P(args[i]);
-				} else {
-					SEPARATE_ZVAL_IF_NOT_REF(&args[i]);
-					convert_scalar_to_number( args[i] TSRMLS_CC );
-					tInt64 = (Z_TYPE_P(args[i]) == IS_DOUBLE)?(int64_t)Z_DVAL_P(args[i]):Z_LVAL_P(args[i]);
-				}
-			    fargs[i].setInt64(tInt64);
-				break;
-            
-	        case Formattable::kString:
-		        convert_to_string_ex(&args[i]);
-				intl_convert_utf8_to_utf16(&stringVal, &stringLen, Z_STRVAL_P(args[i]), Z_STRLEN_P(args[i]), status);
-				if(U_FAILURE(*status)){
-					delete[] fargs;
-					return;
-				}
-				fargs[i].setString(stringVal);
-				efree(stringVal);
-			    break;
-            
-			case Formattable::kArray:
-			case Formattable::kObject:
-				*status = U_UNSUPPORTED_ERROR;
-				delete[] fargs;
-				return;
-        }		
-	}
+	int fmt_count;
+    int32_t i;
+	Formattable* fargs;
+    UnicodeString *farg_names;
+    MessageFormat *mf = (MessageFormat *) fmt;
+    MessagePattern mp = MessageFormatAdapter::getMessagePattern(mf);
+    std::map<UnicodeString, Formattable::Type> argTypesNamed;
+    std::map<int32_t, Formattable::Type> argTypesNumbered;
+
+    int32_t usingNamedArguments = mf->usesNamedArguments();
+
+    /*
+      looking through the pattern, go to each arg_start part type.
+      the arg-typeof that tells us the argument type (simple, complicated)
+      then the next part is either the arg_name or arg number
+      and then if it's simple after that there could be a part-type=arg-type whise substring will tell us number, spellout, etc
+      if the next thing isn't an arg-type then assume string
+      same name that appears more than once in a complicated pattern will appear more than once, we could
+      -- ignore subsequent occurances
+      -- complain if types differ?
+    */
+
+    int32_t parts_count = mp.countParts();
+
+    for (i = 0; i < parts_count; i++) {
+        MessagePattern::Part p = mp.getPart(i);
+        if (p.getType() == UMSGPAT_PART_TYPE_ARG_START) {
+            MessagePattern::Part name_part = mp.getPart(++i); /* Getting name, advancing i */
+            UnicodeString argName;
+            int32_t argNumber;
+            if (name_part.getType() == UMSGPAT_PART_TYPE_ARG_NAME) {
+                argName = mp.getSubstring(name_part);
+            }
+            else if (name_part.getType() == UMSGPAT_PART_TYPE_ARG_NUMBER) {
+                argNumber = name_part.getValue();
+            }
+            /* If we haven't seen this arg name before */
+            int seenBefore = usingNamedArguments ? argTypesNamed.count(argName) : argTypesNumbered.count(argNumber);
+            if (0 == seenBefore) {
+                Formattable::Type fargType;
+                UMessagePatternArgType argType = p.getArgType();
+                /* No type specified, treat it as a string */
+                if (argType == UMSGPAT_ARG_TYPE_NONE) {
+                    fargType = Formattable::kString;
+                }
+                /* Some type was specified, might be simple or complicated */
+                else {
+                    if (argType == UMSGPAT_ARG_TYPE_SIMPLE) {
+                        /* For a SIMPLE arg, after the name part, there should be
+                         * an ARG_TYPE part whose string value tells us what to do */
+                        MessagePattern::Part type_part = mp.getPart(++i); /* Getting type, advancing i */
+                        if (type_part.getType() == UMSGPAT_PART_TYPE_ARG_TYPE) {
+                            UnicodeString typeString = mp.getSubstring(type_part);
+                            /* This is all based on the rules in the docs for MessageFormat
+                             * @see http://icu-project.org/apiref/icu4c/classMessageFormat.html */
+                            if (typeString == "number") {
+                                MessagePattern::Part style_part = mp.getPart(i + 1); /* Not advancing i */
+                                if (style_part.getType() == UMSGPAT_PART_TYPE_ARG_STYLE) {
+                                    UnicodeString styleString = mp.getSubstring(style_part);
+                                    if (styleString == "integer") {
+                                        fargType = Formattable::kInt64;
+                                    }
+                                    else if (styleString == "currency") {
+                                        fargType = Formattable::kDouble;
+                                    }
+                                    else if (styleString == "percent") {
+                                        fargType = Formattable::kDouble;
+                                    }
+                                }
+                                // if missing style, part, make it a double
+                                else {
+                                    fargType = Formattable::kDouble;
+                                }
+                            }
+                            else if ((typeString == "date") || (typeString == "time")) {
+                                fargType = Formattable::kDate;
+                            }
+                            else if ((typeString == "spellout") || (typeString == "ordinal") || (typeString == "duration")) {
+                                fargType = Formattable::kDouble;
+                            }
+
+                        }
+                        else {
+                            /* If there's no UMSGPAT_PART_TYPE_ARG_TYPE right after a
+                             * UMSGPAT_ARG_TYPE_SIMPLE argument, then the pattern
+                             * is broken. */
+                            *status = U_PARSE_ERROR;
+                            return;
+                        }
+                    }
+                    else if (argType == UMSGPAT_ARG_TYPE_PLURAL) {
+                        fargType = Formattable::kDouble;
+                    }
+                    else if (argType == UMSGPAT_ARG_TYPE_CHOICE) {
+                        fargType = Formattable::kDouble;
+                    }
+                    else if (argType == UMSGPAT_ARG_TYPE_SELECT) {
+                        fargType = Formattable::kString;
+                    }
+                    else {
+                        fargType = Formattable::kString;
+                    }
+                } /* was type specified? */
+                if (usingNamedArguments) {
+                    argTypesNamed.insert(std::pair<UnicodeString, Formattable::Type>(argName, fargType));
+                } else {
+                    argTypesNumbered.insert(std::pair<int32_t, Formattable::Type>(argNumber, fargType));
+                }
+            } /* Haven't seen arg before? */
+        } /* checking for ARG_START */
+    } /* visiting each part */
+
+#define CLEANUP_AND_RETURN_ON_ERROR(status) do { \
+        if (U_FAILURE(*status)) {                \
+            delete[] fargs;                      \
+            if (usingNamedArguments) {           \
+                delete[] farg_names;             \
+            }                                    \
+            return;                              \
+        }                                        \
+    } while (0)
+
+
+    fmt_count = arg_count;
+    fargs = new Formattable[fmt_count];
+    if (usingNamedArguments) {
+        farg_names = new UnicodeString[fmt_count];
+    }
+    for (int32_t i = 0; i < fmt_count; ++i) {
+            UChar* text = NULL;
+            int textLen = 0;
+            int found = 0;
+            Formattable::Type argType;
+
+            if (usingNamedArguments) {
+                intl_convert_utf8_to_utf16(&text, &textLen, arg_names[i], strlen(arg_names[i]), status);
+                CLEANUP_AND_RETURN_ON_ERROR(status);
+                farg_names[i].setTo(text, textLen);
+                efree(text);
+                text = NULL; textLen = 0;
+                std::map<UnicodeString, Formattable::Type>::iterator it;
+                it = argTypesNamed.find(farg_names[i]);
+                if (it != argTypesNamed.end()) {
+                    argType = it->second;
+                    found = 1;
+                }
+            }
+            else {
+                std::map<int32_t, Formattable::Type>::iterator it;
+                it = argTypesNumbered.find(i);
+                if (it != argTypesNumbered.end()) {
+                    argType = it->second;
+                    found = 1;
+                }
+            }
+            if (found) {
+                switch (argType) {
+                case Formattable::kString:
+                    /* This implicitly converts objects by attempting to call __toString() */
+                    convert_to_string_ex(&args[i]);
+                    intl_convert_utf8_to_utf16(&text, &textLen, Z_STRVAL_P(args[i]), Z_STRLEN_P(args[i]), status);
+                    CLEANUP_AND_RETURN_ON_ERROR(status);
+                    fargs[i].setString(text);
+                    efree(text);
+                    text = NULL; textLen = 0;
+                    break;
+                case Formattable::kDouble:
+                    {
+                        double d;
+                        if(Z_TYPE_P(args[i]) == IS_DOUBLE) {
+                            d = Z_DVAL_P(args[i]);
+                        } else if(Z_TYPE_P(args[i]) == IS_LONG) {
+                            d = (double)Z_LVAL_P(args[i]);
+                        } else {
+                            SEPARATE_ZVAL_IF_NOT_REF(&args[i]);
+                            convert_scalar_to_number( args[i] TSRMLS_CC );
+                            d = (Z_TYPE_P(args[i]) == IS_DOUBLE)?Z_DVAL_P(args[i]):(double)Z_LVAL_P(args[i]);
+                        }
+                        fargs[i].setDouble(d);
+                        break;
+                    }
+                case Formattable::kInt64:
+                    {
+                        int64_t tInt64;
+                        if(Z_TYPE_P(args[i]) == IS_DOUBLE) {
+                            tInt64 = (int64_t)Z_DVAL_P(args[i]);
+                        } else if(Z_TYPE_P(args[i]) == IS_LONG) {
+                            tInt64 = (int64_t)Z_LVAL_P(args[i]);
+                        } else {
+                            SEPARATE_ZVAL_IF_NOT_REF(&args[i]);
+                            convert_scalar_to_number( args[i] TSRMLS_CC );
+                            tInt64 = (Z_TYPE_P(args[i]) == IS_DOUBLE)?(int64_t)Z_DVAL_P(args[i]):Z_LVAL_P(args[i]);
+                        }
+                        fargs[i].setInt64(tInt64);
+                        break;
+                    }
+                case Formattable::kDate:
+                    {
+                        double dd = umsg_helper_zval_to_millis(args[i], status TSRMLS_CC);
+                        CLEANUP_AND_RETURN_ON_ERROR(status);
+                        fargs[i].setDate(dd);
+                        break;
+                    }
+                }
+            }
+            else {
+                /* We couldn't find any information about the argument in the pattern, this
+                 * means it's an extra argument. So convert it to a number if it's a number or
+                 * bool or null and to a string if it's anything else. */
+                switch (Z_TYPE_P(args[i])) {
+                case IS_DOUBLE:
+                    fargs[i].setDouble(Z_DVAL_P(args[i]));
+                    break;
+                case IS_BOOL:
+                    convert_to_long_ex(&args[i]);
+                    /* Intentional fallthrough */
+                case IS_LONG:
+                    fargs[i].setInt64((int64_t) Z_LVAL_P(args[i]));
+                    break;
+                case IS_NULL:
+                    fargs[i].setInt64((int64_t) 0);
+                    break;
+                default:
+                    convert_to_string_ex(&args[i]);
+                    intl_convert_utf8_to_utf16(&text, &textLen, Z_STRVAL_P(args[i]), Z_STRLEN_P(args[i]), status);
+                    CLEANUP_AND_RETURN_ON_ERROR(status);
+                    fargs[i].setString(text);
+                    efree(text);
+                    text = NULL; textLen = 0;
+                    break;
+                }
+            }
+    } // visiting each argument argument
 
     UnicodeString resultStr;
     FieldPosition fieldPosition(0);
-    
-    /* format the message */
-    ((const MessageFormat*)fmt)->format(fargs, fmt_count, resultStr, fieldPosition, *status);
 
+    /* format the message */
+    if (usingNamedArguments) {
+        mf->format(farg_names, fargs, fmt_count, resultStr, *status);
+        delete[] farg_names;
+    } else {
+        mf->format(fargs, fmt_count, resultStr, fieldPosition, *status);
+    }
     delete[] fargs;
 
     if(U_FAILURE(*status)){
@@ -154,7 +372,7 @@ U_CFUNC void umsg_parse_helper(UMessageFormat *fmt, int *count, zval ***args, UC
 		int stmp_len;
 
 		ALLOC_INIT_ZVAL((*args)[i]);
-		
+
 		switch(fargs[i].getType()) {
         case Formattable::kDate:
 			aDate = ((double)fargs[i].getDate())/U_MILLIS_PER_SECOND;
@@ -185,7 +403,7 @@ U_CFUNC void umsg_parse_helper(UMessageFormat *fmt, int *count, zval ***args, UC
         case Formattable::kString:
             fargs[i].getString(temp);
 			intl_convert_utf16_to_utf8(&stmp, &stmp_len, temp.getBuffer(), temp.length(), status);
-			if(U_FAILURE(*status)) {
+ 			if(U_FAILURE(*status)) {
 				cleanup_zvals();
 				return;
 			}
diff --git a/msgformat/msgformat_helpers.h b/msgformat/msgformat_helpers.h
index 30c7e39..f5b01d7 100755
--- a/msgformat/msgformat_helpers.h
+++ b/msgformat/msgformat_helpers.h
@@ -17,8 +17,8 @@
 #ifndef MSG_FORMAT_HELPERS_H
 #define MSG_FORMAT_HELPERS_H
 
-int32_t umsg_format_arg_count(UMessageFormat *fmt); 
-void umsg_format_helper(UMessageFormat *fmt, int arg_count, zval **args,
+int32_t umsg_format_arg_count(UMessageFormat *fmt);
+void umsg_format_helper(UMessageFormat *fmt, int arg_count, zval **args, char **arg_names,
 						UChar **formatted, int *formatted_len, UErrorCode *status TSRMLS_DC);
 void umsg_parse_helper(UMessageFormat *fmt, int *count, zval ***args,
 					   UChar *source, int source_len, UErrorCode *status);
diff --git a/tests/msgfmt_format_subpatterns.phpt b/tests/msgfmt_format_subpatterns.phpt
new file mode 100644
index 0000000..5a57df6
--- /dev/null
+++ b/tests/msgfmt_format_subpatterns.phpt
@@ -0,0 +1,71 @@
+--TEST--
+msgfmt_format() with subpatterns
+--SKIPIF--
+<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
+--FILE--
+<?php
+
+/*
+ * Format a number using misc locales/patterns.
+ */
+
+
+function ut_main()
+{
+
+$pattern=<<<_MSG_
+{0, select,
+  female {{1, plural, offset:1
+      =0 {{2} does not give a party.}
+      =1 {{2} invites {3} to her party.}
+      =2 {{2} invites {3} and one other person to her party.}
+     other {{2} invites {3} as one of the # people invited to her party.}}}
+  male   {{1, plural, offset:1
+      =0 {{2} does not give a party.}
+      =1 {{2} invites {3} to his party.}
+      =2 {{2} invites {3} and one other person to his party.}
+     other {{2} invites {3} as one of the # other people invited to his party.}}}
+  other {{1, plural, offset:1
+      =0 {{2} does not give a party.}
+      =1 {{2} invites {3} to their party.}
+      =2 {{2} invites {3} and one other person to their party.}
+      other {{2} invites {3} as one of the # other people invited to their party.}}}}
+_MSG_;
+
+
+$args = array(
+      array('female', 0,  'Alice', 'Bob'),
+      array('male',   1,  'Alice', 'Bob'),
+      array('none',   2,  'Alice', 'Bob'),
+      array('female', 27, 'Alice', 'Bob'),
+);
+
+$str_res = '';
+
+        $fmt = ut_msgfmt_create( 'en_US', $pattern );
+		if(!$fmt) {
+			$str_res .= dump(intl_get_error_message())."\n";
+			continue;
+		}
+        foreach ($args as $arg) {
+            $str_res .= dump( ut_msgfmt_format($fmt, $arg) ). "\n";
+            $str_res .= dump( ut_msgfmt_format_message('en_US', $pattern, $arg) ) . "\n";
+    }
+    return $str_res;
+}
+
+include_once( 'ut_common.inc' );
+
+// Run the test
+ut_run();
+
+?>
+--EXPECT--
+'Alice does not give a party.'
+'Alice does not give a party.'
+'Alice invites Bob to his party.'
+'Alice invites Bob to his party.'
+'Alice invites Bob and one other person to their party.'
+'Alice invites Bob and one other person to their party.'
+'Alice invites Bob as one of the 27 people invited to her party.'
+'Alice invites Bob as one of the 27 people invited to her party.'
diff --git a/tests/msgfmt_format_subpatterns_named.phpt b/tests/msgfmt_format_subpatterns_named.phpt
new file mode 100644
index 0000000..94329ed
--- /dev/null
+++ b/tests/msgfmt_format_subpatterns_named.phpt
@@ -0,0 +1,71 @@
+--TEST--
+msgfmt_format() with named subpatterns
+--SKIPIF--
+<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
+--FILE--
+<?php
+
+/*
+ * Format a number using misc locales/patterns.
+ */
+
+
+function ut_main()
+{
+
+$pattern=<<<_MSG_
+{gender_of_host, select,
+  female {{num_guests, plural, offset:1
+      =0 {{host} does not give a party.}
+      =1 {{host} invites {guest} to her party.}
+      =2 {{host} invites {guest} and one other person to her party.}
+     other {{host} invites {guest} as one of the # people invited to her party.}}}
+  male   {{num_guests, plural, offset:1
+      =0 {{host} does not give a party.}
+      =1 {{host} invites {guest} to his party.}
+      =2 {{host} invites {guest} and one other person to his party.}
+     other {{host} invites {guest} as one of the # people invited to his party.}}}
+  other {{num_guests, plural, offset:1
+      =0 {{host} does not give a party.}
+      =1 {{host} invites {guest} to their party.}
+      =2 {{host} invites {guest} and one other person to their party.}
+     other {{host} invites {guest} as one of the # people invited to their party.}}}}
+_MSG_;
+
+
+$args = array(
+      array('gender_of_host' => 'female', 'num_guests' => 0, 'host' => 'Alice', 'guest' => 'Bob'),
+      array('gender_of_host' => 'male', 'num_guests' => 1, 'host' => 'Alice', 'guest' => 'Bob'),
+      array('gender_of_host' => 'none', 'num_guests' => 2, 'host' => 'Alice', 'guest' => 'Bob'),
+      array('gender_of_host' => 'female', 'num_guests' => 27, 'host' => 'Alice', 'guest' => 'Bob'),
+);
+
+$str_res = '';
+
+        $fmt = ut_msgfmt_create( 'en_US', $pattern );
+		if(!$fmt) {
+			$str_res .= dump(intl_get_error_message())."\n";
+			continue;
+		}
+        foreach ($args as $arg) {
+            $str_res .= dump( ut_msgfmt_format($fmt, $arg) ). "\n";
+            $str_res .= dump( ut_msgfmt_format_message('en_US', $pattern, $arg) ) . "\n";
+    }
+    return $str_res;
+}
+
+include_once( 'ut_common.inc' );
+
+// Run the test
+ut_run();
+
+?>
+--EXPECT--
+'Alice does not give a party.'
+'Alice does not give a party.'
+'Alice invites Bob to his party.'
+'Alice invites Bob to his party.'
+'Alice invites Bob and one other person to their party.'
+'Alice invites Bob and one other person to their party.'
+'Alice invites Bob as one of the 27 people invited to her party.'
+'Alice invites Bob as one of the 27 people invited to her party.'
\ No newline at end of file
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 04:01:29 2024 UTC