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 = php_intl_calendar_fetch_object(object);
81 zend_object *ret_val = Calendar_ce_ptr->create_object(object->ce);
82 Calendar_object *co_new = php_intl_calendar_fetch_object(ret_val);
83
84 zend_objects_clone_members(&co_new->zo, &co_orig->zo);
85
86 if (co_orig->ucal != NULL) {
87 Calendar *newCalendar;
88
89 newCalendar = co_orig->ucal->clone();
90 if (UNEXPECTED(!newCalendar)) {
91 zend_throw_error(NULL, "Failed to clone IntlCalendar");
92 } else {
93 co_new->ucal = newCalendar;
94 }
95 } else {
96 zend_throw_error(NULL, "Cannot clone uninitialized IntlCalendar");
97 }
98
99 return ret_val;
100 }
101 /* }}} */
102
103 static const struct {
104 UCalendarDateFields field;
105 const char *name;
106 } debug_info_fields[] = {
107 {UCAL_ERA, "era"},
108 {UCAL_YEAR, "year"},
109 {UCAL_MONTH, "month"},
110 {UCAL_WEEK_OF_YEAR, "week of year"},
111 {UCAL_WEEK_OF_MONTH, "week of month"},
112 {UCAL_DAY_OF_YEAR, "day of year"},
113 {UCAL_DAY_OF_MONTH, "day of month"},
114 {UCAL_DAY_OF_WEEK, "day of week"},
115 {UCAL_DAY_OF_WEEK_IN_MONTH, "day of week in month"},
116 {UCAL_AM_PM, "AM/PM"},
117 {UCAL_HOUR, "hour"},
118 {UCAL_HOUR_OF_DAY, "hour of day"},
119 {UCAL_MINUTE, "minute"},
120 {UCAL_SECOND, "second"},
121 {UCAL_MILLISECOND, "millisecond"},
122 {UCAL_ZONE_OFFSET, "zone offset"},
123 {UCAL_DST_OFFSET, "DST offset"},
124 {UCAL_YEAR_WOY, "year for week of year"},
125 {UCAL_DOW_LOCAL, "localized day of week"},
126 {UCAL_EXTENDED_YEAR, "extended year"},
127 {UCAL_JULIAN_DAY, "julian day"},
128 {UCAL_MILLISECONDS_IN_DAY, "milliseconds in day"},
129 {UCAL_IS_LEAP_MONTH, "is leap month"},
130 };
131
132 /* {{{ get_debug_info handler for Calendar */
Calendar_get_debug_info(zend_object * object,int * is_temp)133 static HashTable *Calendar_get_debug_info(zend_object *object, int *is_temp)
134 {
135 zval zv,
136 zfields;
137 Calendar_object *co;
138 const Calendar *cal;
139 HashTable *debug_info;
140
141 *is_temp = 1;
142
143 debug_info = zend_new_array(8);
144
145 co = php_intl_calendar_fetch_object(object);
146 cal = co->ucal;
147
148 if (cal == NULL) {
149 ZVAL_FALSE(&zv);
150 zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv);
151 return debug_info;
152 }
153 ZVAL_TRUE(&zv);
154 zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &zv);
155
156 ZVAL_STRING(&zv, const_cast<char*>(cal->getType()));
157 zend_hash_str_update(debug_info, "type", sizeof("type") - 1, &zv);
158 {
159 zval ztz,
160 ztz_debug;
161 int is_tmp;
162 HashTable *debug_info_tz;
163
164 timezone_object_construct(&cal->getTimeZone(), &ztz , 0);
165 debug_info_tz = Z_OBJ_HANDLER(ztz, get_debug_info)(Z_OBJ(ztz), &is_tmp);
166 assert(is_tmp == 1);
167
168 array_init(&ztz_debug);
169 zend_hash_copy(Z_ARRVAL(ztz_debug), debug_info_tz, zval_add_ref);
170 zend_hash_destroy(debug_info_tz);
171 FREE_HASHTABLE(debug_info_tz);
172
173 zend_hash_str_update(debug_info, "timeZone", sizeof("timeZone") - 1, &ztz_debug);
174 }
175
176 {
177 UErrorCode uec = U_ZERO_ERROR;
178 Locale locale = cal->getLocale(ULOC_VALID_LOCALE, uec);
179 if (U_SUCCESS(uec)) {
180 ZVAL_STRING(&zv, const_cast<char*>(locale.getName()));
181 zend_hash_str_update(debug_info, "locale", sizeof("locale") - 1, &zv);
182 } else {
183 ZVAL_STRING(&zv, const_cast<char*>(u_errorName(uec)));
184 zend_hash_str_update(debug_info, "locale", sizeof("locale") - 1, &zv);
185 }
186 }
187
188 array_init_size(&zfields, UCAL_FIELD_COUNT);
189
190 for (int i = 0;
191 i < sizeof(debug_info_fields) / sizeof(*debug_info_fields);
192 i++) {
193 UErrorCode uec = U_ZERO_ERROR;
194 const char *name = debug_info_fields[i].name;
195 int32_t res = cal->get(debug_info_fields[i].field, uec);
196 if (U_SUCCESS(uec)) {
197 add_assoc_long(&zfields, name, (zend_long)res);
198 } else {
199 add_assoc_string(&zfields, name, const_cast<char*>(u_errorName(uec)));
200 }
201 }
202
203 zend_hash_str_update(debug_info, "fields", sizeof("fields") - 1, &zfields);
204
205 return debug_info;
206 }
207 /* }}} */
208
209 /* {{{ void calendar_object_init(Calendar_object* to)
210 * Initialize internals of Calendar_object not specific to zend standard objects.
211 */
calendar_object_init(Calendar_object * co)212 static void calendar_object_init(Calendar_object *co)
213 {
214 intl_error_init(CALENDAR_ERROR_P(co));
215 co->ucal = NULL;
216 }
217 /* }}} */
218
219 /* {{{ Calendar_objects_free */
Calendar_objects_free(zend_object * object)220 static void Calendar_objects_free(zend_object *object)
221 {
222 Calendar_object* co = php_intl_calendar_fetch_object(object);
223
224 if (co->ucal) {
225 delete co->ucal;
226 co->ucal = NULL;
227 }
228 intl_error_reset(CALENDAR_ERROR_P(co));
229
230 zend_object_std_dtor(&co->zo);
231 }
232 /* }}} */
233
234 /* {{{ Calendar_object_create */
Calendar_object_create(zend_class_entry * ce)235 static zend_object *Calendar_object_create(zend_class_entry *ce)
236 {
237 Calendar_object* intern;
238
239 intern = (Calendar_object*)ecalloc(1, sizeof(Calendar_object) + sizeof(zval) * (ce->default_properties_count - 1));
240
241 zend_object_std_init(&intern->zo, ce);
242 object_properties_init(&intern->zo, ce);
243 calendar_object_init(intern);
244
245 return &intern->zo;
246 }
247 /* }}} */
248
249 /* {{{ calendar_register_IntlCalendar_class
250 * Initialize 'IntlCalendar' class
251 */
calendar_register_IntlCalendar_class(void)252 void calendar_register_IntlCalendar_class(void)
253 {
254 /* Create and register 'IntlCalendar' class. */
255 Calendar_ce_ptr = register_class_IntlCalendar();
256 Calendar_ce_ptr->default_object_handlers = &Calendar_handlers;
257 Calendar_ce_ptr->create_object = Calendar_object_create;
258
259 memcpy( &Calendar_handlers, &std_object_handlers,
260 sizeof Calendar_handlers);
261 Calendar_handlers.offset = XtOffsetOf(Calendar_object, zo);
262 Calendar_handlers.clone_obj = Calendar_clone_obj;
263 Calendar_handlers.get_debug_info = Calendar_get_debug_info;
264 Calendar_handlers.free_obj = Calendar_objects_free;
265
266 /* Create and register 'IntlGregorianCalendar' class. */
267 GregorianCalendar_ce_ptr = register_class_IntlGregorianCalendar(Calendar_ce_ptr);
268 }
269 /* }}} */
270