1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Stanislav Malyshev <stas@zend.com>                          |
14    +----------------------------------------------------------------------+
15  */
16 
17 #include <unicode/unum.h>
18 
19 #include "msgformat_class.h"
20 #include "php_intl.h"
21 #include "msgformat_data.h"
22 #include "msgformat_format.h"
23 #include "msgformat_parse.h"
24 #include "msgformat.h"
25 #include "msgformat_attr.h"
26 
27 #include <zend_exceptions.h>
28 
29 zend_class_entry *MessageFormatter_ce_ptr = NULL;
30 static zend_object_handlers MessageFormatter_handlers;
31 
32 /*
33  * Auxiliary functions needed by objects of 'MessageFormatter' class
34  */
35 
36 /* {{{ MessageFormatter_objects_dtor */
MessageFormatter_object_dtor(zend_object * object)37 static void MessageFormatter_object_dtor(zend_object *object )
38 {
39 	zend_objects_destroy_object( object );
40 }
41 /* }}} */
42 
43 /* {{{ MessageFormatter_objects_free */
MessageFormatter_object_free(zend_object * object)44 void MessageFormatter_object_free( zend_object *object )
45 {
46 	MessageFormatter_object* mfo = php_intl_messageformatter_fetch_object(object);
47 
48 	zend_object_std_dtor( &mfo->zo );
49 
50 	msgformat_data_free( &mfo->mf_data );
51 }
52 /* }}} */
53 
54 /* {{{ MessageFormatter_object_create */
MessageFormatter_object_create(zend_class_entry * ce)55 zend_object *MessageFormatter_object_create(zend_class_entry *ce)
56 {
57 	MessageFormatter_object*     intern;
58 
59 	intern = ecalloc( 1, sizeof(MessageFormatter_object) + zend_object_properties_size(ce));
60 	msgformat_data_init( &intern->mf_data );
61 	zend_object_std_init( &intern->zo, ce );
62 	object_properties_init(&intern->zo, ce);
63 
64 	intern->zo.handlers = &MessageFormatter_handlers;
65 
66 	return &intern->zo;
67 }
68 /* }}} */
69 
70 /* {{{ MessageFormatter_object_clone */
MessageFormatter_object_clone(zval * object)71 zend_object *MessageFormatter_object_clone(zval *object)
72 {
73 	MessageFormatter_object *mfo, *new_mfo;
74 	zend_object *new_obj;
75 
76 	MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
77 	new_obj = MessageFormatter_ce_ptr->create_object(Z_OBJCE_P(object));
78 	new_mfo = php_intl_messageformatter_fetch_object(new_obj);
79 	/* clone standard parts */
80 	zend_objects_clone_members(&new_mfo->zo, &mfo->zo);
81 
82 	/* clone formatter object */
83 	if (MSG_FORMAT_OBJECT(mfo) != NULL) {
84 		MSG_FORMAT_OBJECT(new_mfo) = umsg_clone(MSG_FORMAT_OBJECT(mfo),
85 				&INTL_DATA_ERROR_CODE(mfo));
86 
87 		if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
88 			intl_errors_set(INTL_DATA_ERROR_P(mfo), INTL_DATA_ERROR_CODE(mfo),
89 					"Failed to clone MessageFormatter object", 0);
90 			zend_throw_exception_ex(NULL, 0, "Failed to clone MessageFormatter object");
91 		}
92 	} else {
93 		zend_throw_exception_ex(NULL, 0, "Cannot clone unconstructed MessageFormatter");
94 	}
95 	return new_obj;
96 }
97 /* }}} */
98 
99 /*
100  * 'MessageFormatter' class registration structures & functions
101  */
102 
103 /* {{{ arginfo */
104 ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter___construct, 0, 0, 2)
105 	ZEND_ARG_INFO(0, locale)
106 	ZEND_ARG_INFO(0, pattern)
107 ZEND_END_ARG_INFO()
108 
109 ZEND_BEGIN_ARG_INFO(arginfo_messageformatter_geterrormessage, 0)
110 ZEND_END_ARG_INFO()
111 
112 ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_formatmessage, 0, 0, 3)
113 	ZEND_ARG_INFO(0, locale)
114 	ZEND_ARG_INFO(0, pattern)
115 	ZEND_ARG_INFO(0, args)
116 ZEND_END_ARG_INFO()
117 
118 ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_format, 0, 0, 1)
119 	ZEND_ARG_INFO(0, args)
120 ZEND_END_ARG_INFO()
121 
122 ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_setpattern, 0, 0, 1)
123 	ZEND_ARG_INFO(0, pattern)
124 ZEND_END_ARG_INFO()
125 
126 ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_parse, 0, 0, 1)
127 	ZEND_ARG_INFO(0, source)
128 ZEND_END_ARG_INFO()
129 /* }}} */
130 
131 /* {{{ MessageFormatter_class_functions
132  * Every 'MessageFormatter' class method has an entry in this table
133  */
134 static zend_function_entry MessageFormatter_class_functions[] = {
135 	PHP_ME( MessageFormatter, __construct, arginfo_messageformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
136 	ZEND_FENTRY(  create, ZEND_FN( msgfmt_create ), arginfo_messageformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
137 	PHP_NAMED_FE( format, ZEND_FN( msgfmt_format ), arginfo_messageformatter_format )
138 	ZEND_FENTRY(  formatMessage, ZEND_FN( msgfmt_format_message ), arginfo_messageformatter_formatmessage, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
139 	PHP_NAMED_FE( parse, ZEND_FN( msgfmt_parse ), arginfo_messageformatter_parse )
140 	ZEND_FENTRY(  parseMessage, ZEND_FN( msgfmt_parse_message ), arginfo_messageformatter_formatmessage, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
141 	PHP_NAMED_FE( setPattern, ZEND_FN( msgfmt_set_pattern ), arginfo_messageformatter_setpattern )
142 	PHP_NAMED_FE( getPattern, ZEND_FN( msgfmt_get_pattern ), arginfo_messageformatter_geterrormessage )
143 	PHP_NAMED_FE( getLocale, ZEND_FN( msgfmt_get_locale ), arginfo_messageformatter_geterrormessage )
144 	PHP_NAMED_FE( getErrorCode, ZEND_FN( msgfmt_get_error_code ), arginfo_messageformatter_geterrormessage )
145 	PHP_NAMED_FE( getErrorMessage, ZEND_FN( msgfmt_get_error_message ), arginfo_messageformatter_geterrormessage )
146 	PHP_FE_END
147 };
148 /* }}} */
149 
150 /* {{{ msgformat_register_class
151  * Initialize 'MessageFormatter' class
152  */
msgformat_register_class(void)153 void msgformat_register_class( void )
154 {
155 	zend_class_entry ce;
156 
157 	/* Create and register 'MessageFormatter' class. */
158 	INIT_CLASS_ENTRY( ce, "MessageFormatter", MessageFormatter_class_functions );
159 	ce.create_object = MessageFormatter_object_create;
160 	MessageFormatter_ce_ptr = zend_register_internal_class( &ce );
161 
162 	memcpy(&MessageFormatter_handlers, zend_get_std_object_handlers(),
163 		sizeof MessageFormatter_handlers);
164 	MessageFormatter_handlers.offset = XtOffsetOf(MessageFormatter_object, zo);
165 	MessageFormatter_handlers.clone_obj = MessageFormatter_object_clone;
166 	MessageFormatter_handlers.dtor_obj = MessageFormatter_object_dtor;
167 	MessageFormatter_handlers.free_obj = MessageFormatter_object_free;
168 
169 
170 	/* Declare 'MessageFormatter' class properties. */
171 	if( !MessageFormatter_ce_ptr )
172 	{
173 		zend_error(E_ERROR, "Failed to register MessageFormatter class");
174 		return;
175 	}
176 }
177 /* }}} */
178 
179 /*
180  * Local variables:
181  * tab-width: 4
182  * c-basic-offset: 4
183  * End:
184  * vim600: noet sw=4 ts=4 fdm=marker
185  * vim<600: noet sw=4 ts=4
186  */
187