diff --git a/NEWS b/NEWS index de80e308a4df..3cdfc7969523 100644 --- a/NEWS +++ b/NEWS @@ -87,6 +87,8 @@ PHP NEWS . Fixed intl_error_name() truncating error codes outside the int range. (lacatoire) . Fixed IntlCalendar::setTime() accepting non-finite timestamps. (Weilin Du) + . Fixed MessageFormatter::setPattern() losing the previous pattern when + the new pattern is invalid. (Weilin Du, David Carlier) - JSON: . Fixed json_decode() and json_validate() error messages. (lacatoire) diff --git a/ext/intl/msgformat/msgformat_attr.c b/ext/intl/msgformat/msgformat_attr.c index b56bf360d114..25c960b956b2 100644 --- a/ext/intl/msgformat/msgformat_attr.c +++ b/ext/intl/msgformat/msgformat_attr.c @@ -76,18 +76,24 @@ PHP_FUNCTION( msgfmt_set_pattern ) } #endif - umsg_applyPattern(MSG_FORMAT_OBJECT(mfo), spattern, spattern_len, &spattern_error, &INTL_DATA_ERROR_CODE(mfo)); + /* ICU may discard the old pattern if parsing the new one fails. */ + UMessageFormat *new_format = umsg_clone(MSG_FORMAT_OBJECT(mfo), &INTL_DATA_ERROR_CODE(mfo)); + umsg_applyPattern(new_format, spattern, spattern_len, &spattern_error, &INTL_DATA_ERROR_CODE(mfo)); if (spattern) { efree(spattern); } if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) { char *msg; + umsg_close(new_format); spprintf(&msg, 0, "Error setting symbol value at line %d, offset %d", spattern_error.line, spattern_error.offset); intl_errors_set_custom_msg(INTL_DATA_ERROR_P(mfo), msg, 1); efree(msg); RETURN_FALSE; } + umsg_close(MSG_FORMAT_OBJECT(mfo)); + MSG_FORMAT_OBJECT(mfo) = new_format; + mfo->mf_data.tz_set = 0; if(mfo->mf_data.orig_format) { efree(mfo->mf_data.orig_format); } diff --git a/ext/intl/tests/msgfmt_set_pattern_preserves_state.phpt b/ext/intl/tests/msgfmt_set_pattern_preserves_state.phpt new file mode 100644 index 000000000000..e26b42b3ed35 --- /dev/null +++ b/ext/intl/tests/msgfmt_set_pattern_preserves_state.phpt @@ -0,0 +1,28 @@ +--TEST-- +MessageFormatter::setPattern() preserves the previous pattern on failure +--CREDITS-- +Wong Wai Kin +--EXTENSIONS-- +intl +--FILE-- +format(['world'])); + +var_dump($formatter->setPattern('{0,date')); +var_dump($formatter->getPattern()); +var_dump($formatter->format(['world'])); + +var_dump($formatter->setPattern('{0,number,integer} items')); +var_dump($formatter->format([2])); + +var_dump($formatter->setPattern('{0} {0,number}')); +?> +--EXPECT-- +string(13) "Hello, world!" +bool(false) +string(11) "Hello, {0}!" +string(13) "Hello, world!" +bool(true) +string(7) "2 items" +bool(true)