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 #include <unicode/unum.h>
18 
19 #include "formatter_class.h"
20 #include "php_intl.h"
21 #include "formatter_data.h"
22 #include "formatter_format.h"
23 #include "formatter_parse.h"
24 #include "formatter_main.h"
25 #include "formatter_attr.h"
26 
27 #include <zend_exceptions.h>
28 
29 zend_class_entry *NumberFormatter_ce_ptr = NULL;
30 static zend_object_handlers NumberFormatter_handlers;
31 
32 /*
33  * Auxiliary functions needed by objects of 'NumberFormatter' class
34  */
35 
36 /* {{{ NumberFormatter_objects_dtor */
NumberFormatter_object_dtor(void * object,zend_object_handle handle TSRMLS_DC)37 static void NumberFormatter_object_dtor(
38 	void *object,
39 	zend_object_handle handle TSRMLS_DC )
40 {
41 	zend_objects_destroy_object( object, handle TSRMLS_CC );
42 }
43 /* }}} */
44 
45 /* {{{ NumberFormatter_objects_free */
NumberFormatter_object_free(zend_object * object TSRMLS_DC)46 void NumberFormatter_object_free( zend_object *object TSRMLS_DC )
47 {
48 	NumberFormatter_object* nfo = (NumberFormatter_object*)object;
49 
50 	zend_object_std_dtor( &nfo->zo TSRMLS_CC );
51 
52 	formatter_data_free( &nfo->nf_data TSRMLS_CC );
53 
54 	efree( nfo );
55 }
56 /* }}} */
57 
58 /* {{{ NumberFormatter_object_create */
NumberFormatter_object_create(zend_class_entry * ce TSRMLS_DC)59 zend_object_value NumberFormatter_object_create(zend_class_entry *ce TSRMLS_DC)
60 {
61 	zend_object_value    retval;
62 	NumberFormatter_object*     intern;
63 
64 	intern = ecalloc( 1, sizeof(NumberFormatter_object) );
65 	formatter_data_init( &intern->nf_data TSRMLS_CC );
66 	zend_object_std_init( &intern->zo, ce TSRMLS_CC );
67 
68 	retval.handle = zend_objects_store_put(
69 		intern,
70 		NumberFormatter_object_dtor,
71 		(zend_objects_free_object_storage_t)NumberFormatter_object_free,
72 		NULL TSRMLS_CC );
73 
74 	retval.handlers = &NumberFormatter_handlers;
75 
76 	return retval;
77 }
78 /* }}} */
79 
80 /* {{{ NumberFormatter_object_clone */
NumberFormatter_object_clone(zval * object TSRMLS_DC)81 zend_object_value NumberFormatter_object_clone(zval *object TSRMLS_DC)
82 {
83 	zend_object_value new_obj_val;
84 	zend_object_handle handle = Z_OBJ_HANDLE_P(object);
85 	NumberFormatter_object *nfo, *new_nfo;
86 
87 	FORMATTER_METHOD_FETCH_OBJECT_NO_CHECK;
88 	new_obj_val = NumberFormatter_ce_ptr->create_object(Z_OBJCE_P(object) TSRMLS_CC);
89 	new_nfo = (NumberFormatter_object *)zend_object_store_get_object_by_handle(new_obj_val.handle TSRMLS_CC);
90 	/* clone standard parts */
91 	zend_objects_clone_members(&new_nfo->zo, new_obj_val, &nfo->zo, handle TSRMLS_CC);
92 	/* clone formatter object. It may fail, the destruction code must handle this case */
93 	if (FORMATTER_OBJECT(nfo) != NULL) {
94 		FORMATTER_OBJECT(new_nfo) = unum_clone(FORMATTER_OBJECT(nfo),
95 				&INTL_DATA_ERROR_CODE(nfo));
96 		if (U_FAILURE(INTL_DATA_ERROR_CODE(nfo))) {
97 			/* set up error in case error handler is interested */
98 			intl_errors_set(INTL_DATA_ERROR_P(nfo), INTL_DATA_ERROR_CODE(nfo),
99 					"Failed to clone NumberFormatter object", 0 TSRMLS_CC);
100 			zend_throw_exception(NULL, "Failed to clone NumberFormatter object", 0 TSRMLS_CC);
101 		}
102 	} else {
103 		zend_throw_exception(NULL, "Cannot clone unconstructed NumberFormatter", 0 TSRMLS_CC);
104 	}
105 	return new_obj_val;
106 }
107 /* }}} */
108 
109 /*
110  * 'NumberFormatter' class registration structures & functions
111  */
112 
113 /* {{{ arginfo */
114 ZEND_BEGIN_ARG_INFO_EX(number_parse_arginfo, 0, 0, 1)
115 	ZEND_ARG_INFO(0, string)
116 	ZEND_ARG_INFO(0, type)
117 	ZEND_ARG_INFO(1, position)
118 ZEND_END_ARG_INFO()
119 
120 ZEND_BEGIN_ARG_INFO_EX(number_parse_currency_arginfo, 0, 0, 2)
121 	ZEND_ARG_INFO(0, string)
122 	ZEND_ARG_INFO(1, currency)
123 	ZEND_ARG_INFO(1, position)
124 ZEND_END_ARG_INFO()
125 
126 ZEND_BEGIN_ARG_INFO_EX(arginfo_numberformatter_getattribute, 0, 0, 1)
127 	ZEND_ARG_INFO(0, attr)
128 ZEND_END_ARG_INFO()
129 
130 ZEND_BEGIN_ARG_INFO_EX(arginfo_numberformatter_setattribute, 0, 0, 2)
131 	ZEND_ARG_INFO(0, attr)
132 	ZEND_ARG_INFO(0, value)
133 ZEND_END_ARG_INFO()
134 
135 ZEND_BEGIN_ARG_INFO_EX(arginfo_numberformatter_setsymbol, 0, 0, 2)
136 	ZEND_ARG_INFO(0, attr)
137 	ZEND_ARG_INFO(0, symbol)
138 ZEND_END_ARG_INFO()
139 
140 ZEND_BEGIN_ARG_INFO(arginfo_numberformatter_getpattern, 0)
141 ZEND_END_ARG_INFO()
142 
143 ZEND_BEGIN_ARG_INFO_EX(arginfo_numberformatter_setpattern, 0, 0, 1)
144 	ZEND_ARG_INFO(0, pattern)
145 ZEND_END_ARG_INFO()
146 
147 ZEND_BEGIN_ARG_INFO_EX(arginfo_numberformatter_getlocale, 0, 0, 0)
148 	ZEND_ARG_INFO(0, type)
149 ZEND_END_ARG_INFO()
150 
151 ZEND_BEGIN_ARG_INFO_EX(arginfo_numberformatter___construct, 0, 0, 2)
152 	ZEND_ARG_INFO(0, locale)
153 	ZEND_ARG_INFO(0, style)
154 	ZEND_ARG_INFO(0, pattern)
155 ZEND_END_ARG_INFO()
156 
157 ZEND_BEGIN_ARG_INFO_EX(arginfo_numberformatter_formatcurrency, 0, 0, 2)
158 	ZEND_ARG_INFO(0, num)
159 	ZEND_ARG_INFO(0, currency)
160 ZEND_END_ARG_INFO()
161 
162 ZEND_BEGIN_ARG_INFO_EX(arginfo_numberformatter_format, 0, 0, 1)
163 	ZEND_ARG_INFO(0, num)
164 	ZEND_ARG_INFO(0, type)
165 ZEND_END_ARG_INFO()
166 /* }}} */
167 
168 /* {{{ NumberFormatter_class_functions
169  * Every 'NumberFormatter' class method has an entry in this table
170  */
171 static function_entry NumberFormatter_class_functions[] = {
172 	PHP_ME( NumberFormatter, __construct, arginfo_numberformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
173 	ZEND_FENTRY( create, ZEND_FN( numfmt_create ), arginfo_numberformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
174 	PHP_NAMED_FE( format, ZEND_FN( numfmt_format ), arginfo_numberformatter_format )
175 	PHP_NAMED_FE( parse, ZEND_FN( numfmt_parse ), number_parse_arginfo )
176 	PHP_NAMED_FE( formatCurrency, ZEND_FN( numfmt_format_currency ), arginfo_numberformatter_formatcurrency )
177 	PHP_NAMED_FE( parseCurrency, ZEND_FN( numfmt_parse_currency ), number_parse_currency_arginfo )
178 	PHP_NAMED_FE( setAttribute, ZEND_FN( numfmt_set_attribute ), arginfo_numberformatter_setattribute )
179 	PHP_NAMED_FE( getAttribute, ZEND_FN( numfmt_get_attribute ), arginfo_numberformatter_getattribute )
180 	PHP_NAMED_FE( setTextAttribute, ZEND_FN( numfmt_set_text_attribute ), arginfo_numberformatter_setattribute )
181 	PHP_NAMED_FE( getTextAttribute, ZEND_FN( numfmt_get_text_attribute ), arginfo_numberformatter_getattribute )
182 	PHP_NAMED_FE( setSymbol, ZEND_FN( numfmt_set_symbol ), arginfo_numberformatter_setsymbol )
183 	PHP_NAMED_FE( getSymbol, ZEND_FN( numfmt_get_symbol ), arginfo_numberformatter_getattribute )
184 	PHP_NAMED_FE( setPattern, ZEND_FN( numfmt_set_pattern ), arginfo_numberformatter_setpattern )
185 	PHP_NAMED_FE( getPattern, ZEND_FN( numfmt_get_pattern ), arginfo_numberformatter_getpattern )
186 	PHP_NAMED_FE( getLocale, ZEND_FN( numfmt_get_locale ), arginfo_numberformatter_getlocale )
187 	PHP_NAMED_FE( getErrorCode, ZEND_FN( numfmt_get_error_code ), arginfo_numberformatter_getpattern )
188 	PHP_NAMED_FE( getErrorMessage, ZEND_FN( numfmt_get_error_message ), arginfo_numberformatter_getpattern )
189 	PHP_FE_END
190 };
191 /* }}} */
192 
193 /* {{{ formatter_register_class
194  * Initialize 'NumberFormatter' class
195  */
formatter_register_class(TSRMLS_D)196 void formatter_register_class( TSRMLS_D )
197 {
198 	zend_class_entry ce;
199 
200 	/* Create and register 'NumberFormatter' class. */
201 	INIT_CLASS_ENTRY( ce, "NumberFormatter", NumberFormatter_class_functions );
202 	ce.create_object = NumberFormatter_object_create;
203 	NumberFormatter_ce_ptr = zend_register_internal_class( &ce TSRMLS_CC );
204 
205 	memcpy(&NumberFormatter_handlers, zend_get_std_object_handlers(),
206 		sizeof(NumberFormatter_handlers));
207 	NumberFormatter_handlers.clone_obj = NumberFormatter_object_clone;
208 
209 	/* Declare 'NumberFormatter' class properties. */
210 	if( !NumberFormatter_ce_ptr )
211 	{
212 		zend_error(E_ERROR, "Failed to register NumberFormatter class");
213 		return;
214 	}
215 }
216 /* }}} */
217 
218 /*
219  * Local variables:
220  * tab-width: 4
221  * c-basic-offset: 4
222  * End:
223  * vim600: noet sw=4 ts=4 fdm=marker
224  * vim<600: noet sw=4 ts=4
225  */
226