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 #include <unicode/unum.h>
16
17 #include "formatter_class.h"
18 #include "php_intl.h"
19 #include "formatter_data.h"
20 #include "formatter_format.h"
21 #include "formatter_arginfo.h"
22
23 #include <zend_exceptions.h>
24 #include "Zend/zend_interfaces.h"
25
26 zend_class_entry *NumberFormatter_ce_ptr = NULL;
27 static zend_object_handlers NumberFormatter_handlers;
28
29 /*
30 * Auxiliary functions needed by objects of 'NumberFormatter' class
31 */
32
33 /* {{{ NumberFormatter_objects_free */
NumberFormatter_object_free(zend_object * object)34 void NumberFormatter_object_free( zend_object *object )
35 {
36 NumberFormatter_object* nfo = php_intl_number_format_fetch_object(object);
37
38 zend_object_std_dtor( &nfo->zo );
39
40 formatter_data_free( &nfo->nf_data );
41 }
42 /* }}} */
43
44 /* {{{ NumberFormatter_object_create */
NumberFormatter_object_create(zend_class_entry * ce)45 zend_object *NumberFormatter_object_create(zend_class_entry *ce)
46 {
47 NumberFormatter_object* intern;
48
49 intern = zend_object_alloc(sizeof(NumberFormatter_object), ce);
50 formatter_data_init( &intern->nf_data );
51 zend_object_std_init( &intern->zo, ce );
52 object_properties_init(&intern->zo, ce);
53
54 return &intern->zo;
55 }
56 /* }}} */
57
58 /* {{{ NumberFormatter_object_clone */
NumberFormatter_object_clone(zend_object * object)59 zend_object *NumberFormatter_object_clone(zend_object *object)
60 {
61 NumberFormatter_object *nfo = php_intl_number_format_fetch_object(object);
62 zend_object *new_obj = NumberFormatter_ce_ptr->create_object(object->ce);
63 NumberFormatter_object *new_nfo = php_intl_number_format_fetch_object(new_obj);
64
65 /* clone standard parts */
66 zend_objects_clone_members(&new_nfo->zo, &nfo->zo);
67
68 /* clone formatter object. It may fail, the destruction code must handle this case */
69 if (FORMATTER_OBJECT(nfo) != NULL) {
70 UErrorCode error = U_ZERO_ERROR;
71 FORMATTER_OBJECT(new_nfo) = unum_clone(FORMATTER_OBJECT(nfo), &error);
72 if (U_FAILURE(error)) {
73 zend_throw_error(NULL, "Failed to clone NumberFormatter");
74 }
75 } else {
76 zend_throw_error(NULL, "Cannot clone uninitialized NumberFormatter");
77 }
78 return new_obj;
79 }
80 /* }}} */
81
82 /*
83 * 'NumberFormatter' class registration structures & functions
84 */
85
86 /* {{{ formatter_register_class
87 * Initialize 'NumberFormatter' class
88 */
formatter_register_class(void)89 void formatter_register_class( void )
90 {
91 /* Create and register 'NumberFormatter' class. */
92 NumberFormatter_ce_ptr = register_class_NumberFormatter();
93 NumberFormatter_ce_ptr->create_object = NumberFormatter_object_create;
94 NumberFormatter_ce_ptr->default_object_handlers = &NumberFormatter_handlers;
95
96 memcpy(&NumberFormatter_handlers, &std_object_handlers,
97 sizeof(NumberFormatter_handlers));
98 NumberFormatter_handlers.offset = XtOffsetOf(NumberFormatter_object, zo);
99 NumberFormatter_handlers.clone_obj = NumberFormatter_object_clone;
100 NumberFormatter_handlers.free_obj = NumberFormatter_object_free;
101 }
102 /* }}} */
103