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
21 #include "php_intl.h"
22 #include "formatter_class.h"
23 #include "intl_convert.h"
24
25 /* {{{ */
numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS)26 static int numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
27 {
28 const char* locale;
29 char* pattern = NULL;
30 size_t locale_len = 0, pattern_len = 0;
31 zend_long style;
32 UChar* spattern = NULL;
33 int32_t spattern_len = 0;
34 FORMATTER_METHOD_INIT_VARS;
35
36 /* Parse parameters. */
37 if( zend_parse_parameters( ZEND_NUM_ARGS(), "sl|s!",
38 &locale, &locale_len, &style, &pattern, &pattern_len ) == FAILURE )
39 {
40 return FAILURE;
41 }
42
43 INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
44 object = return_value;
45 FORMATTER_METHOD_FETCH_OBJECT_NO_CHECK;
46 if (FORMATTER_OBJECT(nfo)) {
47 zend_throw_error(NULL, "NumberFormatter object is already constructed");
48 return FAILURE;
49 }
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(nfo));
54 INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: error converting pattern to UTF-16");
55 }
56
57 if(locale_len == 0) {
58 locale = intl_locale_get_default();
59 }
60
61 /* Create an ICU number formatter. */
62 FORMATTER_OBJECT(nfo) = unum_open(style, spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(nfo));
63
64 if(spattern) {
65 efree(spattern);
66 }
67
68 INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: number formatter creation failed");
69 return SUCCESS;
70 }
71 /* }}} */
72
73 /* {{{ Create number formatter. */
PHP_FUNCTION(numfmt_create)74 PHP_FUNCTION( numfmt_create )
75 {
76 object_init_ex( return_value, NumberFormatter_ce_ptr );
77 if (numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU) == FAILURE) {
78 zval_ptr_dtor(return_value);
79 RETURN_NULL();
80 }
81 }
82 /* }}} */
83
84 /* {{{ NumberFormatter object constructor. */
PHP_METHOD(NumberFormatter,__construct)85 PHP_METHOD( NumberFormatter, __construct )
86 {
87 zend_error_handling error_handling;
88
89 zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
90 return_value = ZEND_THIS;
91 if (numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU) == FAILURE) {
92 if (!EG(exception)) {
93 zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
94 }
95 }
96 zend_restore_error_handling(&error_handling);
97 }
98 /* }}} */
99
100 /* {{{ Get formatter's last error code. */
PHP_FUNCTION(numfmt_get_error_code)101 PHP_FUNCTION( numfmt_get_error_code )
102 {
103 FORMATTER_METHOD_INIT_VARS
104
105 /* Parse parameters. */
106 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
107 &object, NumberFormatter_ce_ptr ) == FAILURE )
108 {
109 RETURN_THROWS();
110 }
111
112 nfo = Z_INTL_NUMBERFORMATTER_P(object);
113
114 /* Return formatter's last error code. */
115 RETURN_LONG( INTL_DATA_ERROR_CODE(nfo) );
116 }
117 /* }}} */
118
119 /* {{{ Get text description for formatter's last error code. */
PHP_FUNCTION(numfmt_get_error_message)120 PHP_FUNCTION( numfmt_get_error_message )
121 {
122 zend_string *message = NULL;
123 FORMATTER_METHOD_INIT_VARS
124
125 /* Parse parameters. */
126 if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
127 &object, NumberFormatter_ce_ptr ) == FAILURE )
128 {
129 RETURN_THROWS();
130 }
131
132 nfo = Z_INTL_NUMBERFORMATTER_P(object);
133
134 /* Return last error message. */
135 message = intl_error_get_message( INTL_DATA_ERROR_P(nfo) );
136 RETURN_STR(message);
137 }
138 /* }}} */
139