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 | http://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)28 static int msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
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 INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
49 MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
50
51 /* Convert pattern (if specified) to UTF-16. */
52 if(pattern && pattern_len) {
53 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
54 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16");
55 } else {
56 spattern_len = 0;
57 spattern = NULL;
58 }
59
60 if(locale_len == 0) {
61 locale = intl_locale_get_default();
62 }
63
64 #ifdef MSG_FORMAT_QUOTE_APOS
65 if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
66 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format");
67 }
68 #endif
69
70 if ((mfo)->mf_data.orig_format) {
71 msgformat_data_free(&mfo->mf_data);
72 }
73
74 (mfo)->mf_data.orig_format = estrndup(pattern, pattern_len);
75 (mfo)->mf_data.orig_format_len = pattern_len;
76
77 /* Create an ICU message formatter. */
78 MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, &parse_error, &INTL_DATA_ERROR_CODE(mfo));
79
80 if(spattern) {
81 efree(spattern);
82 }
83
84 if (INTL_DATA_ERROR_CODE( mfo ) == U_PATTERN_SYNTAX_ERROR) {
85 char *msg = NULL;
86 smart_str parse_error_str;
87 parse_error_str = intl_parse_error_to_string( &parse_error );
88 spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
89 smart_str_free( &parse_error_str );
90
91 intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) );
92 intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 );
93
94 efree( msg );
95 return FAILURE;
96 }
97
98 INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed");
99 return SUCCESS;
100 }
101 /* }}} */
102
103 /* {{{ Create formatter. */
PHP_FUNCTION(msgfmt_create)104 PHP_FUNCTION( msgfmt_create )
105 {
106 object_init_ex( return_value, MessageFormatter_ce_ptr );
107 if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU) == FAILURE) {
108 zval_ptr_dtor(return_value);
109 RETURN_NULL();
110 }
111 }
112 /* }}} */
113
114 /* {{{ MessageFormatter object constructor. */
PHP_METHOD(MessageFormatter,__construct)115 PHP_METHOD( MessageFormatter, __construct )
116 {
117 zend_error_handling error_handling;
118
119 zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
120 return_value = ZEND_THIS;
121 if (msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU) == FAILURE) {
122 if (!EG(exception)) {
123 zend_string *err = intl_error_get_message(NULL);
124 zend_throw_exception(IntlException_ce_ptr, ZSTR_VAL(err), intl_error_get_code(NULL));
125 zend_string_release_ex(err, 0);
126 }
127 }
128 zend_restore_error_handling(&error_handling);
129 }
130 /* }}} */
131
132 /* {{{ Get formatter's last error code. */
PHP_FUNCTION(msgfmt_get_error_code)133 PHP_FUNCTION( msgfmt_get_error_code )
134 {
135 zval* object = NULL;
136 MessageFormatter_object* mfo = NULL;
137
138 /* Parse parameters. */
139 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
140 &object, MessageFormatter_ce_ptr ) == FAILURE )
141 {
142 RETURN_THROWS();
143 }
144
145 mfo = Z_INTL_MESSAGEFORMATTER_P( object );
146
147 /* Return formatter's last error code. */
148 RETURN_LONG( INTL_DATA_ERROR_CODE(mfo) );
149 }
150 /* }}} */
151
152 /* {{{ Get text description for formatter's last error code. */
PHP_FUNCTION(msgfmt_get_error_message)153 PHP_FUNCTION( msgfmt_get_error_message )
154 {
155 zend_string* message = NULL;
156 zval* object = NULL;
157 MessageFormatter_object* mfo = NULL;
158
159 /* Parse parameters. */
160 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
161 &object, MessageFormatter_ce_ptr ) == FAILURE )
162 {
163 RETURN_THROWS();
164 }
165
166 mfo = Z_INTL_MESSAGEFORMATTER_P( object );
167
168 /* Return last error message. */
169 message = intl_error_get_message( &mfo->mf_data.error );
170 RETURN_STR(message);
171 }
172 /* }}} */
173