Skip to content

Commit 5174744

Browse files
committed
Extract init methods for utcdatetime_t
1 parent 4852ea0 commit 5174744

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

src/BSON/UTCDateTime.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,35 @@ static bool php_phongo_utcdatetime_init_from_date(php_phongo_utcdatetime_t* inte
104104
return true;
105105
}
106106

107+
static bool php_phongo_utcdatetime_init_from_object(php_phongo_utcdatetime_t* intern, zend_object* object)
108+
{
109+
if (instanceof_function(object->ce, php_date_get_interface_ce())) {
110+
php_phongo_utcdatetime_init_from_date(intern, php_date_obj_from_obj(object));
111+
112+
return true;
113+
}
114+
115+
if (instanceof_function(object->ce, php_phongo_int64_ce)) {
116+
php_phongo_utcdatetime_init(intern, php_int64_fetch_object(object)->integer);
117+
118+
return true;
119+
}
120+
121+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of %s or %s, %s given", ZSTR_VAL(php_date_get_interface_ce()->name), ZSTR_VAL(php_phongo_int64_ce->name), ZSTR_VAL(object->name));
122+
123+
return false;
124+
}
125+
126+
static bool php_phongo_utcdatetime_init_from_double(php_phongo_utcdatetime_t* intern, double milliseconds)
127+
{
128+
char tmp[24];
129+
int tmp_len;
130+
131+
tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", milliseconds > 0 ? floor(milliseconds) : ceil(milliseconds));
132+
133+
return php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len);
134+
}
135+
107136
static HashTable* php_phongo_utcdatetime_get_properties_hash(phongo_compat_object_handler_type* object, bool is_temp)
108137
{
109138
php_phongo_utcdatetime_t* intern;
@@ -181,28 +210,15 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct)
181210

182211
switch (Z_TYPE_P(milliseconds)) {
183212
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 if (instanceof_function(Z_OBJCE_P(milliseconds), php_phongo_int64_ce)) {
187-
php_phongo_utcdatetime_init(intern, Z_INT64_OBJ_P(milliseconds)->integer);
188-
} else {
189-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected instance of %s or %s, %s given", ZSTR_VAL(php_date_get_interface_ce()->name), ZSTR_VAL(php_phongo_int64_ce->name), ZSTR_VAL(Z_OBJCE_P(milliseconds)->name));
190-
}
213+
php_phongo_utcdatetime_init_from_object(intern, Z_OBJ_P(milliseconds));
191214
return;
192215

193216
case IS_LONG:
194217
php_phongo_utcdatetime_init(intern, Z_LVAL_P(milliseconds));
195218
return;
196219

197-
case IS_DOUBLE: {
198-
char tmp[24];
199-
int tmp_len;
200-
201-
tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", Z_DVAL_P(milliseconds) > 0 ? floor(Z_DVAL_P(milliseconds)) : ceil(Z_DVAL_P(milliseconds)));
202-
203-
php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len);
204-
}
205-
220+
case IS_DOUBLE:
221+
php_phongo_utcdatetime_init_from_double(intern, Z_DVAL_P(milliseconds));
206222
return;
207223

208224
case IS_STRING:

0 commit comments

Comments
 (0)