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