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