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