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