Skip to content
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

Add __wakeup method to Time #3557

Merged
merged 2 commits into from Aug 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion system/I18n/Time.php
Expand Up @@ -56,10 +56,11 @@
* Requires the intl PHP extension.
*
* @package CodeIgniter\I18n
*
* @property string $date
*/
class Time extends DateTime
{

/**
* @var \DateTimeZone
*/
Expand Down Expand Up @@ -1371,4 +1372,21 @@ public function __isset($name): bool
return method_exists($this, $method);
}

//--------------------------------------------------------------------

/**
* This is called when we unserialize the Time object.
*/
public function __wakeup()
{
/**
* Prior to unserialization, this is a string.
*
* @var string $timezone
*/
$timezone = $this->timezone;

$this->timezone = new DateTimeZone($timezone);
parent::__construct($this->date, $this->timezone);
}
}
10 changes: 10 additions & 0 deletions tests/system/I18n/TimeTest.php
Expand Up @@ -1008,4 +1008,14 @@ public function testGetter()
$this->assertNull($time->weekOfWeek);
}

public function testUnserializeTimeObject()
{
$time1 = new Time('August 28, 2020 10:04:00pm', 'Asia/Manila', 'en');
$timeCache = serialize($time1);
$time2 = unserialize($timeCache);

$this->assertInstanceOf(Time::class, $time2);
$this->assertTrue($time2->equals($time1));
$this->assertEquals($time1, $time2);
}
}