Skip to content

Commit d54bcbb

Browse files
authored
Add DatePeriod's __serialize and __unserialize methods (#8464)
1 parent da857c9 commit d54bcbb

9 files changed

+857
-13
lines changed

ext/date/php_date.c

+56-10
Original file line numberDiff line numberDiff line change
@@ -5015,17 +5015,9 @@ static HashTable *date_object_get_gc_period(zend_object *object, zval **table, i
50155015
return zend_std_get_properties(object);
50165016
} /* }}} */
50175017

5018-
static HashTable *date_object_get_properties_period(zend_object *object) /* {{{ */
5018+
static void date_period_object_to_hash(php_period_obj *period_obj, HashTable *props)
50195019
{
5020-
HashTable *props;
5021-
zval zv;
5022-
php_period_obj *period_obj;
5023-
5024-
period_obj = php_period_obj_from_obj(object);
5025-
props = zend_std_get_properties(object);
5026-
if (!period_obj->start) {
5027-
return props;
5028-
}
5020+
zval zv;
50295021

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

50755067
ZVAL_BOOL(&zv, period_obj->include_start_date);
50765068
zend_hash_str_update(props, "include_start_date", sizeof("include_start_date")-1, &zv);
5069+
}
5070+
5071+
static HashTable *date_object_get_properties_period(zend_object *object) /* {{{ */
5072+
{
5073+
HashTable *props;
5074+
php_period_obj *period_obj;
5075+
5076+
period_obj = php_period_obj_from_obj(object);
5077+
props = zend_std_get_properties(object);
5078+
if (!period_obj->start) {
5079+
return props;
5080+
}
5081+
5082+
date_period_object_to_hash(period_obj, props);
50775083

50785084
return props;
50795085
} /* }}} */
@@ -5179,6 +5185,46 @@ PHP_METHOD(DatePeriod, __set_state)
51795185
}
51805186
/* }}} */
51815187

5188+
/* {{{ */
5189+
PHP_METHOD(DatePeriod, __serialize)
5190+
{
5191+
zval *object = ZEND_THIS;
5192+
php_period_obj *period_obj;
5193+
HashTable *myht;
5194+
5195+
ZEND_PARSE_PARAMETERS_NONE();
5196+
5197+
period_obj = Z_PHPPERIOD_P(object);
5198+
DATE_CHECK_INITIALIZED(period_obj->start, DatePeriod);
5199+
5200+
array_init(return_value);
5201+
myht = Z_ARRVAL_P(return_value);
5202+
date_period_object_to_hash(period_obj, myht);
5203+
}
5204+
/* }}} */
5205+
5206+
5207+
/* {{{ */
5208+
PHP_METHOD(DatePeriod, __unserialize)
5209+
{
5210+
zval *object = ZEND_THIS;
5211+
php_period_obj *period_obj;
5212+
zval *array;
5213+
HashTable *myht;
5214+
5215+
ZEND_PARSE_PARAMETERS_START(1, 1)
5216+
Z_PARAM_ARRAY(array)
5217+
ZEND_PARSE_PARAMETERS_END();
5218+
5219+
period_obj = Z_PHPPERIOD_P(object);
5220+
myht = Z_ARRVAL_P(array);
5221+
5222+
if (!php_date_period_initialize_from_hash(period_obj, myht)) {
5223+
zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
5224+
}
5225+
}
5226+
/* }}} */
5227+
51825228
/* {{{ */
51835229
PHP_METHOD(DatePeriod, __wakeup)
51845230
{

ext/date/php_date.stub.php

+4
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,10 @@ public function getDateInterval(): DateInterval {}
513513
/** @tentative-return-type */
514514
public function getRecurrences(): ?int {}
515515

516+
public function __serialize(): array;
517+
518+
public function __unserialize(array $data): void;
519+
516520
/** @tentative-return-type */
517521
public function __wakeup(): void {}
518522

ext/date/php_date_arginfo.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 4845891ab3872f292438de639953e2022f849125 */
2+
* Stub hash: db6fd5b6c366dd8ee48ead99d235baa86e564401 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strtotime, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
55
ZEND_ARG_TYPE_INFO(0, datetime, IS_STRING, 0)
@@ -480,6 +480,10 @@ ZEND_END_ARG_INFO()
480480
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_DatePeriod_getRecurrences, 0, 0, IS_LONG, 1)
481481
ZEND_END_ARG_INFO()
482482

483+
#define arginfo_class_DatePeriod___serialize arginfo_timezone_abbreviations_list
484+
485+
#define arginfo_class_DatePeriod___unserialize arginfo_class_DateTimeInterface___unserialize
486+
483487
#define arginfo_class_DatePeriod___wakeup arginfo_class_DateTimeInterface___wakeup
484488

