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