xref: /PHP-7.3/ext/intl/formatter/formatter_main.c (revision 701437a9)
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 "formatter_class.h"
25 #include "intl_convert.h"
26 
27 /* {{{ */
numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS,zend_bool is_constructor)28 static int numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_constructor)
29 {
30 	const char* locale;
31 	char*       pattern = NULL;
32 	size_t      locale_len = 0, pattern_len = 0;
33 	zend_long   style;
34 	UChar*      spattern     = NULL;
35 	int32_t     spattern_len = 0;
36 	int         zpp_flags = is_constructor ? ZEND_PARSE_PARAMS_THROW : 0;
37 	FORMATTER_METHOD_INIT_VARS;
38 
39 	/* Parse parameters. */
40 	if( zend_parse_parameters_ex( zpp_flags, ZEND_NUM_ARGS(), "sl|s",
41 		&locale, &locale_len, &style, &pattern, &pattern_len ) == FAILURE )
42 	{
43 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
44 			"numfmt_create: unable to parse input parameters", 0 );
45 		return FAILURE;
46 	}
47 
48 	INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
49 	object = return_value;
50 	FORMATTER_METHOD_FETCH_OBJECT_NO_CHECK;
51 
52 	/* Convert pattern (if specified) to UTF-16. */
53 	if(pattern && pattern_len) {
54 		intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(nfo));
55 		INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: error converting pattern to UTF-16");
56 	}
57 
58 	if(locale_len == 0) {
59 		locale = intl_locale_get_default();
60 	}
61 
62 	/* Create an ICU number formatter. */
63 	FORMATTER_OBJECT(nfo) = unum_open(style, spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(nfo));
64 
65 	if(spattern) {
66 		efree(spattern);
67 	}
68 
69 	INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: number formatter creation failed");
70 	return SUCCESS;
71 }
72 /* }}} */
73 
74 /* {{{ proto NumberFormatter NumberFormatter::create( string $locale, int style[, string $pattern ] )
75  * Create number formatter. }}} */
76 /* {{{ proto NumberFormatter numfmt_create( string $locale, int style[, string $pattern ] )
77  * Create number formatter.
78  */
PHP_FUNCTION(numfmt_create)79 PHP_FUNCTION( numfmt_create )
80 {
81 	object_init_ex( return_value, NumberFormatter_ce_ptr );
82 	if (numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) == FAILURE) {
83 		zval_ptr_dtor(return_value);
84 		RETURN_NULL();
85 	}
86 }
87 /* }}} */
88 
89 /* {{{ proto NumberFormatter::__construct( string $locale, int style[, string $pattern ] )
90  * NumberFormatter object constructor.
91  */
PHP_METHOD(NumberFormatter,__construct)92 PHP_METHOD( NumberFormatter, __construct )
93 {
94 	zend_error_handling error_handling;
95 
96 	zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
97 	return_value = getThis();
98 	if (numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
99 		if (!EG(exception)) {
100 			zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
101 		}
102 	}
103 	zend_restore_error_handling(&error_handling);
104 }
105 /* }}} */
106 
107 /* {{{ proto int NumberFormatter::getErrorCode()
108  * Get formatter's last error code. }}} */
109 /* {{{ proto int numfmt_get_error_code( NumberFormatter $nf )
110  * Get formatter's last error code.
111  */
PHP_FUNCTION(numfmt_get_error_code)112 PHP_FUNCTION( numfmt_get_error_code )
113 {
114 	FORMATTER_METHOD_INIT_VARS
115 
116 	/* Parse parameters. */
117 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
118 		&object, NumberFormatter_ce_ptr ) == FAILURE )
119 	{
120 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
121 			"numfmt_get_error_code: unable to parse input params", 0 );
122 
123 		RETURN_FALSE;
124 	}
125 
126 	nfo = Z_INTL_NUMBERFORMATTER_P(object);
127 
128 	/* Return formatter's last error code. */
129 	RETURN_LONG( INTL_DATA_ERROR_CODE(nfo) );
130 }
131 /* }}} */
132 
133 /* {{{ proto string NumberFormatter::getErrorMessage( )
134  * Get text description for formatter's last error code. }}} */
135 /* {{{ proto string numfmt_get_error_message( NumberFormatter $nf )
136  * Get text description for formatter's last error code.
137  */
PHP_FUNCTION(numfmt_get_error_message)138 PHP_FUNCTION( numfmt_get_error_message )
139 {
140 	zend_string *message = NULL;
141 	FORMATTER_METHOD_INIT_VARS
142 
143 	/* Parse parameters. */
144 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
145 		&object, NumberFormatter_ce_ptr ) == FAILURE )
146 	{
147 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
148 			"numfmt_get_error_message: unable to parse input params", 0 );
149 
150 		RETURN_FALSE;
151 	}
152 
153 	nfo = Z_INTL_NUMBERFORMATTER_P(object);
154 
155 	/* Return last error message. */
156 	message = intl_error_get_message( INTL_DATA_ERROR_P(nfo) );
157 	RETURN_STR(message);
158 }
159 /* }}} */
160 
161 /*
162  * Local variables:
163  * tab-width: 4
164  * c-basic-offset: 4
165  * End:
166  * vim600: noet sw=4 ts=4 fdm=marker
167  * vim<600: noet sw=4 ts=4
168  */
169