Skip to content

Commit 351046e

Browse files
committed
ext/intl: Refactor timezone parameter passing
1 parent a3f3404 commit 351046e

14 files changed

+200
-136
lines changed

ext/intl/calendar/calendar.stub.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,10 @@ class IntlCalendar
9090
private function __construct() {}
9191

9292
/**
93-
* @param IntlTimeZone|DateTimeZone|string|null $timezone
9493
* @tentative-return-type
9594
* @alias intlcal_create_instance
9695
*/
97-
public static function createInstance($timezone = null, ?string $locale = null): ?IntlCalendar {}
96+
public static function createInstance(IntlTimeZone|DateTimeZone|string|null $timezone = null, ?string $locale = null): ?IntlCalendar {}
9897

9998
/**
10099
* @tentative-return-type
@@ -355,11 +354,9 @@ public function setSkippedWallTimeOption(int $option): true {}
355354
public function setTime(float $timestamp): bool {}
356355

357356
/**
358-
* @param IntlTimeZone|DateTimeZone|string|null $timezone
359357
* @tentative-return-type
360-
* @alias intlcal_set_time_zone
361358
*/
362-
public function setTimeZone($timezone): bool {}
359+
public function setTimeZone(IntlTimeZone|DateTimeZone|string|null $timezone): bool {}
363360

364361
/**
365362
* @tentative-return-type

ext/intl/calendar/calendar_arginfo.h

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/intl/calendar/calendar_methods.cpp

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ U_CFUNC PHP_METHOD(IntlCalendar, __construct)
7272

7373
U_CFUNC PHP_FUNCTION(intlcal_create_instance)
7474
{
75-
zval *zv_timezone = NULL;
75+
zend_object *timezone_object = nullptr;
76+
zend_string *timezone_string = nullptr;
7677
char *locale_str = NULL;
7778
size_t locale_len = 0;
78-
TimeZone *timeZone;
7979
UErrorCode status = U_ZERO_ERROR;
8080
intl_error_reset(NULL);
8181

8282
ZEND_PARSE_PARAMETERS_START(0, 2)
8383
Z_PARAM_OPTIONAL
84-
Z_PARAM_ZVAL(zv_timezone)
84+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
8585
Z_PARAM_STRING_OR_NULL(locale_str, locale_len)
8686
ZEND_PARSE_PARAMETERS_END();
8787

88-
timeZone = timezone_process_timezone_argument(zv_timezone, NULL);
89-
if (timeZone == NULL) {
88+
TimeZone *timeZone = timezone_process_timezone_argument(timezone_object, timezone_string, nullptr);
89+
if (timeZone == nullptr) {
9090
RETURN_NULL();
9191
}
9292

@@ -296,26 +296,28 @@ U_CFUNC PHP_FUNCTION(intlcal_add)
296296
RETURN_TRUE;
297297
}
298298

299+
/* {{{ Set formatter's timezone. */
299300
U_CFUNC PHP_FUNCTION(intlcal_set_time_zone)
300301
{
301-
zval *zv_timezone;
302-
TimeZone *timeZone;
302+
zend_object *timezone_object = nullptr;
303+
zend_string *timezone_string = nullptr;
304+
303305
CALENDAR_METHOD_INIT_VARS;
304306

305-
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
306-
"Oz!", &object, Calendar_ce_ptr, &zv_timezone) == FAILURE) {
307-
RETURN_THROWS();
308-
}
307+
ZEND_PARSE_PARAMETERS_START(2, 2)
308+
Z_PARAM_OBJECT_OF_CLASS(object, Calendar_ce_ptr)
309+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
310+
ZEND_PARSE_PARAMETERS_END();
309311

310312
CALENDAR_METHOD_FETCH_OBJECT;
311313

