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