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
98 /* Parse parameters. */
99 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "ssa",
100 &slocale, &slocale_len, &pattern, &pattern_len, &args ) == FAILURE )
101 {
102 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
103 "msgfmt_format_message: unable to parse input params", 0 );
104
105 RETURN_FALSE;
106 }
107
108 INTL_CHECK_LOCALE_LEN(slocale_len);
109
110 memset(mfo, 0, sizeof(*mfo));
111 msgformat_data_init(&mfo->mf_data);
112
113 if(pattern && pattern_len) {
114 intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
115 if( U_FAILURE(INTL_DATA_ERROR_CODE((mfo))) )
116 {
117 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
118 "msgfmt_format_message: error converting pattern to UTF-16", 0 );
119 RETURN_FALSE;
120 }
121 } else {
122 spattern_len = 0;
123 spattern = NULL;
124 }
125
126 if(slocale_len == 0) {
127 slocale = intl_locale_get_default();
128 }
129
130 #ifdef MSG_FORMAT_QUOTE_APOS
131 if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
132 intl_error_set( NULL, U_INVALID_FORMAT_ERROR,
133 "msgfmt_format_message: error converting pattern to quote-friendly format", 0 );
134 RETURN_FALSE;
135 }
136 #endif
137
138 /* Create an ICU message formatter. */
139 MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, slocale, NULL, &INTL_DATA_ERROR_CODE(mfo));
140 if(spattern && spattern_len) {
141 efree(spattern);
142 }
143 INTL_METHOD_CHECK_STATUS(mfo, "Creating message formatter failed");
144
145 msgfmt_do_format(mfo, args, return_value);
146
147 /* drop the temporary formatter */
148 msgformat_data_free(&mfo->mf_data);
149 }
150 /* }}} */
151
152 /*
153 * Local variables:
154 * tab-width: 4
155 * c-basic-offset: 4
156 * End:
157 * vim600: noet sw=4 ts=4 fdm=marker
158 * vim<600: noet sw=4 ts=4
159 */
160