Skip to content

Add DatePeriod's __serialize and __unserialize methods #8464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 56 additions & 10 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -5015,17 +5015,9 @@ static HashTable *date_object_get_gc_period(zend_object *object, zval **table, i
return zend_std_get_properties(object);
} /* }}} */

static HashTable *date_object_get_properties_period(zend_object *object) /* {{{ */
static void date_period_object_to_hash(php_period_obj *period_obj, HashTable *props)
{
HashTable *props;
zval zv;
php_period_obj *period_obj;

period_obj = php_period_obj_from_obj(object);
props = zend_std_get_properties(object);
if (!period_obj->start) {
return props;
}
zval zv;

if (period_obj->start) {
php_date_obj *date_obj;
Expand Down Expand Up @@ -5074,6 +5066,20 @@ static HashTable *date_object_get_properties_period(zend_object *object) /* {{{

ZVAL_BOOL(&zv, period_obj->include_start_date);
zend_hash_str_update(props, "include_start_date", sizeof("include_start_date")-1, &zv);
}

static HashTable *date_object_get_properties_period(zend_object *object) /* {{{ */
{
HashTable *props;
php_period_obj *period_obj;

period_obj = php_period_obj_from_obj(object);
props = zend_std_get_properties(object);
if (!period_obj->start) {
return props;
}

date_period_object_to_hash(period_obj, props);

return props;
} /* }}} */
Expand Down Expand Up @@ -5179,6 +5185,46 @@ PHP_METHOD(DatePeriod, __set_state)
}
/* }}} */

/* {{{ */
PHP_METHOD(DatePeriod, __serialize)
{
zval *object = ZEND_THIS;
php_period_obj *period_obj;
HashTable *myht;

ZEND_PARSE_PARAMETERS_NONE();

period_obj = Z_PHPPERIOD_P(object);
DATE_CHECK_INITIALIZED(period_obj->start, DatePeriod);

array_init(return_value);
myht = Z_ARRVAL_P(return_value);
date_period_object_to_hash(period_obj, myht);
}
/* }}} */


/* {{{ */
PHP_METHOD(DatePeriod, __unserialize)
{
zval *object = ZEND_THIS;
php_period_obj *period_obj;
zval *array;
HashTable *myht;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(array)
ZEND_PARSE_PARAMETERS_END();

period_obj = Z_PHPPERIOD_P(object);
myht = Z_ARRVAL_P(array);

if (!php_date_period_initialize_from_hash(period_obj, myht)) {
zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
}
}
/* }}} */

/* {{{ */
PHP_METHOD(DatePeriod, __wakeup)
{
Expand Down
4 changes: 4 additions & 0 deletions ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ public function getDateInterval(): DateInterval {}
/** @tentative-return-type */
public function getRecurrences(): ?int {}

public function __serialize(): array;

public function __unserialize(array $data): void;

/** @tentative-return-type */
public function __wakeup(): void {}

Expand Down
10 changes: 9 additions & 1 deletion ext/date/php_date_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 4845891ab3872f292438de639953e2022f849125 */
* Stub hash: db6fd5b6c366dd8ee48ead99d235baa86e564401 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strtotime, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, datetime, IS_STRING, 0)
Expand Down Expand Up @@ -480,6 +480,10 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_DatePeriod_getRecurrences, 0, 0, IS_LONG, 1)
ZEND_END_ARG_INFO()

#define arginfo_class_DatePeriod___serialize arginfo_timezone_abbreviations_list

#define arginfo_class_DatePeriod___unserialize arginfo_class_DateTimeInterface___unserialize

