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