1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
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(void * object,zend_object_handle handle TSRMLS_DC)37 static void MessageFormatter_object_dtor(void *object, zend_object_handle handle TSRMLS_DC )
38 {
39 	zend_objects_destroy_object( object, handle TSRMLS_CC );
40 }
41 /* }}} */
42 
43 /* {{{ MessageFormatter_objects_free */
MessageFormatter_object_free(zend_object * object TSRMLS_DC)44 void MessageFormatter_object_free( zend_object *object TSRMLS_DC )
45 {
46 	MessageFormatter_object* mfo = (MessageFormatter_object*)object;
47 
48 	zend_object_std_dtor( &mfo->zo TSRMLS_CC );
49 
50 	msgformat_data_free( &mfo->mf_data TSRMLS_CC );
51 
52 	efree( mfo );
53 }
54 /* }}} */
55 
56 /* {{{ MessageFormatter_object_create */
MessageFormatter_object_create(zend_class_entry * ce TSRMLS_DC)57 zend_object_value MessageFormatter_object_create(zend_class_entry *ce TSRMLS_DC)
58 {
59 	zend_object_value    retval;
60 	MessageFormatter_object*     intern;
61 
62 	intern = ecalloc( 1, sizeof(MessageFormatter_object) );
63 	msgformat_data_init( &intern->mf_data TSRMLS_CC );
64 	zend_object_std_init( &intern->zo, ce TSRMLS_CC );
65 	object_properties_init(&intern->zo, ce);
66 
67 	retval.handle = zend_objects_store_put(
68 		intern,
69 		MessageFormatter_object_dtor,
70 		(zend_objects_free_object_storage_t)MessageFormatter_object_free,
71 		NULL TSRMLS_CC );
72 
73 	retval.handlers = &MessageFormatter_handlers;
74 
75 	return retval;
76 }
77 /* }}} */
78 
79 /* {{{ MessageFormatter_object_clone */
MessageFormatter_object_clone(zval * object TSRMLS_DC)80 zend_object_value MessageFormatter_object_clone(zval *object TSRMLS_DC)
81 {
82 	zend_object_value new_obj_val;
83 	zend_object_handle handle = Z_OBJ_HANDLE_P(object);
84 	MessageFormatter_object *mfo, *new_mfo;
85 
86 	MSG_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
87 	new_obj_val = MessageFormatter_ce_ptr->create_object(Z_OBJCE_P(object) TSRMLS_CC);
88 	new_mfo = (MessageFormatter_object *)zend_object_store_get_object_by_handle(new_obj_val.handle TSRMLS_CC);
89 	/* clone standard parts */
90 	zend_objects_clone_members(&new_mfo->zo, new_obj_val, &mfo->zo, handle TSRMLS_CC);
91 
92 	/* clone formatter object */
93 	if (MSG_FORMAT_OBJECT(mfo) != NULL) {
94 		MSG_FORMAT_OBJECT(new_mfo) = umsg_clone(MSG_FORMAT_OBJECT(mfo),
95 				&INTL_DATA_ERROR_CODE(mfo));
96 
97 		if (U_FAILURE(INTL_DATA_ERROR_CODE(mfo))) {
98 			intl_errors_set(INTL_DATA_ERROR_P(mfo), INTL_DATA_ERROR_CODE(mfo),
99 					"Failed to clone MessageFormatter object", 0 TSRMLS_CC);
100 			zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Failed to clone MessageFormatter object");
101 		}
102 	} else {
103 		zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Cannot clone unconstructed MessageFormatter");
104 	}
105 	return new_obj_val;
106 }
107 /* }}} */
108 
109 /*
110  * 'MessageFormatter' class registration structures & functions
111  */
112 
113 /* {{{ arginfo */
114 ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter___construct, 0, 0, 2)
115 	ZEND_ARG_INFO(0, locale)
116 	ZEND_ARG_INFO(0, pattern)
117 ZEND_END_ARG_INFO()
118 
119 ZEND_BEGIN_ARG_INFO(arginfo_messageformatter_geterrormessage, 0)
120 ZEND_END_ARG_INFO()
121 
122 ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_formatmessage, 0, 0, 3)
123 	ZEND_ARG_INFO(0, locale)
124 	ZEND_ARG_INFO(0, pattern)
125 	ZEND_ARG_INFO(0, args)
126 ZEND_END_ARG_INFO()
127 
128 ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_format, 0, 0, 1)
129 	ZEND_ARG_INFO(0, args)
130 ZEND_END_ARG_INFO()
131 
132 ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_setpattern, 0, 0, 1)
133 	ZEND_ARG_INFO(0, pattern)
134 ZEND_END_ARG_INFO()
135 
136 ZEND_BEGIN_ARG_INFO_EX(arginfo_messageformatter_parse, 0, 0, 1)
137 	ZEND_ARG_INFO(0, source)
138 ZEND_END_ARG_INFO()
139 /* }}} */
140 
141 /* {{{ MessageFormatter_class_functions
142  * Every 'MessageFormatter' class method has an entry in this table
143  */
144 static zend_function_entry MessageFormatter_class_functions[] = {
145 	PHP_ME( MessageFormatter, __construct, arginfo_messageformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
146 	ZEND_FENTRY(  create, ZEND_FN( msgfmt_create ), arginfo_messageformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
147 	PHP_NAMED_FE( format, ZEND_FN( msgfmt_format ), arginfo_messageformatter_format )
148 	ZEND_FENTRY(  formatMessage, ZEND_FN( msgfmt_format_message ), arginfo_messageformatter_formatmessage, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
149 	PHP_NAMED_FE( parse, ZEND_FN( msgfmt_parse ), arginfo_messageformatter_parse )
150 	ZEND_FENTRY(  parseMessage, ZEND_FN( msgfmt_parse_message ), arginfo_messageformatter_formatmessage, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
151 	PHP_NAMED_FE( setPattern, ZEND_FN( msgfmt_set_pattern ), arginfo_messageformatter_setpattern )
152 	PHP_NAMED_FE( getPattern, ZEND_FN( msgfmt_get_pattern ), arginfo_messageformatter_geterrormessage )
153 	PHP_NAMED_FE( getLocale, ZEND_FN( msgfmt_get_locale ), arginfo_messageformatter_geterrormessage )
154 	PHP_NAMED_FE( getErrorCode, ZEND_FN( msgfmt_get_error_code ), arginfo_messageformatter_geterrormessage )
155 	PHP_NAMED_FE( getErrorMessage, ZEND_FN( msgfmt_get_error_message ), arginfo_messageformatter_geterrormessage )
156 	PHP_FE_END
157 };
158 /* }}} */
159 
160 /* {{{ msgformat_register_class
161  * Initialize 'MessageFormatter' class
162  */
msgformat_register_class(TSRMLS_D)163 void msgformat_register_class( TSRMLS_D )
164 {
165 	zend_class_entry ce;
166 
167 	/* Create and register 'MessageFormatter' class. */
168 	INIT_CLASS_ENTRY( ce, "MessageFormatter", MessageFormatter_class_functions );
169 	ce.create_object = MessageFormatter_object_create;
170 	MessageFormatter_ce_ptr = zend_register_internal_class( &ce TSRMLS_CC );
171 
172 	memcpy(&MessageFormatter_handlers, zend_get_std_object_handlers(),
173 		sizeof MessageFormatter_handlers);
174 	MessageFormatter_handlers.clone_obj = MessageFormatter_object_clone;
175 
176 	/* Declare 'MessageFormatter' class properties. */
177 	if( !MessageFormatter_ce_ptr )
178 	{
179 		zend_error(E_ERROR, "Failed to register MessageFormatter class");
180 		return;
181 	}
182 }
183 /* }}} */
184 
185 /*
186  * Local variables:
187  * tab-width: 4
188  * c-basic-offset: 4
189  * End:
190  * vim600: noet sw=4 ts=4 fdm=marker
191  * vim<600: noet sw=4 ts=4
192  */
193