xref: /PHP-8.1/ext/intl/formatter/formatter_main.c (revision 925a3097)
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    | https://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,zend_error_handling * error_handling,bool * error_handling_replaced)26 static int numfmt_ctor(INTERNAL_FUNCTION_PARAMETERS, zend_error_handling *error_handling, bool *error_handling_replaced)
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 	if (error_handling != NULL) {
44 		zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, error_handling);
45 		*error_handling_replaced = 1;
46 	}
47 
48 	INTL_CHECK_LOCALE_LEN_OR_FAILURE(locale_len);
49 	object = return_value;
50 	FORMATTER_METHOD_FETCH_OBJECT_NO_CHECK;
51 	if (FORMATTER_OBJECT(nfo)) {
52 		zend_throw_error(NULL, "NumberFormatter object is already constructed");
53 		return FAILURE;
54 	}
55 
56 	/* Convert pattern (if specified) to UTF-16. */
57 	if(pattern && pattern_len) {
58 		intl_convert_utf8_to_utf16(&spattern, &spattern_len, pattern, pattern_len, &INTL_DATA_ERROR_CODE(nfo));
59 		INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: error converting pattern to UTF-16");
60 	}
61 
62 	if(locale_len == 0) {
63 		locale = intl_locale_get_default();
64 	}
65 
66 	/* Create an ICU number formatter. */
67 	FORMATTER_OBJECT(nfo) = unum_open(style, spattern, spattern_len, locale, NULL, &INTL_DATA_ERROR_CODE(nfo));
68 
69 	if(spattern) {
70 		efree(spattern);
71 	}
72 
73 	INTL_CTOR_CHECK_STATUS(nfo, "numfmt_create: number formatter creation failed");
74 	return SUCCESS;
75 }
76 /* }}} */
77 
78 /* {{{ Create number formatter. */
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, NULL, NULL) == FAILURE) {
83 		zval_ptr_dtor(return_value);
84 		RETURN_NULL();
85 	}
86 }
87 /* }}} */
88 
89 /* {{{ NumberFormatter object constructor. */
PHP_METHOD(NumberFormatter,__construct)90 PHP_METHOD( NumberFormatter, __construct )
91 {
92 	zend_error_handling error_handling;
93 	bool error_handling_replaced = 0;
94 
95 	return_value = ZEND_THIS;
96 	if (numfmt_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, &error_handling, &error_handling_replaced) == FAILURE) {
97 		if (!EG(exception)) {
98 			zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);
99 		}
100 	}
101 	if (error_handling_replaced) {
102 		zend_restore_error_handling(&error_handling);
103 	}
104 }
105 /* }}} */
106 
107 /* {{{ Get formatter's last error code. */
PHP_FUNCTION(numfmt_get_error_code)108 PHP_FUNCTION( numfmt_get_error_code )
109 {
110 	FORMATTER_METHOD_INIT_VARS
111 
112 	/* Parse parameters. */
113 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
114 		&object, NumberFormatter_ce_ptr ) == FAILURE )
115 	{
116 		RETURN_THROWS();
117 	}
118 
119 	nfo = Z_INTL_NUMBERFORMATTER_P(object);
120 
121 	/* Return formatter's last error code. */
122 	RETURN_LONG( INTL_DATA_ERROR_CODE(nfo) );
123 }
124 /* }}} */
125 
126 /* {{{ Get text description for formatter's last error code. */
PHP_FUNCTION(numfmt_get_error_message)127 PHP_FUNCTION( numfmt_get_error_message )
128 {
129 	zend_string *message = NULL;
130 	FORMATTER_METHOD_INIT_VARS
131 
132 	/* Parse parameters. */
133 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "O",
134 		&object, NumberFormatter_ce_ptr ) == FAILURE )
135 	{
136 		RETURN_THROWS();
137 	}
138 
139 	nfo = Z_INTL_NUMBERFORMATTER_P(object);
140 
141 	/* Return last error message. */
142 	message = intl_error_get_message( INTL_DATA_ERROR_P(nfo) );
143 	RETURN_STR(message);
144 }
145 /* }}} */
146