485489
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_DatePeriod___set_state, 0, 1, DatePeriod, 0)
@@ -575,6 +579,8 @@ ZEND_METHOD(DatePeriod, getStartDate);
575579
ZEND_METHOD(DatePeriod, getEndDate);
576580
ZEND_METHOD(DatePeriod, getDateInterval);
577581
ZEND_METHOD(DatePeriod, getRecurrences);
582+
ZEND_METHOD(DatePeriod, __serialize);
583+
ZEND_METHOD(DatePeriod, __unserialize);
578584
ZEND_METHOD(DatePeriod, __wakeup);
579585
ZEND_METHOD(DatePeriod, __set_state);
580586
ZEND_METHOD(DatePeriod, getIterator);
@@ -734,6 +740,8 @@ static const zend_function_entry class_DatePeriod_methods[] = {
734740
ZEND_ME(DatePeriod, getEndDate, arginfo_class_DatePeriod_getEndDate, ZEND_ACC_PUBLIC)
735741
ZEND_ME(DatePeriod, getDateInterval, arginfo_class_DatePeriod_getDateInterval, ZEND_ACC_PUBLIC)
736742
ZEND_ME(DatePeriod, getRecurrences, arginfo_class_DatePeriod_getRecurrences, ZEND_ACC_PUBLIC)
743+
ZEND_ME(DatePeriod, __serialize, arginfo_class_DatePeriod___serialize, ZEND_ACC_PUBLIC)
744+
ZEND_ME(DatePeriod, __unserialize, arginfo_class_DatePeriod___unserialize, ZEND_ACC_PUBLIC)
737745
ZEND_ME(DatePeriod, __wakeup, arginfo_class_DatePeriod___wakeup, ZEND_ACC_PUBLIC)
738746
ZEND_ME(DatePeriod, __set_state, arginfo_class_DatePeriod___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
739747
ZEND_ME(DatePeriod, getIterator, arginfo_class_DatePeriod_getIterator, ZEND_ACC_PUBLIC)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
--TEST--
2+
Test DatePeriod::__serialize and DatePeriod::__unserialize (ISO String)
3+
--FILE--
4+
<?php
5+
date_default_timezone_set("Europe/London");
6+
7+
$d = new DatePeriod('R4/2012-07-01T00:00:00Z/P7D');
8+
echo "Original object:\n";
9+
var_dump($d);
10+
11+
echo "\n\nSerialised object:\n";
12+
$s = serialize($d);
13+
var_dump($s);
14+
15+
echo "\n\nUnserialised object:\n";
16+
$e = unserialize($s);
17+
var_dump($e);
18+
19+
echo "\n\nCalling __serialize manually:\n";
20+
var_dump($d->__serialize());
21+
22+
?>
23+
--EXPECTF--
24+
Original object:
25+
object(DatePeriod)#1 (6) {
26+
["start"]=>
27+
object(DateTime)#2 (3) {
28+
["date"]=>
29+
string(26) "2012-07-01 00:00:00.000000"
30+
["timezone_type"]=>
31+
int(1)
32+
["timezone"]=>
33+
string(6) "+00:00"
34+
}
35+
["current"]=>
36+
NULL
37+
["end"]=>
38+
NULL
39+
["interval"]=>
40+
object(DateInterval)#3 (10) {
41+
["y"]=>
42+
int(0)
43+
["m"]=>
44+
int(0)
45+
["d"]=>
46+
int(7)
47+
["h"]=>
48+
int(0)
49+
["i"]=>
50+
int(0)
51+
["s"]=>
52+
int(0)
53+
["f"]=>
54+
float(0)
55+
["invert"]=>
56+
int(0)
57+
["days"]=>
58+
bool(false)
59+
["from_string"]=>
60+
bool(false)
61+
}
62+
["recurrences"]=>
63+
int(5)
64+
["include_start_date"]=>
65+
bool(true)
66+
}
67+
68+
69+
Serialised object:
70+
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;}"
71+
72+
73+
Unserialised object:
74+
object(DatePeriod)#5 (6) {
75+
["start"]=>
76+
object(DateTime)#6 (3) {
77+
["date"]=>
78+
string(26) "2012-07-01 00:00:00.000000"
79+
["timezone_type"]=>
80+
int(1)
81+
["timezone"]=>
82+
string(6) "+00:00"
83+
}
84+
["current"]=>
85+
NULL
86+
["end"]=>
87+
NULL
88+
["interval"]=>
89+
object(DateInterval)#4 (10) {
90+
["y"]=>
91+
int(0)
92+
["m"]=>
93+
int(0)
94+
["d"]=>
95+
int(7)
96+
["h"]=>
97+
int(0)
98+
["i"]=>
99+
int(0)
100+
["s"]=>
101+
int(0)
102+
["f"]=>
103+
float(0)
104+
["invert"]=>
105+
int(0)
106+
["days"]=>
107+
bool(false)
108+
["from_string"]=>
109+
bool(false)
110+
}
111+
["recurrences"]=>
112+
int(5)
113+
["include_start_date"]=>
114+
bool(true)
115+
}
116+
117+
118+
Calling __serialize manually:
119+
array(6) {
120+
["start"]=>
121+
object(DateTime)#7 (3) {
122+
["date"]=>
123+
string(26) "2012-07-01 00:00:00.000000"
124+
["timezone_type"]=>
125+
int(1)
126+
["timezone"]=>
127+
string(6) "+00:00"
128+
}
129+
["current"]=>
130+
NULL
131+
["end"]=>
132+
NULL
133+
["interval"]=>
134+
object(DateInterval)#8 (10) {
135+
["y"]=>
136+
int(0)
137+
["m"]=>
138+
int(0)
139+
["d"]=>
140+
int(7)
141+
["h"]=>
142+
int(0)
143+
["i"]=>
144+
int(0)
145+
["s"]=>
146+
int(0)
147+
["f"]=>
148+
float(0)
149+
["invert"]=>
150+
int(0)
151+
["days"]=>
152+
bool(false)
153+
["from_string"]=>
154+
bool(false)
155+
}
156+
["recurrences"]=>
157+
int(5)
158+
["include_start_date"]=>
159+
bool(true)
160+
}

0 commit comments

Comments
 (0)