Skip to content

Commit 5597dfb

Browse files
committed
Refactor UTCDateTime initialisation logic
1 parent 7ba3371 commit 5597dfb

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

src/BSON/UTCDateTime.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -179,36 +179,36 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct)
179179
return;
180180
}
181181

182-
if (Z_TYPE_P(milliseconds) == IS_OBJECT) {
183-
if (instanceof_function(Z_OBJCE_P(milliseconds), php_date_get_interface_ce())) {
184-
php_phongo_utcdatetime_init_from_date(intern, Z_PHPDATE_P(milliseconds));
185-
} else {
186-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of DateTimeInterface, %s given", ZSTR_VAL(Z_OBJCE_P(milliseconds)->name));
182+
switch (Z_TYPE_P(milliseconds)) {
183+
case IS_OBJECT:
184+
if (instanceof_function(Z_OBJCE_P(milliseconds), php_date_get_interface_ce())) {
185+
php_phongo_utcdatetime_init_from_date(intern, Z_PHPDATE_P(milliseconds));
186+
} else {
187+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of DateTimeInterface, %s given", ZSTR_VAL(Z_OBJCE_P(milliseconds)->name));
188+
}
189+
return;
190+
191+
case IS_LONG:
192+
php_phongo_utcdatetime_init(intern, Z_LVAL_P(milliseconds));
193+
return;
194+
195+
case IS_DOUBLE: {
196+
char tmp[24];
197+
int tmp_len;
198+
199+
tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", Z_DVAL_P(milliseconds) > 0 ? floor(Z_DVAL_P(milliseconds)) : ceil(Z_DVAL_P(milliseconds)));
200+
201+
php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len);
187202
}
188-
return;
189-
}
190-
191-
if (Z_TYPE_P(milliseconds) == IS_LONG) {
192-
php_phongo_utcdatetime_init(intern, Z_LVAL_P(milliseconds));
193-
return;
194-
}
195203

196-
if (Z_TYPE_P(milliseconds) == IS_DOUBLE) {
197-
char tmp[24];
198-
int tmp_len;
204+
return;
199205

200-
tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", Z_DVAL_P(milliseconds) > 0 ? floor(Z_DVAL_P(milliseconds)) : ceil(Z_DVAL_P(milliseconds)));
201-
202-
php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len);
203-
return;
204-
}
205-
206-
if (Z_TYPE_P(milliseconds) != IS_STRING) {
207-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds));
208-
return;
206+
case IS_STRING:
207+
php_phongo_utcdatetime_init_from_string(intern, Z_STRVAL_P(milliseconds), Z_STRLEN_P(milliseconds));
208+
return;
209209
}
210210

211-
php_phongo_utcdatetime_init_from_string(intern, Z_STRVAL_P(milliseconds), Z_STRLEN_P(milliseconds));
211+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds));
212212
}
213213

214214
static PHP_METHOD(MongoDB_BSON_UTCDateTime, __set_state)

0 commit comments

Comments
 (0)