xref: /PHP-7.4/ext/intl/msgformat/msgformat.c (revision 74cf2eb8)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Stanislav Malyshev <stas@zend.com>                          |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include <unicode/ustring.h>
22 #include <unicode/umsg.h>
23 
24 #include "php_intl.h"
25 #include "msgformat_class.h"
26 #include "msgformat_data.h"
27 #include "intl_convert.h"
28 
29 /* {{{ */
msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS,zend_bool is_constructor)30 static int msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor)
31 {
32 	const char* locale;
33 	char*       pattern;
34 	size_t      locale_len = 0, pattern_len = 0;
35 	UChar*      spattern     = NULL;
36 	int         spattern_len = 0;
37 	zval*       object;
38 	MessageFormatter_object* mfo;
39 	UParseError parse_error;
40 	int zpp_flags = is_constructor ? ZEND_PARSE_PARAMS_THROW : 0;
41 	intl_error_reset( NULL );
42 
43 	object = return_value;
44 	/* Parse parameters. */
45 	if( zend_parse_parameters_ex( zpp_flags, ZEND_NUM_ARGS(), "ss",
46 		&locale, &locale_len, &pattern, &pattern_len ) == FAILURE )
47 	{
48 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
49 			"msgfmt_create: unable to parse input parameters", 0 );
50 		return FAILURE;
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 /* {{{ proto MessageFormatter MesssageFormatter::create( string $locale, string $pattern )
109  * Create formatter. }}} */
110 /* {{{ proto MessageFormatter msgfmt_create( string $locale, string $pattern )
111  * Create formatter.
112  */
PHP_FUNCTION(msgfmt_create)113 PHP_FUNCTION( msgfmt_create )
114 {
115 	object_init_ex( return_value, MessageFormatter_ce_ptr );
116 	if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) == FAILURE) {
117 		zval_ptr_dtor(return_value);
118 		RETURN_NULL();
119 	}
120 }
121 /* }}} */
122 
123 /* {{{ proto MessageFormatter::__construct( string $locale, string $pattern )
124  * MessageFormatter object constructor.
125  */
PHP_METHOD(MessageFormatter,__construct)126 PHP_METHOD( MessageFormatter, __construct )
127 {
128 	zend_error_handling error_handling;
129 
130 	zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
131 	return_value = ZEND_THIS;
132 	if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
133 		if (!EG(exception)) {
134 			zend_string *err = intl_error_get_message(NULL);
135 			zend_throw_exception(IntlException_ce_ptr, ZSTR_VAL(err), intl_error_get_code(NULL));
136 			zend_string_release_ex(err, 0);
137 		}
138 	}
139 	zend_restore_error_handling(&error_handling);
140 }
141 /* }}} */
142 
143 /* {{{ proto int MessageFormatter::getErrorCode()
144  * Get formatter's last error code. }}} */
145 /* {{{ proto int msgfmt_get_error_code( MessageFormatter $nf )
146  * Get formatter's last error code.
147  */
PHP_FUNCTION(msgfmt_get_error_code)148 PHP_FUNCTION( msgfmt_get_error_code )
149 {
150 	zval*                    object  = NULL;
151 	MessageFormatter_object*  mfo     = NULL;
152 
153 	/* Parse parameters. */
154 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
155 		&object, MessageFormatter_ce_ptr ) == FAILURE )
156 	{
157 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
158 			"msgfmt_get_error_code: unable to parse input params", 0 );
159 
160 		RETURN_FALSE;
161 	}
162 
163 	mfo = Z_INTL_MESSAGEFORMATTER_P( object );
164 
165 	/* Return formatter's last error code. */
166 	RETURN_LONG( INTL_DATA_ERROR_CODE(mfo) );
167 }
168 /* }}} */
169 
170 /* {{{ proto string MessageFormatter::getErrorMessage( )
171  * Get text description for formatter's last error code. }}} */
172 /* {{{ proto string msgfmt_get_error_message( MessageFormatter $coll )
173  * Get text description for formatter's last error code.
174  */
PHP_FUNCTION(msgfmt_get_error_message)175 PHP_FUNCTION( msgfmt_get_error_message )
176 {
177 	zend_string*             message = NULL;
178 	zval*                    object  = NULL;
179 	MessageFormatter_object*  mfo     = NULL;
180 
181 	/* Parse parameters. */
182 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
183 		&object, MessageFormatter_ce_ptr ) == FAILURE )
184 	{
185 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
186 			"msgfmt_get_error_message: unable to parse input params", 0 );
187 
188 		RETURN_FALSE;
189 	}
190 
191 	mfo = Z_INTL_MESSAGEFORMATTER_P( object );
192 
193 	/* Return last error message. */
194 	message = intl_error_get_message( &mfo->mf_data.error );
195 	RETURN_STR(message);
196 }
197 /* }}} */
198