Skip to content

Commit b031a3b

Browse files
authored
Add __wakeup method to Time (#3557)
Add __wakeup method to Time
1 parent 6059c83 commit b031a3b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

system/I18n/Time.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@
5656
* Requires the intl PHP extension.
5757
*
5858
* @package CodeIgniter\I18n
59+
*
60+
* @property string $date
5961
*/
6062
class Time extends DateTime
6163
{
62-
6364
/**
6465
* @var \DateTimeZone
6566
*/
@@ -1371,4 +1372,21 @@ public function __isset($name): bool
13711372
return method_exists($this, $method);
13721373
}
13731374

1375+
//--------------------------------------------------------------------
1376+
1377+
/**
1378+
* This is called when we unserialize the Time object.
1379+
*/
1380+
public function __wakeup()
1381+
{
1382+
/**
1383+
* Prior to unserialization, this is a string.
1384+
*
1385+
* @var string $timezone
1386+
*/
1387+
$timezone = $this->timezone;
1388+
1389+
$this->timezone = new DateTimeZone($timezone);
1390+
parent::__construct($this->date, $this->timezone);
1391+
}
13741392
}

tests/system/I18n/TimeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,4 +1008,14 @@ public function testGetter()
10081008
$this->assertNull($time->weekOfWeek);
10091009
}
10101010

1011+
public function testUnserializeTimeObject()
1012+
{
1013+
$time1 = new Time('August 28, 2020 10:04:00pm', 'Asia/Manila', 'en');
1014+
$timeCache = serialize($time1);
1015+
$time2 = unserialize($timeCache);
1016+
1017+
$this->assertInstanceOf(Time::class, $time2);
1018+
$this->assertTrue($time2->equals($time1));
1019+
$this->assertEquals($time1, $time2);
1020+
}
10111021
}

0 commit comments

Comments
 (0)