#define arginfo_class_DatePeriod___wakeup arginfo_class_DateTimeInterface___wakeup

ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_DatePeriod___set_state, 0, 1, DatePeriod, 0)
Expand Down Expand Up @@ -575,6 +579,8 @@ ZEND_METHOD(DatePeriod, getStartDate);
ZEND_METHOD(DatePeriod, getEndDate);
ZEND_METHOD(DatePeriod, getDateInterval);
ZEND_METHOD(DatePeriod, getRecurrences);
ZEND_METHOD(DatePeriod, __serialize);
ZEND_METHOD(DatePeriod, __unserialize);
ZEND_METHOD(DatePeriod, __wakeup);
ZEND_METHOD(DatePeriod, __set_state);
ZEND_METHOD(DatePeriod, getIterator);
Expand Down Expand Up @@ -734,6 +740,8 @@ static const zend_function_entry class_DatePeriod_methods[] = {
ZEND_ME(DatePeriod, getEndDate, arginfo_class_DatePeriod_getEndDate, ZEND_ACC_PUBLIC)
ZEND_ME(DatePeriod, getDateInterval, arginfo_class_DatePeriod_getDateInterval, ZEND_ACC_PUBLIC)
ZEND_ME(DatePeriod, getRecurrences, arginfo_class_DatePeriod_getRecurrences, ZEND_ACC_PUBLIC)
ZEND_ME(DatePeriod, __serialize, arginfo_class_DatePeriod___serialize, ZEND_ACC_PUBLIC)
ZEND_ME(DatePeriod, __unserialize, arginfo_class_DatePeriod___unserialize, ZEND_ACC_PUBLIC)
ZEND_ME(DatePeriod, __wakeup, arginfo_class_DatePeriod___wakeup, ZEND_ACC_PUBLIC)
ZEND_ME(DatePeriod, __set_state, arginfo_class_DatePeriod___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_ME(DatePeriod, getIterator, arginfo_class_DatePeriod_getIterator, ZEND_ACC_PUBLIC)
Expand Down
160 changes: 160 additions & 0 deletions ext/date/tests/DatePeriod_serialize-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
--TEST--
Test DatePeriod::__serialize and DatePeriod::__unserialize (ISO String)
--FILE--
<?php
date_default_timezone_set("Europe/London");

$d = new DatePeriod('R4/2012-07-01T00:00:00Z/P7D');
echo "Original object:\n";
var_dump($d);

echo "\n\nSerialised object:\n";
$s = serialize($d);
var_dump($s);

echo "\n\nUnserialised object:\n";
$e = unserialize($s);
var_dump($e);

echo "\n\nCalling __serialize manually:\n";
var_dump($d->__serialize());

?>
--EXPECTF--
Original object:
object(DatePeriod)#1 (6) {
["start"]=>
object(DateTime)#2 (3) {
["date"]=>
string(26) "2012-07-01 00:00:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
["current"]=>
NULL
["end"]=>
NULL
["interval"]=>
object(DateInterval)#3 (10) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(7)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["f"]=>
float(0)
["invert"]=>
int(0)
["days"]=>
bool(false)
["from_string"]=>
bool(false)
}
["recurrences"]=>
int(5)
["include_start_date"]=>
bool(true)
}


Serialised object:
string(411) "O:10:"DatePeriod":6:{s:5:"start";O:8:"DateTime":3:{s:4:"date";s:26:"2012-07-01 00:00:00.000000";s:13:"timezone_type";i:1;s:8:"timezone";s:6:"+00:00";}s:7:"current";N;s:3:"end";N;s:8:"interval";O:12:"DateInterval":10:{s:1:"y";i:0;s:1:"m";i:0;s:1:"d";i:7;s:1:"h";i:0;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:6:"invert";i:0;s:4:"days";b:0;s:11:"from_string";b:0;}s:11:"recurrences";i:5;s:18:"include_start_date";b:1;}"


Unserialised object:
object(DatePeriod)#5 (6) {
["start"]=>
object(DateTime)#6 (3) {
["date"]=>
string(26) "2012-07-01 00:00:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
["current"]=>
NULL
["end"]=>
NULL
["interval"]=>
object(DateInterval)#4 (10) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(7)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["f"]=>
float(0)
["invert"]=>
int(0)
["days"]=>
bool(false)
["from_string"]=>
bool(false)
}
["recurrences"]=>
int(5)
["include_start_date"]=>
bool(true)
}


Calling __serialize manually:
array(6) {
["start"]=>
object(DateTime)#7 (3) {
["date"]=>
string(26) "2012-07-01 00:00:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
["current"]=>
NULL
["end"]=>
NULL
["interval"]=>
object(DateInterval)#8 (10) {
["y"]=>
int(0)
["m"]=>
int(0)
["d"]=>
int(7)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["f"]=>
float(0)
["invert"]=>
int(0)
["days"]=>
bool(false)
["from_string"]=>
bool(false)
}
["recurrences"]=>
int(5)
["include_start_date"]=>
bool(true)
}
Loading