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 "msgformat_class.h"
18 #include "php_intl.h"
19 #include "msgformat_data.h"
20 #include "msgformat_arginfo.h"
21 
22 #include <zend_exceptions.h>
23 
24 zend_class_entry *MessageFormatter_ce_ptr = NULL;
25 static zend_object_handlers MessageFormatter_handlers;
26 
27 /*
28  * Auxiliary functions needed by objects of 'MessageFormatter' class
29  */
30 
31 /* {{{ MessageFormatter_objects_free */
MessageFormatter_object_free(zend_object * object)32 void MessageFormatter_object_free( zend_object *object )
33 {
34 	MessageFormatter_object* mfo = php_intl_messageformatter_fetch_object(object);
35 
36 	zend_object_std_dtor( &mfo->zo );
37 
38 	msgformat_data_free( &mfo->mf_data );
39 }
40 /* }}} */
41 
42 /* {{{ MessageFormatter_object_create */
MessageFormatter_object_create(zend_class_entry * ce)43 zend_object *MessageFormatter_object_create(zend_class_entry *ce)
44 {
45 	MessageFormatter_object*     intern;
46 
47 	intern = zend_object_alloc(sizeof(MessageFormatter_object), ce);
48 	msgformat_data_init( &intern->mf_data );
49 	zend_object_std_init( &intern->zo, ce );
50 	object_properties_init(&intern->zo, ce);
51 
52 	return &intern->zo;
53 }
54 /* }}} */
55 
56 /* {{{ MessageFormatter_object_clone */
MessageFormatter_object_clone(zend_object * object)57 zend_object *MessageFormatter_object_clone(zend_object *object)
58 {
59 	MessageFormatter_object     *mfo = php_intl_messageformatter_fetch_object(object);
60 	zend_object             *new_obj = MessageFormatter_ce_ptr->create_object(object->ce);
61 	MessageFormatter_object *new_mfo = php_intl_messageformatter_fetch_object(new_obj);
62 
63 	/* clone standard parts */
64 	zend_objects_clone_members(&new_mfo->zo, &mfo->zo);
65 
66 	/* clone formatter object */
67 	if (MSG_FORMAT_OBJECT(mfo) != NULL) {
68 		UErrorCode error = U_ZERO_ERROR;
69 		MSG_FORMAT_OBJECT(new_mfo) = umsg_clone(MSG_FORMAT_OBJECT(mfo), &error);
70 
71 		if (U_FAILURE(error)) {
72 			zend_throw_error(NULL, "Failed to clone MessageFormatter");
73 		}
74 	} else {
75 		zend_throw_error(NULL, "Cannot clone uninitialized MessageFormatter");
76 	}
77 	return new_obj;
78 }
79 /* }}} */
80 
81 /*
82  * 'MessageFormatter' class registration structures & functions
83  */
84 
85 /* {{{ msgformat_register_class
86  * Initialize 'MessageFormatter' class
87  */
msgformat_register_class(void)88 void msgformat_register_class( void )
89 {
90 	/* Create and register 'MessageFormatter' class. */
91 	MessageFormatter_ce_ptr = register_class_MessageFormatter();
92 	MessageFormatter_ce_ptr->create_object = MessageFormatter_object_create;
93 	MessageFormatter_ce_ptr->default_object_handlers = &MessageFormatter_handlers;
94 
95 	memcpy(&MessageFormatter_handlers, &std_object_handlers,
96 		sizeof MessageFormatter_handlers);
97 	MessageFormatter_handlers.offset = XtOffsetOf(MessageFormatter_object, zo);
98 	MessageFormatter_handlers.clone_obj = MessageFormatter_object_clone;
99 	MessageFormatter_handlers.free_obj = MessageFormatter_object_free;
100 }
101 /* }}} */
102