312-
if (zv_timezone == NULL) {
314+
if (timezone_object == nullptr && timezone_string == nullptr) {
313315
RETURN_TRUE; /* the method does nothing if passed null */
314316
}
315317

316-
timeZone = timezone_process_timezone_argument(zv_timezone,
317-
CALENDAR_ERROR_P(co));
318-
if (timeZone == NULL) {
318+
TimeZone *timeZone = timezone_process_timezone_argument(
319+
timezone_object, timezone_string, CALENDAR_ERROR_P(co));
320+
if (timeZone == nullptr) {
319321
RETURN_FALSE;
320322
}
321323

@@ -324,6 +326,34 @@ U_CFUNC PHP_FUNCTION(intlcal_set_time_zone)
324326
RETURN_TRUE;
325327
}
326328

329+
U_CFUNC PHP_METHOD(IntlCalendar, setTimeZone)
330+
{
331+
zend_object *timezone_object = nullptr;
332+
zend_string *timezone_string = nullptr;
333+
334+
CALENDAR_METHOD_INIT_VARS;
335+
336+
ZEND_PARSE_PARAMETERS_START(1, 1)
337+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
338+
ZEND_PARSE_PARAMETERS_END();
339+
340+
object = ZEND_THIS;
341+
CALENDAR_METHOD_FETCH_OBJECT;
342+
343+
if (timezone_object == nullptr && timezone_string == nullptr) {
344+
RETURN_TRUE; /* the method does nothing if passed null */
345+
}
346+
347+
TimeZone *timeZone = timezone_process_timezone_argument(
348+
timezone_object, timezone_string, CALENDAR_ERROR_P(co));
349+
if (timeZone == nullptr) {
350+
RETURN_FALSE;
351+
}
352+
353+
co->ucal->adoptTimeZone(timeZone);
354+
355+
RETURN_TRUE;
356+
}
327357

328358
static void _php_intlcal_before_after(
329359
UBool (Calendar::*func)(const Calendar&, UErrorCode&) const,

ext/intl/calendar/gregoriancalendar_methods.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ static bool set_gregorian_calendar_time_zone(GregorianCalendar *gcal, UErrorCode
7777

7878
static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS, bool is_constructor)
7979
{
80-
zval *tz_object = NULL;
80+
zend_object *timezone_object = nullptr;
81+
zend_string *timezone_string = nullptr;
8182
zval args_a[6],
8283
*args = &args_a[0];
8384
char *locale = NULL;
@@ -112,15 +113,23 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS, bool
112113

113114
// argument parsing
114115
if (variant <= 2) {
115-
if (zend_parse_parameters(MIN(ZEND_NUM_ARGS(), 2),
116-
"|z!s!", &tz_object, &locale, &locale_len) == FAILURE) {
117-
RETURN_THROWS();
118-
}
119-
}
120-
if (variant > 2 && zend_parse_parameters(ZEND_NUM_ARGS(),
121-
"lll|lll", &largs[0], &largs[1], &largs[2], &largs[3], &largs[4],
122-
&largs[5]) == FAILURE) {
123-
RETURN_THROWS();
116+
/* These dummy variables are needed because the 2 param constructor allows trailing nulls... */
117+
zval *dummy1, *dummy2, *dummy3, *dummy4;
118+
ZEND_PARSE_PARAMETERS_START(0, 6)
119+
Z_PARAM_OPTIONAL
120+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
121+
Z_PARAM_STRING_OR_NULL(locale, locale_len)
122+
Z_PARAM_ZVAL(dummy1)
123+
Z_PARAM_ZVAL(dummy2)
124+
Z_PARAM_ZVAL(dummy3)
125+
Z_PARAM_ZVAL(dummy4)
126+
ZEND_PARSE_PARAMETERS_END();
127+
}
128+
if (variant > 2
129+
&& zend_parse_parameters(ZEND_NUM_ARGS(), "lll|lll",
130+
&largs[0], &largs[1], &largs[2], &largs[3], &largs[4], &largs[5]) == FAILURE
131+
) {
132+
RETURN_THROWS();
124133
}
125134

126135
// instantion of ICU object
@@ -134,8 +143,8 @@ static void _php_intlgregcal_constructor_body(INTERNAL_FUNCTION_PARAMETERS, bool
134143

135144
if (variant <= 2) {
136145
// From timezone and locale (0 to 2 arguments)
137-
TimeZone *tz = timezone_process_timezone_argument(tz_object, NULL);
138-
if (tz == NULL) {
146+
TimeZone *tz = timezone_process_timezone_argument(timezone_object, timezone_string, nullptr);
147+
if (tz == nullptr) {
139148
// TODO: Exception should always occur already?
140149
if (!EG(exception)) {
141150
zend_throw_exception(IntlException_ce_ptr, "Constructor failed", 0);

ext/intl/dateformat/dateformat.stub.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,9 @@ public function getCalendarObject(): IntlCalendar|false|null {}
101101
public function getTimeZone(): IntlTimeZone|false {}
102102

103103
/**
104-
* @param IntlTimeZone|DateTimeZone|string|null $timezone
105104
* @tentative-return-type
106-
* @alias datefmt_set_timezone
107105
*/
108-
public function setTimeZone($timezone): bool {}
106+
public function setTimeZone(IntlTimeZone|DateTimeZone|string|null $timezone): bool {}
109107

110108
/**
111109
* @tentative-return-type

ext/intl/dateformat/dateformat_arginfo.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/intl/dateformat/dateformat_attrcpp.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,46 @@ U_CFUNC PHP_FUNCTION(datefmt_get_timezone)
8181
/* {{{ Set formatter's timezone. */
8282
U_CFUNC PHP_FUNCTION(datefmt_set_timezone)
8383
{
84-
zval *timezone_zv;
85-
TimeZone *timezone;
84+
zend_object *timezone_object = nullptr;
85+
zend_string *timezone_string = nullptr;
8686

8787
DATE_FORMAT_METHOD_INIT_VARS;
8888

89-
if ( zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
90-
"Oz", &object, IntlDateFormatter_ce_ptr, &timezone_zv) == FAILURE) {
91-
RETURN_THROWS();
89+
ZEND_PARSE_PARAMETERS_START(2, 2)
90+
Z_PARAM_OBJECT_OF_CLASS(object, IntlDateFormatter_ce_ptr)
91+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
92+
ZEND_PARSE_PARAMETERS_END();
93+
94+
DATE_FORMAT_METHOD_FETCH_OBJECT;
95+
96+
TimeZone *timezone = timezone_process_timezone_argument(
97+
timezone_object, timezone_string, INTL_DATA_ERROR_P(dfo));
98+
if (timezone == nullptr) {
99+
RETURN_FALSE;
92100
}
93101

102+
fetch_datefmt(dfo)->adoptTimeZone(timezone);
103+
104+
RETURN_TRUE;
105+
}
106+
107+
U_CFUNC PHP_METHOD(IntlDateFormatter, setTimeZone)
108+
{
109+
zend_object *timezone_object = nullptr;
110+
zend_string *timezone_string = nullptr;
111+
112+
DATE_FORMAT_METHOD_INIT_VARS;
113+
114+
ZEND_PARSE_PARAMETERS_START(1, 1)
115+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
116+
ZEND_PARSE_PARAMETERS_END();
117+
118+
object = ZEND_THIS;
94119
DATE_FORMAT_METHOD_FETCH_OBJECT;
95120

96-
timezone = timezone_process_timezone_argument(timezone_zv,
97-
INTL_DATA_ERROR_P(dfo));
98-
if (timezone == NULL) {
121+
TimeZone *timezone = timezone_process_timezone_argument(
122+
timezone_object, timezone_string, INTL_DATA_ERROR_P(dfo));
123+
if (timezone == nullptr) {
99124
RETURN_FALSE;
100125
}
101126

ext/intl/dateformat/dateformat_create.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ static zend_result datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
5959
Calendar *cal = NULL;
6060
zend_long calendar_type;
6161
bool calendar_owned;
62-
zval *timezone_zv = NULL;
62+
zend_object *timezone_object = nullptr;
63+
zend_string *timezone_string = nullptr;
6364
TimeZone *timezone = NULL;
6465
bool explicit_tz;
6566
char* pattern_str = NULL;
@@ -76,7 +77,7 @@ static zend_result datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
7677
Z_PARAM_OPTIONAL
7778
Z_PARAM_LONG(date_type)
7879
Z_PARAM_LONG(time_type)
79-
Z_PARAM_ZVAL(timezone_zv)
80+
Z_PARAM_OBJ_OR_STR_OR_NULL(timezone_object, timezone_string)
8081
Z_PARAM_OBJ_OF_CLASS_OR_LONG_OR_NULL(calendar_obj, Calendar_ce_ptr, calendar_long, calendar_is_null)
8182
Z_PARAM_STRING_OR_NULL(pattern_str, pattern_str_len)
8283
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
@@ -120,13 +121,12 @@ static zend_result datefmt_ctor(INTERNAL_FUNCTION_PARAMETERS)
120121
}
121122

122123
/* process timezone */
123-
explicit_tz = timezone_zv != NULL && Z_TYPE_P(timezone_zv) != IS_NULL;
124+
explicit_tz = timezone_object != nullptr || timezone_string != nullptr;
124125

125126
if (explicit_tz || calendar_owned ) {
126127
//we have an explicit time zone or a non-object calendar
127-
timezone = timezone_process_timezone_argument(timezone_zv,
128-
INTL_DATA_ERROR_P(dfo));
129-
if (timezone == NULL) {
128+
timezone = timezone_process_timezone_argument(timezone_object, timezone_string, INTL_DATA_ERROR_P(dfo));
129+
if (timezone == nullptr) {
130130
goto error;
131131
}
132132
}

ext/intl/msgformat/msgformat_helpers.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,7 @@ static void umsg_set_timezone(MessageFormatter_object *mfo,
341341
}
342342

343343
if (used_tz == NULL) {
344-
zval nullzv;
345-
ZVAL_NULL(&nullzv);
346-
used_tz = timezone_process_timezone_argument(&nullzv, &err);
344+
used_tz = timezone_process_timezone_argument(nullptr, nullptr, &err);
347345
if (used_tz == NULL) {
348346
continue;
349347
}

0 commit comments

Comments
 (0)