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: 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(zend_object * object)38 static void IntlDateFormatter_object_dtor(zend_object *object )
39 {
40 	zend_objects_destroy_object( object );
41 }
42 /* }}} */
43 
44 /* {{{ IntlDateFormatter_objects_free */
IntlDateFormatter_object_free(zend_object * object)45 void IntlDateFormatter_object_free( zend_object *object )
46 {
47 	IntlDateFormatter_object* dfo = php_intl_dateformatter_fetch_object(object);
48 
49 	zend_object_std_dtor( &dfo->zo );
50 
51 	if (dfo->requested_locale) {
52 		efree( dfo->requested_locale );
53 	}
54 
55 	dateformat_data_free( &dfo->datef_data );
56 }
57 /* }}} */
58 
59 /* {{{ IntlDateFormatter_object_create */
IntlDateFormatter_object_create(zend_class_entry * ce)60 zend_object *IntlDateFormatter_object_create(zend_class_entry *ce)
61 {
62 	IntlDateFormatter_object*     intern;
63 
64 	intern = zend_object_alloc(sizeof(IntlDateFormatter_object), ce);
65 	dateformat_data_init( &intern->datef_data );
66 	zend_object_std_init( &intern->zo, ce );
67 	object_properties_init(&intern->zo, ce);
68 	intern->date_type			= 0;
69 	intern->time_type			= 0;
70 	intern->calendar			= -1;
71 	intern->requested_locale	= NULL;
72 
73 	intern->zo.handlers = &IntlDateFormatter_handlers;
74 
75 	return &intern->zo;
76 }
77 /* }}} */
78 
79 /* {{{ IntlDateFormatter_object_clone */
IntlDateFormatter_object_clone(zval * object)80 zend_object *IntlDateFormatter_object_clone(zval *object)
81 {
82 	IntlDateFormatter_object *dfo, *new_dfo;
83 	zend_object *new_obj;
84 
85 	DATE_FORMAT_METHOD_FETCH_OBJECT_NO_CHECK;
86 
87 	new_obj = IntlDateFormatter_ce_ptr->create_object(Z_OBJCE_P(object));
88 	new_dfo = php_intl_dateformatter_fetch_object(new_obj);
89 	/* clone standard parts */
90 	zend_objects_clone_members(&new_dfo->zo, &dfo->zo);
91 	/* clone formatter object */
92 	if (dfo->datef_data.udatf != NULL) {
93 		DATE_FORMAT_OBJECT(new_dfo) = udat_clone(DATE_FORMAT_OBJECT(dfo),  &INTL_DATA_ERROR_CODE(dfo));
94 		if (U_FAILURE(INTL_DATA_ERROR_CODE(dfo))) {
95 			/* set up error in case error handler is interested */
96 			intl_errors_set(INTL_DATA_ERROR_P(dfo), INTL_DATA_ERROR_CODE(dfo),
97 					"Failed to clone IntlDateFormatter object", 0 );
98 			zend_throw_exception(NULL, "Failed to clone IntlDateFormatter object", 0);
99 		}
100 	} else {
101 		zend_throw_exception(NULL, "Cannot clone unconstructed IntlDateFormatter", 0);
102 	}
103 	return new_obj;
104 }
105 /* }}} */
106 
107 /*
108  * 'IntlDateFormatter' class registration structures & functions
109  */
110 
111 /* {{{ arginfo */
112 ZEND_BEGIN_ARG_INFO_EX(datefmt_parse_args, 0, 0, 1)
113 		ZEND_ARG_INFO(0, string)
114 		ZEND_ARG_INFO(1, position)
115 ZEND_END_ARG_INFO()
116 
117 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_format, 0, 0, 0)
118 	ZEND_ARG_INFO(0, args)
119 	ZEND_ARG_INFO(0, array)
120 ZEND_END_ARG_INFO()
121 
122 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_format_object, 0, 0, 1)
123 	ZEND_ARG_INFO(0, object)
124 	ZEND_ARG_INFO(0, format)
125 	ZEND_ARG_INFO(0, locale)
126 ZEND_END_ARG_INFO()
127 
128 ZEND_BEGIN_ARG_INFO(arginfo_intldateformatter_getdatetype, 0)
129 ZEND_END_ARG_INFO()
130 
131 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_settimezoneid, 0, 0, 1)
132 	ZEND_ARG_INFO(0, zone)
133 ZEND_END_ARG_INFO()
134 
135 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_setpattern, 0, 0, 1)
136 	ZEND_ARG_INFO(0, pattern)
137 ZEND_END_ARG_INFO()
138 
139 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_setlenient, 0, 0, 1)
140 	ZEND_ARG_INFO(0, lenient)
141 ZEND_END_ARG_INFO()
142 
143 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter_setcalendar, 0, 0, 1)
144 	ZEND_ARG_INFO(0, which)
145 ZEND_END_ARG_INFO()
146 
147 ZEND_BEGIN_ARG_INFO_EX(arginfo_intldateformatter___construct, 0, 0, 3)
148 	ZEND_ARG_INFO(0, locale)
149 	ZEND_ARG_INFO(0, datetype)
150 	ZEND_ARG_INFO(0, timetype)
151 	ZEND_ARG_INFO(0, timezone)
152 	ZEND_ARG_INFO(0, calendar)
153 	ZEND_ARG_INFO(0, pattern)
154 ZEND_END_ARG_INFO()
155 /* }}} */
156 
157 /* {{{ IntlDateFormatter_class_functions
158  * Every 'IntlDateFormatter' class method has an entry in this table
159  */
160 static const zend_function_entry IntlDateFormatter_class_functions[] = {
161 	PHP_ME( IntlDateFormatter, __construct, arginfo_intldateformatter___construct, ZEND_ACC_PUBLIC )
162 	ZEND_FENTRY(  create, ZEND_FN( datefmt_create ), arginfo_intldateformatter___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC )
163 	PHP_NAMED_FE( getDateType, ZEND_FN( datefmt_get_datetype ), arginfo_intldateformatter_getdatetype )
164 	PHP_NAMED_FE( getTimeType, ZEND_FN( datefmt_get_timetype ), arginfo_intldateformatter_getdatetype )
165 	PHP_NAMED_FE( getCalendar, ZEND_FN( datefmt_get_calendar ), arginfo_intldateformatter_getdatetype )
166 	PHP_NAMED_FE( getCalendarObject, ZEND_FN( datefmt_get_calendar_object ), arginfo_intldateformatter_getdatetype )
167 	PHP_NAMED_FE( setCalendar, ZEND_FN( datefmt_set_calendar ), arginfo_intldateformatter_setcalendar )
168 	PHP_NAMED_FE( getTimeZoneId, ZEND_FN( datefmt_get_timezone_id ), arginfo_intldateformatter_getdatetype )
169 	PHP_NAMED_FE( getTimeZone, ZEND_FN( datefmt_get_timezone ), arginfo_intldateformatter_getdatetype )
170 	PHP_NAMED_FE( setTimeZone, ZEND_FN( datefmt_set_timezone ), arginfo_intldateformatter_settimezoneid )
171 	PHP_NAMED_FE( setPattern, ZEND_FN( datefmt_set_pattern ), arginfo_intldateformatter_setpattern )
172 	PHP_NAMED_FE( getPattern, ZEND_FN( datefmt_get_pattern ), arginfo_intldateformatter_getdatetype )
173 	PHP_NAMED_FE( getLocale, ZEND_FN( datefmt_get_locale ), arginfo_intldateformatter_getdatetype )
174 	PHP_NAMED_FE( setLenient, ZEND_FN( datefmt_set_lenient ), arginfo_intldateformatter_setlenient )
175 	PHP_NAMED_FE( isLenient, ZEND_FN( datefmt_is_lenient ), arginfo_intldateformatter_getdatetype )
176 	PHP_NAMED_FE( format, ZEND_FN( datefmt_format ), arginfo_intldateformatter_format )
177 	PHP_ME_MAPPING( formatObject, datefmt_format_object, arginfo_intldateformatter_format_object, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
178 	PHP_NAMED_FE( parse, ZEND_FN( datefmt_parse), datefmt_parse_args )
179 	PHP_NAMED_FE( localtime, ZEND_FN( datefmt_localtime ), datefmt_parse_args )
180 	PHP_NAMED_FE( getErrorCode, ZEND_FN( datefmt_get_error_code ), arginfo_intldateformatter_getdatetype )
181 	PHP_NAMED_FE( getErrorMessage, ZEND_FN( datefmt_get_error_message ), arginfo_intldateformatter_getdatetype )
182 	PHP_FE_END
183 };
184 /* }}} */
185 
186 /* {{{ dateformat_register_class
187  * Initialize 'IntlDateFormatter' class
188  */
dateformat_register_IntlDateFormatter_class(void)189 void dateformat_register_IntlDateFormatter_class( void )
190 {
191 	zend_class_entry ce;
192 
193 	/* Create and register 'IntlDateFormatter' class. */
194 	INIT_CLASS_ENTRY( ce, "IntlDateFormatter", IntlDateFormatter_class_functions );
195 	ce.create_object = IntlDateFormatter_object_create;
196 	IntlDateFormatter_ce_ptr = zend_register_internal_class( &ce );
197 
198 	memcpy(&IntlDateFormatter_handlers, &std_object_handlers,
199 		sizeof IntlDateFormatter_handlers);
200 	IntlDateFormatter_handlers.offset = XtOffsetOf(IntlDateFormatter_object, zo);
201 	IntlDateFormatter_handlers.clone_obj = IntlDateFormatter_object_clone;
202 	IntlDateFormatter_handlers.dtor_obj = IntlDateFormatter_object_dtor;
203 	IntlDateFormatter_handlers.free_obj = IntlDateFormatter_object_free;
204 }
205 /* }}} */
206