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: Kirti Velankar <kirtig@yahoo-inc.com>                       |
14    +----------------------------------------------------------------------+
15 */
16 #include <unicode/unum.h>
17 
18 #include "dateformat_class.h"
19 #include "php_intl.h"
20 #include "dateformat_data.h"
21 #include "dateformat_format.h"
22 #include "dateformat_format_object.h"
23 #include "dateformat_parse.h"
24 #include "dateformat.h"
25 #include "dateformat_attr.h"
26 #include "dateformat_attrcpp.h"
27 
28 #include <zend_exceptions.h>
29 
30 zend_class_entry *IntlDateFormatter_ce_ptr = NULL;
31 static zend_object_handlers IntlDateFormatter_handlers;
32 
33 /*
34  * Auxiliary functions needed by objects of 'IntlDateFormatter' class
35  */
36 
37 /* {{{ IntlDateFormatter_objects_dtor */
IntlDateFormatter_object_dtor(void * object,zend_object_handle handle TSRMLS_DC)38 static void IntlDateFormatter_object_dtor(void *object, zend_object_handle handle TSRMLS_DC )
39 {
40 	zend_objects_destroy_object( object, handle TSRMLS_CC );
41 }
42 /* }}} */
43 
44 /* {{{ IntlDateFormatter_objects_free */
IntlDateFormatter_object_free(zend_object * object TSRMLS_DC)45 void IntlDateFormatter_object_free( zend_object *object TSRMLS_DC )
46 {
47 	IntlDateFormatter_object* dfo = (IntlDateFormatter_object*)object;
48 
49 	zend_object_std_dtor( &dfo->zo TSRMLS_CC );
50 
51 	if (dfo->requested_locale) {
52 		efree( dfo->requested_locale );
53 	}
54 
55 	dateformat_data_free( &dfo->datef_data TSRMLS_CC );
56 
57 	efree( dfo );
58 }
59 /* }}} */
60 
61 /* {{{ IntlDateFormatter_object_create */
IntlDateFormatter_object_create(zend_class_entry * ce TSRMLS_DC)62 zend_object_value IntlDateFormatter_object_create(zend_class_entry *ce TSRMLS_DC)
63 {
64 	zend_object_value    retval;
65 	IntlDateFormatter_object*     intern;
66 
67 	intern = ecalloc( 1, sizeof(IntlDateFormatter_object) );
68 	dateformat_data_init( &intern->datef_data TSRMLS_CC );
69 	zend_object_std_init( &intern->zo, ce TSRMLS_CC );
70 	object_properties_init(&intern->zo, ce);
71 	intern->date_type			= 0;
72 	intern->time_type			= 0;
73 	intern->calendar			= -1;
74 	intern->requested_locale	= NULL;
75 
76 	retval.handle = zend_objects_store_put(
77 		intern,
78 		IntlDateFormatter_object_dtor,
79 		(zend_objects_free_object_storage_t)IntlDateFormatter_object_free,
80 		NULL TSRMLS_CC );
81 
82 	retval.handlers = &IntlDateFormatter_handlers;
83 
84 	return retval;
85 }
86 /* }}} */
87 
88 /* {{{ IntlDateFormatter_object_clone */
IntlDateFormatter_object_clone(zval * object TSRMLS_DC)89 zend_object_value IntlDateFormatter_object_clone(zval *object TSRMLS_DC)
90 {
91 	zend_object_value new_obj_val;
92 	zend_object_handle handle = Z_OBJ_HANDLE_P(object);
93 	IntlDateFormatter_object *dfo, *new_dfo;
94 
95 	DATE_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
96 
97 	new_obj_val = IntlDateFormatter_ce_ptr->create_object(Z_OBJCE_P(object) TSRMLS_CC);
98 	new_dfo = (IntlDateFormatter_object *)zend_object_store_get_object_by_handle(new_obj_val.handle TSRMLS_CC);
99 	/* clone standard parts */
100 	zend_objects_clone_members(&new_dfo->zo, new_obj_val, &dfo->zo, handle TSRMLS_CC);
101 	/* clone formatter object */
102 	if (dfo->datef_data.udatf != NULL) {
103 		DATE_FORMAT_OBJECT(new_dfo) = udat_clone(DATE_FORMAT_OBJECT(dfo),  &INTL_DATA_ERROR_CODE(dfo));
104 		if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
105 			/* set up error in case error handler is interested */
106 			intl_errors_set(INTL_DATA_ERROR_P(dfo), INTL_DATA_ERROR_CODE(dfo),
107 					"Failed to clone IntlDateFormatter object", 0 TSRMLS_CC );
108 			zend_throw_exception(NULL, "Failed to clone IntlDateFormatter object", 0 TSRMLS_CC);
109 		}
110 	} else {
111 		zend_throw_exception(NULL, "Cannot clone unconstructed IntlDateFormatter", 0 TSRMLS_CC);
112 	}
113 	return new_obj_val;
114 }
115 /* }}} */
116 
117 /*
118  * 'IntlDateFormatter' class registration structures & functions
119  */
120 
121 /* {{{ arginfo */
122 ZEND_BEGIN_ARG_INFO_EX(datefmt_parse_args, 0, 0, 1)
123 		ZEND_ARG_INFO(0, string)
124 		ZEND_ARG_INFO(1, position)
125 ZEND_END_ARG_INFO()
126 
127 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_format, 0, 0, 0)
128 	ZEND_ARG_INFO(0, args)
129 	ZEND_ARG_INFO(0, array)
130 ZEND_END_ARG_INFO()
131 
132 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_format_object, 0, 0, 1)
133 	ZEND_ARG_INFO(0, object)
134 	ZEND_ARG_INFO(0, format)
135 	ZEND_ARG_INFO(0, locale)
136 ZEND_END_ARG_INFO()
137 
138 ZEND_BEGIN_ARG_INFO(arginfo_intldateformatter_getdatetype, 0)
139 ZEND_END_ARG_INFO()
140 
141 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_settimezoneid, 0, 0, 1)
142 	ZEND_ARG_INFO(0, zone)
143 ZEND_END_ARG_INFO()
144 
145 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_setpattern, 0, 0, 1)
146 	ZEND_ARG_INFO(0, pattern)
147 ZEND_END_ARG_INFO()
148 
149 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_setlenient, 0, 0, 1)
150 	ZEND_ARG_INFO(0, lenient)
151 ZEND_END_ARG_INFO()
152 
153 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_setcalendar, 0, 0, 1)
154 	ZEND_ARG_INFO(0, which)
155 ZEND_END_ARG_INFO()
156 
157 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter___construct, 0, 0, 3)
158 	ZEND_ARG_INFO(0, locale)
159 	ZEND_ARG_INFO(0, datetype)
160 	ZEND_ARG_INFO(0, timetype)
161 	ZEND_ARG_INFO(0, timezone)
162 	ZEND_ARG_INFO(0, calendar)
163 	ZEND_ARG_INFO(0, pattern)
164 ZEND_END_ARG_INFO()
165 /* }}} */
166 
167 /* {{{ IntlDateFormatter_class_functions
168  * Every 'IntlDateFormatter' class method has an entry in this table
169  */
170 static zend_function_entry IntlDateFormatter_class_functions[] = {
171 	PHP_ME( IntlDateFormatter, __construct, arginfo_intldateformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR )
172 	ZEND_FENTRY(  create, ZEND_FN( datefmt_create ), arginfo_intldateformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
173 	PHP_NAMED_FE( getDateType, ZEND_FN( datefmt_get_datetype ), arginfo_intldateformatter_getdatetype )
174 	PHP_NAMED_FE( getTimeType, ZEND_FN( datefmt_get_timetype ), arginfo_intldateformatter_getdatetype )
175 	PHP_NAMED_FE( getCalendar, ZEND_FN( datefmt_get_calendar ), arginfo_intldateformatter_getdatetype )
176 	PHP_NAMED_FE( getCalendarObject, ZEND_FN( datefmt_get_calendar_object ), arginfo_intldateformatter_getdatetype )
177 	PHP_NAMED_FE( setCalendar, ZEND_FN( datefmt_set_calendar ), arginfo_intldateformatter_setcalendar )
178 	PHP_NAMED_FE( getTimeZoneId, ZEND_FN( datefmt_get_timezone_id ), arginfo_intldateformatter_getdatetype )
179 	PHP_NAMED_FE( setTimeZoneId, ZEND_FN( datefmt_set_timezone_id ), arginfo_intldateformatter_settimezoneid )
180 	PHP_NAMED_FE( getTimeZone, ZEND_FN( datefmt_get_timezone ), arginfo_intldateformatter_getdatetype )
181 	PHP_NAMED_FE( setTimeZone, ZEND_FN( datefmt_set_timezone ), arginfo_intldateformatter_settimezoneid )
182 	PHP_NAMED_FE( setPattern, ZEND_FN( datefmt_set_pattern ), arginfo_intldateformatter_setpattern )
183 	PHP_NAMED_FE( getPattern, ZEND_FN( datefmt_get_pattern ), arginfo_intldateformatter_getdatetype )
184 	PHP_NAMED_FE( getLocale, ZEND_FN( datefmt_get_locale ), arginfo_intldateformatter_getdatetype )
185 	PHP_NAMED_FE( setLenient, ZEND_FN( datefmt_set_lenient ), arginfo_intldateformatter_setlenient )
186 	PHP_NAMED_FE( isLenient, ZEND_FN( datefmt_is_lenient ), arginfo_intldateformatter_getdatetype )
187 	PHP_NAMED_FE( format, ZEND_FN( datefmt_format ), arginfo_intldateformatter_format )
188 	PHP_ME_MAPPING( formatObject, datefmt_format_object, arginfo_intldateformatter_format_object, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
189 	PHP_NAMED_FE( parse, ZEND_FN( datefmt_parse), datefmt_parse_args )
190 	PHP_NAMED_FE( localtime, ZEND_FN( datefmt_localtime ), datefmt_parse_args )
191 	PHP_NAMED_FE( getErrorCode, ZEND_FN( datefmt_get_error_code ), arginfo_intldateformatter_getdatetype )
192 	PHP_NAMED_FE( getErrorMessage, ZEND_FN( datefmt_get_error_message ), arginfo_intldateformatter_getdatetype )
193 	PHP_FE_END
194 };
195 /* }}} */
196 
197 /* {{{ dateformat_register_class
198  * Initialize 'IntlDateFormatter' class
199  */
dateformat_register_IntlDateFormatter_class(TSRMLS_D)200 void dateformat_register_IntlDateFormatter_class( TSRMLS_D )
201 {
202 	zend_class_entry ce;
203 
204 	/* Create and register 'IntlDateFormatter' class. */
205 	INIT_CLASS_ENTRY( ce, "IntlDateFormatter", IntlDateFormatter_class_functions );
206 	ce.create_object = IntlDateFormatter_object_create;
207 	IntlDateFormatter_ce_ptr = zend_register_internal_class( &ce TSRMLS_CC );
208 
209 	memcpy(&IntlDateFormatter_handlers, zend_get_std_object_handlers(),
210 		sizeof IntlDateFormatter_handlers);
211 	IntlDateFormatter_handlers.clone_obj = IntlDateFormatter_object_clone;
212 
213 	/* Declare 'IntlDateFormatter' class properties. */
214 	if( !IntlDateFormatter_ce_ptr )
215 	{
216 		zend_error(E_ERROR, "Failed to register IntlDateFormatter class");
217 		return;
218 	}
219 }
220 /* }}} */
221