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