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
23 #include "php_intl.h"
24 #include "msgformat_class.h"
25 #include "msgformat_format.h"
26 #include "msgformat_data.h"
27 #include "msgformat_helpers.h"
28 #include "intl_convert.h"
29
30 #ifndef Z_ADDREF_P
31 #define Z_ADDREF_P(z) ((z)->refcount++)
32 #endif
33
34 /* {{{ */
msgfmt_do_format(MessageFormatter_object * mfo,zval * args,zval * return_value)35 static void msgfmt_do_format(MessageFormatter_object *mfo, zval *args, zval *return_value)
36 {
37 UChar* formatted = NULL;
38 int32_t formatted_len = 0;
39
40 umsg_format_helper(mfo, Z_ARRVAL_P(args), &formatted, &formatted_len);
41
42 if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
43 if (formatted) {
44 efree(formatted);
45 }
46 RETURN_FALSE;
47 } else {
48 INTL_METHOD_RETVAL_UTF8(mfo, formatted, formatted_len, 1);
49 }
50 }
51 /* }}} */
52
53 /* {{{ proto mixed MessageFormatter::format( array $args )
54 * Format a message. }}} */
55 /* {{{ proto mixed msgfmt_format( MessageFormatter $nf, array $args )
56 * Format a message.
57 */
PHP_FUNCTION(msgfmt_format)58 PHP_FUNCTION( msgfmt_format )
59 {
60 zval *args;
61 MSG_FORMAT_METHOD_INIT_VARS;
62
63
64 /* Parse parameters. */
65 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Oa",
66 &object, MessageFormatter_ce_ptr, &args ) == FAILURE )
67 {
68 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
69 "msgfmt_format: unable to parse input params", 0 );
70
71 RETURN_FALSE;
72 }
73
74 /* Fetch the object. */
75 MSG_FORMAT_METHOD_FETCH_OBJECT;
76
77 msgfmt_do_format(mfo, args, return_value);
78 }
79 /* }}} */
80
81 /* {{{ proto mixed MessageFormatter::formatMessage( string $locale, string $pattern, array $args )
82 * Format a message. }}} */
83 /* {{{ proto mixed msgfmt_format_message( string $locale, string $pattern, array $args )
84 * Format a message.
85 */
PHP_FUNCTION(msgfmt_format_message)86 PHP_FUNCTION( msgfmt_format_message )
87 {
88 zval *args;
89 UChar *spattern = NULL;
90 int spattern_len = 0;
91 char *pattern = NULL;
92 size_t pattern_len = 0;
93 const char *slocale = NULL;
94 size_t slocale_len = 0;
95 MessageFormatter_object mf;
96 MessageFormatter_object *mfo = &mf;
97 UParseError parse_error;
98
99 /* Parse parameters. */
100 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "ssa",
101 &slocale, &slocale_len, &pattern, &pattern_len, &args ) == FAILURE )
102 {
103 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
104 "msgfmt_format_message: unable to parse input params", 0 );
105
106 RETURN_FALSE;
107 }
108
109 INTL_CHECK_LOCALE_LEN(slocale_len);
110
111 memset(mfo, 0, sizeof(*mfo));
112 msgformat_data_init(&mfo->mf_data);
113
114 if(pattern && pattern_len) {
115 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
116 if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
117 {
118 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
119 "msgfmt_format_message: error converting pattern to UTF-16", 0 );
120 RETURN_FALSE;
121 }
122 } else {
123 spattern_len = 0;
124 spattern = NULL;
125 }
126
127 if(slocale_len == 0) {
128 slocale = intl_locale_get_default();
129 }
130
131 #ifdef MSG_FORMAT_QUOTE_APOS
132 if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
133 intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
134 "msgfmt_format_message: error converting pattern to quote-friendly format", 0 );
135 RETURN_FALSE;
136 }
137 #endif
138
139 /* Create an ICU message formatter. */
140 MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, &parse_error, &INTL_DATA_ERROR_CODE(mfo));
141 if(spattern && spattern_len) {
142 efree(spattern);
143 }
144
145 if (INTL_DATA_ERROR_CODE( mfo ) == U_PATTERN_SYNTAX_ERROR) {
146 char *msg = NULL;
147 smart_str parse_error_str;
148 parse_error_str = intl_parse_error_to_string( &parse_error );
149 spprintf( &msg, 0, "pattern syntax error (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "unknown parser error" );
150 smart_str_free( &parse_error_str );
151
152 intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( mfo ) );
153 intl_errors_set_custom_msg( INTL_DATA_ERROR_P( mfo ), msg, 1 );
154
155 efree( msg );
156 RETURN_FALSE;
157 }
158
159 INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
160
161 msgfmt_do_format(mfo, args, return_value);
162
163 /* drop the temporary formatter */
164 msgformat_data_free(&mfo->mf_data);
165 }
166 /* }}} */
167