1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
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 "intl_convert.h"
27 :
28 : /* {{{ */
29 : static void msgfmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
30 58 : {
31 : char* locale;
32 : char* pattern;
33 58 : int locale_len = 0, pattern_len = 0;
34 58 : UChar* spattern = NULL;
35 58 : int spattern_len = 0;
36 : zval* object;
37 : MessageFormatter_object* mfo;
38 58 : intl_error_reset( NULL TSRMLS_CC );
39 :
40 58 : object = return_value;
41 : /* Parse parameters. */
42 58 : if( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "ss",
43 : &locale, &locale_len, &pattern, &pattern_len ) == FAILURE )
44 : {
45 9 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
46 : "msgfmt_create: unable to parse input parameters", 0 TSRMLS_CC );
47 9 : zval_dtor(return_value);
48 9 : RETURN_NULL();
49 : }
50 :
51 49 : INTL_CHECK_LOCALE_LEN_OBJ(locale_len, return_value);
52 49 : MSG_FORMAT_METHOD_FETCH_OBJECT;
53 :
54 : /* Convert pattern (if specified) to UTF-16. */
55 92 : if(pattern && pattern_len) {
56 46 : intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(mfo));
57 46 : INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to UTF-16");
58 : } else {
59 3 : spattern_len = 0;
60 3 : spattern = NULL;
61 : }
62 :
63 46 : if(locale_len == 0) {
64 3 : locale = INTL_G(default_locale);
65 : }
66 :
67 46 : if(msgformat_fix_quotes(&spattern, &spattern_len, &INTL_DATA_ERROR_CODE(mfo)) != SUCCESS) {
68 0 : INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: error converting pattern to quote-friendly format");
69 : }
70 :
71 46 : (mfo)->mf_data.orig_format = estrndup(pattern, pattern_len);
72 46 : (mfo)->mf_data.orig_format_len = pattern_len;
73 :
74 : /* Create an ICU message formatter. */
75 46 : MSG_FORMAT_OBJECT(mfo) = umsg_open(spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(mfo));
76 :
77 46 : if(spattern) {
78 43 : efree(spattern);
79 : }
80 :
81 46 : INTL_CTOR_CHECK_STATUS(mfo, "msgfmt_create: message formatter creation failed");
82 : }
83 : /* }}} */
84 :
85 : /* {{{ proto MessageFormatter MesssageFormatter::create( string $locale, string $pattern )
86 : * Create formatter. }}} */
87 : /* {{{ proto MessageFormatter msgfmt_create( string $locale, string $pattern )
88 : * Create formatter.
89 : */
90 : PHP_FUNCTION( msgfmt_create )
91 50 : {
92 50 : object_init_ex( return_value, MessageFormatter_ce_ptr );
93 50 : msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
94 50 : }
95 : /* }}} */
96 :
97 : /* {{{ proto void MessageFormatter::__construct( string $locale, string $pattern )
98 : * MessageFormatter object constructor.
99 : */
100 : PHP_METHOD( MessageFormatter, __construct )
101 8 : {
102 8 : return_value = getThis();
103 8 : msgfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU);
104 8 : }
105 : /* }}} */
106 :
107 : /* {{{ proto int MessageFormatter::getErrorCode()
108 : * Get formatter's last error code. }}} */
109 : /* {{{ proto int msgfmt_get_error_code( MessageFormatter $nf )
110 : * Get formatter's last error code.
111 : */
112 : PHP_FUNCTION( msgfmt_get_error_code )
113 2 : {
114 2 : zval* object = NULL;
115 2 : MessageFormatter_object* mfo = NULL;
116 :
117 : /* Parse parameters. */
118 2 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
119 : &object, MessageFormatter_ce_ptr ) == FAILURE )
120 : {
121 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
122 : "msgfmt_get_error_code: unable to parse input params", 0 TSRMLS_CC );
123 :
124 0 : RETURN_FALSE;
125 : }
126 :
127 2 : mfo = (MessageFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
128 :
129 : /* Return formatter's last error code. */
130 2 : RETURN_LONG( INTL_DATA_ERROR_CODE(mfo) );
131 : }
132 : /* }}} */
133 :
134 : /* {{{ proto string MessageFormatter::getErrorMessage( )
135 : * Get text description for formatter's last error code. }}} */
136 : /* {{{ proto string msgfmt_get_error_message( MessageFormatter $coll )
137 : * Get text description for formatter's last error code.
138 : */
139 : PHP_FUNCTION( msgfmt_get_error_message )
140 2 : {
141 2 : char* message = NULL;
142 2 : zval* object = NULL;
143 2 : MessageFormatter_object* mfo = NULL;
144 :
145 : /* Parse parameters. */
146 2 : if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
147 : &object, MessageFormatter_ce_ptr ) == FAILURE )
148 : {
149 0 : intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
150 : "msgfmt_get_error_message: unable to parse input params", 0 TSRMLS_CC );
151 :
152 0 : RETURN_FALSE;
153 : }
154 :
155 2 : mfo = (MessageFormatter_object *) zend_object_store_get_object( object TSRMLS_CC );
156 :
157 : /* Return last error message. */
158 2 : message = intl_error_get_message( &mfo->mf_data.error TSRMLS_CC );
159 2 : RETURN_STRING( message, 0);
160 : }
161 : /* }}} */
162 :
163 : /*
164 : * Local variables:
165 : * tab-width: 4
166 : * c-basic-offset: 4
167 : * End:
168 : * vim600: noet sw=4 ts=4 fdm=marker
169 : * vim<600: noet sw=4 ts=4
170 : */
|