xref: /PHP-8.1/ext/intl/msgformat/msgformat.c (revision 925a3097)
1 /*
2    +----------------------------------------------------------------------+
3    | This source file is subject to version 3.01 of the PHP license,      |
4    | that is bundled with this package in the file LICENSE, and is        |
5    | available through the world-wide-web at the following url:           |
6    | https://www.php.net/license/3_01.txt                                 |
7    | If you did not receive a copy of the PHP license and are unable to   |
8    | obtain it through the world-wide-web, please send a note to          |
9    | license@php.net so we can mail you a copy immediately.               |
10    +----------------------------------------------------------------------+
11    | Authors: Stanislav Malyshev <stas@zend.com>                          |
12    +----------------------------------------------------------------------+
13  */
14 
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18 
19 #include <unicode/ustring.h>
20 #include <unicode/umsg.h>
21 
22 #include "php_intl.h"
23 #include "msgformat_class.h"
24 #include "msgformat_data.h"
25 #include "intl_convert.h"
26 
27 /* {{{ */
msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS,zend_error_handling * error_handling,bool * error_handling_replaced)28 static int msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_error_handling *error_handling, bool *error_handling_replaced)
29 {
30 	const char* locale;
31 	char*       pattern;
32 	size_t      locale_len = 0, pattern_len = 0;
33 	UChar*      spattern     = NULL;
34 	int         spattern_len = 0;
35 	zval*       object;
36 	MessageFormatter_object* mfo;
37 	UParseError parse_error;
38 	intl_error_reset( NULL );
39 
40 	object = return_value;
41 	/* Parse parameters. */
42 	if( zend_parse_parameters( ZEND_NUM_ARGS(), "ss",
43 		&locale, &locale_len, &pattern, &pattern_len ) == FAILURE )
44 	{
45 		return FAILURE;
46 	}
47 
48 	if (error_handling != NULL) {
49 		zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, error_handling);
50 		*error_handling_replaced = 1;
51 	}
52 
53 	INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
54 	MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
55 
56 	/* Convert pattern (if specified) to UTF-16. */
57 	if(pattern && pattern_len) {
58 		intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
59 		INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16");
60 	} else {
61 		spattern_len = 0;
62 		spattern = NULL;
63 	}
64 
65 	if(locale_len == 0) {
66 		locale = intl_locale_get_default();
67 	}
68 
69 #ifdef MSG_FORMAT_QUOTE_APOS
70 	if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
71 		INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format");
72 	}
73 #endif
74 
75 	if ((mfo)->mf_data.orig_format) {
76 		msgformat_data_free(&mfo->mf_data);
77 	}
78 
79 	(mfo)->mf_data.orig_format = estrndup(pattern, pattern_len);
80 	(mfo)->mf_data.orig_format_len = pattern_len;
81 
82 	/* Create an ICU message formatter. */
83 	MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, &parse_error, &INTL_DATA_ERROR_CODE(mfo));
84 
85 	if(spattern) {
86 		efree(spattern);
87 	}
88 
89 	if (INTL_DATA_ERROR_CODE( mfo ) == U_PATTERN_SYNTAX_ERROR) {
90 		char *msg = NULL;
91 		smart_str parse_error_str;
92 		parse_error_str = intl_parse_error_to_string( &parse_error );
93 		spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
94 		smart_str_free( &parse_error_str );
95 
96 		intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) );
97 		intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 );
98 
99 		efree( msg );
100 		return FAILURE;
101 	}
102 
103 	INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed");
104 	return SUCCESS;
105 }
106 /* }}} */
107 
108 /* {{{ Create formatter. */
PHP_FUNCTION(msgfmt_create)109 PHP_FUNCTION( msgfmt_create )
110 {
111 	object_init_ex( return_value, MessageFormatter_ce_ptr );
112 	if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL, NULL) == FAILURE) {
113 		zval_ptr_dtor(return_value);
114 		RETURN_NULL();
115 	}
116 }
117 /* }}} */
118 
119 /* {{{ MessageFormatter object constructor. */
PHP_METHOD(MessageFormatter,__construct)120 PHP_METHOD( MessageFormatter, __construct )
121 {
122 	zend_error_handling error_handling;
123 	bool error_handling_replaced = 0;
124 
125 	return_value = ZEND_THIS;
126 	if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU,  &error_handling, &error_handling_replaced) == FAILURE) {
127 		if (!EG(exception)) {
128 			zend_string *err = intl_error_get_message(NULL);
129 			zend_throw_exception(IntlException_ce_ptr, ZSTR_VAL(err), intl_error_get_code(NULL));
130 			zend_string_release_ex(err, 0);
131 		}
132 	}
133 	if (error_handling_replaced) {
134 		zend_restore_error_handling(&error_handling);
135 	}
136 }
137 /* }}} */
138 
139 /* {{{ Get formatter's last error code. */
PHP_FUNCTION(msgfmt_get_error_code)140 PHP_FUNCTION( msgfmt_get_error_code )
141 {
142 	zval*                    object  = NULL;
143 	MessageFormatter_object*  mfo     = NULL;
144 
145 	/* Parse parameters. */
146 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
147 		&object, MessageFormatter_ce_ptr ) == FAILURE )
148 	{
149 		RETURN_THROWS();
150 	}
151 
152 	mfo = Z_INTL_MESSAGEFORMATTER_P( object );
153 
154 	/* Return formatter's last error code. */
155 	RETURN_LONG( INTL_DATA_ERROR_CODE(mfo) );
156 }
157 /* }}} */
158 
159 /* {{{ Get text description for formatter's last error code. */
PHP_FUNCTION(msgfmt_get_error_message)160 PHP_FUNCTION( msgfmt_get_error_message )
161 {
162 	zend_string*             message = NULL;
163 	zval*                    object  = NULL;
164 	MessageFormatter_object*  mfo     = NULL;
165 
166 	/* Parse parameters. */
167 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
168 		&object, MessageFormatter_ce_ptr ) == FAILURE )
169 	{
170 		RETURN_THROWS();
171 	}
172 
173 	mfo = Z_INTL_MESSAGEFORMATTER_P( object );
174 
175 	/* Return last error message. */
176 	message = intl_error_get_message( &mfo->mf_data.error );
177 	RETURN_STR(message);
178 }
179 /* }}} */
180