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, *new_nfo;
62 	zend_object *new_obj;
63 
64 	nfo = php_intl_number_format_fetch_object(object);
65 	intl_error_reset(INTL_DATA_ERROR_P(nfo));
66 
67 	new_obj = NumberFormatter_ce_ptr->create_object(object->ce);
68 	new_nfo = php_intl_number_format_fetch_object(new_obj);
69 	/* clone standard parts */
70 	zend_objects_clone_members(&new_nfo->zo, &nfo->zo);
71 	/* clone formatter object. It may fail, the destruction code must handle this case */
72 	if (FORMATTER_OBJECT(nfo) != NULL) {
73 		FORMATTER_OBJECT(new_nfo) = unum_clone(FORMATTER_OBJECT(nfo),
74 				&INTL_DATA_ERROR_CODE(nfo));
75 		if (U_FAILURE(INTL_DATA_ERROR_CODE(nfo))) {
76 			/* set up error in case error handler is interested */
77 			intl_errors_set(INTL_DATA_ERROR_P(nfo), INTL_DATA_ERROR_CODE(nfo),
78 					"Failed to clone NumberFormatter object", 0);
79 			zend_throw_exception(NULL, "Failed to clone NumberFormatter object", 0);
80 		}
81 	} else {
82 		zend_throw_exception(NULL, "Cannot clone unconstructed NumberFormatter", 0);
83 	}
84 	return new_obj;
85 }
86 /* }}} */
87 
88 /*
89  * 'NumberFormatter' class registration structures & functions
90  */
91 
92 /* {{{ formatter_register_class
93  * Initialize 'NumberFormatter' class
94  */
formatter_register_class(void)95 void formatter_register_class( void )
96 {
97 	/* Create and register 'NumberFormatter' class. */
98 	NumberFormatter_ce_ptr = register_class_NumberFormatter();
99 	NumberFormatter_ce_ptr->create_object = NumberFormatter_object_create;
100 	NumberFormatter_ce_ptr->default_object_handlers = &NumberFormatter_handlers;
101 
102 	memcpy(&NumberFormatter_handlers, &std_object_handlers,
103 		sizeof(NumberFormatter_handlers));
104 	NumberFormatter_handlers.offset = XtOffsetOf(NumberFormatter_object, zo);
105 	NumberFormatter_handlers.clone_obj = NumberFormatter_object_clone;
106 	NumberFormatter_handlers.free_obj = NumberFormatter_object_free;
107 }
108 /* }}} */
109