1 /*
2 +----------------------------------------------------------------------+
3 | This source file is subject to version 3.01 of the PHP license, |
4 | that is bundled with this package in the file LICENSE, and is |
5 | available through the world-wide-web at the following url: |
6 | https://www.php.net/license/3_01.txt |
7 | If you did not receive a copy of the PHP license and are unable to |
8 | obtain it through the world-wide-web, please send a note to |
9 | license@php.net so we can mail you a copy immediately. |
10 +----------------------------------------------------------------------+
11 | Authors: Gustavo Lopes <cataphract@php.net> |
12 +----------------------------------------------------------------------+
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18
19 #include "../intl_cppshims.h"
20
21 #include <unicode/calendar.h>
22 #include <unicode/gregocal.h>
23
24 extern "C" {
25 #define USE_TIMEZONE_POINTER 1
26 #include "../timezone/timezone_class.h"
27 #define USE_CALENDAR_POINTER 1
28 #include "calendar_class.h"
29 #include "calendar_arginfo.h"
30 #include <zend_exceptions.h>
31 #include <assert.h>
32 }
33
34 using icu::GregorianCalendar;
35 using icu::Locale;
36
37 /* {{{ Global variables */
38 zend_class_entry *Calendar_ce_ptr;
39 zend_class_entry *GregorianCalendar_ce_ptr;
40 zend_object_handlers Calendar_handlers;
41 /* }}} */
42
calendar_object_create(zval * object,Calendar * calendar)43 U_CFUNC void calendar_object_create(zval *object,
44 Calendar *calendar)
45 {
46 UClassID classId = calendar->getDynamicClassID();
47 zend_class_entry *ce;
48
49 //if (dynamic_cast<GregorianCalendar*>(calendar) != NULL) {
50 if (classId == GregorianCalendar::getStaticClassID()) {
51 ce = GregorianCalendar_ce_ptr;
52 } else {
53 ce = Calendar_ce_ptr;
54 }
55
56 object_init_ex(object, ce);
57 calendar_object_construct(object, calendar);
58 }
59
calendar_fetch_native_calendar(zend_object * object)60 U_CFUNC Calendar *calendar_fetch_native_calendar(zend_object *object)
61 {
62 Calendar_object *co = php_intl_calendar_fetch_object(object);
63
64 return co->ucal;
65 }
66
calendar_object_construct(zval * object,Calendar * calendar)67 U_CFUNC void calendar_object_construct(zval *object,
68 Calendar *calendar)
69 {
70 Calendar_object *co;
71
72 CALENDAR_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object
73 assert(co->ucal == NULL);
74 co->ucal = (Calendar*)calendar;
75 }
76
77 /* {{{ clone handler for Calendar */
Calendar_clone_obj(zend_object * object)78 static zend_object *Calendar_clone_obj(zend_object *object)
79 {
80 Calendar_object *co_orig,
81 *co_new;
82 zend_object *ret_val;
83 intl_error_reset(NULL);
84
85 co_orig = php_intl_calendar_fetch_object(object);
86 intl_error_reset(INTL_DATA_ERROR_P(co_orig));
87
88 ret_val = Calendar_ce_ptr->create_object(object->ce);
89 co_new = php_intl_calendar_fetch_object(ret_val);
90
91 zend_objects_clone_members(&co_new->zo, &co_orig->zo);
92
93 if (co_orig->ucal != NULL) {
94 Calendar *newCalendar;
95
96 newCalendar = co_orig->ucal->clone();
97 if (UNEXPECTED(!newCalendar)) {
98 zend_string *err_msg;
99 intl_errors_set_code(CALENDAR_ERROR_P(co_orig),
100 U_MEMORY_ALLOCATION_ERROR);
101 intl_errors_set_custom_msg(CALENDAR_ERROR_P(co_orig),
102 "Could not clone IntlCalendar", 0);
103 err_msg = intl_error_get_message(CALENDAR_ERROR_P(co_orig));
104 zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
105 zend_string_free(err_msg);
106 } else {
107 co_new->ucal = newCalendar;
108 }
109 } else {
110 zend_throw_exception(NULL, "Cannot clone unconstructed IntlCalendar", 0);
111 }
112
113 return ret_val;
114 }
115 /* }}} */
116
117 static const struct {
118 UCalendarDateFields field;
119 const char *name;
120 } debug_info_fields[] = {
121 {UCAL_ERA, "era"},
122 {UCAL_YEAR, "year"},
123 {UCAL_MONTH, "month"},
124 {UCAL_WEEK_OF_YEAR, "week of year"},
125 {UCAL_WEEK_OF_MONTH, "week of month"},
126 {UCAL_DAY_OF_YEAR, "day of year"},
127 {UCAL_DAY_OF_MONTH, "day of month"},
128 {UCAL_DAY_OF_WEEK, "day of week"},
129 {UCAL_DAY_OF_WEEK_IN_MONTH, "day of week in month"},
130 {UCAL_AM_PM, "AM/PM"},
131 {UCAL_HOUR, "hour"},
132 {UCAL_HOUR_OF_DAY, "hour of day"},
133 {UCAL_MINUTE, "minute"},
134 {UCAL_SECOND, "second"},
135 {UCAL_MILLISECOND, "millisecond"},
136 {UCAL_ZONE_OFFSET, "zone offset"},
137 {UCAL_DST_OFFSET, "DST offset"},
138 {UCAL_YEAR_WOY, "year for week of year"},
139 {UCAL_DOW_LOCAL, "localized day of week"},
140 {UCAL_EXTENDED_YEAR, "extended year"},
141 {UCAL_JULIAN_DAY, "julian day"},
142 {UCAL_MILLISECONDS_IN_DAY, "milliseconds in day"},
143 {UCAL_IS_LEAP_MONTH, "is leap month"},
144 };
145
146 /* {{{ get_debug_info handler for Calendar */
Calendar_get_debug_info(zend_object * object,int * is_temp)147 static HashTable *Calendar_get_debug_info(zend_object *object, int *is_temp)
148 {
149 zval zv,
150 zfields;
151 Calendar_object *co;
152 const Calendar *cal;
153 HashTable *debug_info;
154
155 *is_temp = 1;
156
157 debug_info = zend_new_array(8);
158
159 co = php_intl_calendar_fetch_object(object);
160 cal = co->ucal;
161
162 if (cal == NULL) {
163 ZVAL_FALSE(&zv);
164 zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv);
165 return debug_info;
166 }
167 ZVAL_TRUE(&zv);
168 zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv);
169
170 ZVAL_STRING(&zv, const_cast<char*>(cal->getType()));
171 zend_hash_str_update(debug_info, "type", sizeof("type") - 1, &zv);
172 {
173 zval ztz,
174 ztz_debug;
175 int is_tmp;
176 HashTable *debug_info_tz;
177
178 timezone_object_construct(&cal->getTimeZone(), &ztz , 0);
179 debug_info_tz = Z_OBJ_HANDLER(ztz, get_debug_info)(Z_OBJ(ztz), &is_tmp);
180 assert(is_tmp == 1);
181
182 array_init(&ztz_debug);
183 zend_hash_copy(Z_ARRVAL(ztz_debug), debug_info_tz, zval_add_ref);
184 zend_hash_destroy(debug_info_tz);
185 FREE_HASHTABLE(debug_info_tz);
186
187 zend_hash_str_update(debug_info, "timeZone", sizeof("timeZone") - 1, &ztz_debug);
188 }
189
190 {
191 UErrorCode uec = U_ZERO_ERROR;
192 Locale locale = cal->getLocale(ULOC_VALID_LOCALE, uec);
193 if (U_SUCCESS(uec)) {
194 ZVAL_STRING(&zv, const_cast<char*>(locale.getName()));
195 zend_hash_str_update(debug_info, "locale", sizeof("locale") - 1, &zv);
196 } else {
197 ZVAL_STRING(&zv, const_cast<char*>(u_errorName(uec)));
198 zend_hash_str_update(debug_info, "locale", sizeof("locale") - 1, &zv);
199 }
200 }
201
202 array_init_size(&zfields, UCAL_FIELD_COUNT);
203
204 for (int i = 0;
205 i < sizeof(debug_info_fields) / sizeof(*debug_info_fields);
206 i++) {
207 UErrorCode uec = U_ZERO_ERROR;
208 const char *name = debug_info_fields[i].name;
209 int32_t res = cal->get(debug_info_fields[i].field, uec);
210 if (U_SUCCESS(uec)) {
211 add_assoc_long(&zfields, name, (zend_long)res);
212 } else {
213 add_assoc_string(&zfields, name, const_cast<char*>(u_errorName(uec)));
214 }
215 }
216
217 zend_hash_str_update(debug_info, "fields", sizeof("fields") - 1, &zfields);
218
219 return debug_info;
220 }
221 /* }}} */
222
223 /* {{{ void calendar_object_init(Calendar_object* to)
224 * Initialize internals of Calendar_object not specific to zend standard objects.
225 */
calendar_object_init(Calendar_object * co)226 static void calendar_object_init(Calendar_object *co)
227 {
228 intl_error_init(CALENDAR_ERROR_P(co));
229 co->ucal = NULL;
230 }
231 /* }}} */
232
233 /* {{{ Calendar_objects_free */
Calendar_objects_free(zend_object * object)234 static void Calendar_objects_free(zend_object *object)
235 {
236 Calendar_object* co = php_intl_calendar_fetch_object(object);
237
238 if (co->ucal) {
239 delete co->ucal;
240 co->ucal = NULL;
241 }
242 intl_error_reset(CALENDAR_ERROR_P(co));
243
244 zend_object_std_dtor(&co->zo);
245 }
246 /* }}} */
247
248 /* {{{ Calendar_object_create */
Calendar_object_create(zend_class_entry * ce)249 static zend_object *Calendar_object_create(zend_class_entry *ce)
250 {
251 Calendar_object* intern;
252
253 intern = (Calendar_object*)ecalloc(1, sizeof(Calendar_object) + sizeof(zval) * (ce->default_properties_count - 1));
254
255 zend_object_std_init(&intern->zo, ce);
256 object_properties_init(&intern->zo, ce);
257 calendar_object_init(intern);
258
259 return &intern->zo;
260 }
261 /* }}} */
262
263 /* {{{ calendar_register_IntlCalendar_class
264 * Initialize 'IntlCalendar' class
265 */
calendar_register_IntlCalendar_class(void)266 void calendar_register_IntlCalendar_class(void)
267 {
268 /* Create and register 'IntlCalendar' class. */
269 Calendar_ce_ptr = register_class_IntlCalendar();
270 Calendar_ce_ptr->default_object_handlers = &Calendar_handlers;
271 Calendar_ce_ptr->create_object = Calendar_object_create;
272
273 memcpy( &Calendar_handlers, &std_object_handlers,
274 sizeof Calendar_handlers);
275 Calendar_handlers.offset = XtOffsetOf(Calendar_object, zo);
276 Calendar_handlers.clone_obj = Calendar_clone_obj;
277 Calendar_handlers.get_debug_info = Calendar_get_debug_info;
278 Calendar_handlers.free_obj = Calendar_objects_free;
279
280 /* Create and register 'IntlGregorianCalendar' class. */
281 GregorianCalendar_ce_ptr = register_class_IntlGregorianCalendar(Calendar_ce_ptr);
282 }
283 /* }}} */
284