Completed
Branch BUG-10209-session-encoding (a58b72)
by
unknown
108:10 queued 97:50
created

DbSafeDateTime::__wakeup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\domain\entities;
3
4
defined( 'ABSPATH' ) || exit;
5
6
7
8
/**
9
 * Class DbSafeDateTime
10
 * Some versions of PHP do bad things when you try to serialize a DateTime object for storage.
11
 * This DateTime class extension can be safely serialized and unserialized,
12
 * because the only data it stores is a string containing all o fits relevant details
13
 *
14
 * @package       Event Espresso
15
 * @author        Brent Christensen
16
 * @since         $VID:$
17
 */
18
class DbSafeDateTime extends \DateTime {
19
20
	/**
21
	 * @type string db_safe_timestamp_format
22
	 */
23
	const db_safe_timestamp_format = 'Y-m-d H:i:s O e';
24
25
	/**
26
	 * DateTime object converted to a string that includes the date, time, UTC offset, and timezone identifier
27
	 *
28
	 * @type string $_datetime_string
29
	 */
30
	protected $_datetime_string = '';
31
32
33
34
	public function __toString() {
35
		return $this->format( DbSafeDateTime::db_safe_timestamp_format );
36
	}
37
38
39
40
	public function __sleep() {
41
		$this->_datetime_string = $this->format( DbSafeDateTime::db_safe_timestamp_format );
42
		return array( '_datetime_string' );
43
	}
44
45
46
47
	public function __wakeup() {
48
		$date = \DateTime::createFromFormat( DbSafeDateTime::db_safe_timestamp_format, $this->_datetime_string );
49
		$this->__construct(
50
			$date->format( \EE_Datetime_Field::mysql_timestamp_format),
51
			new \DateTimeZone( $date->format( 'e' ) )
52
		);
53
	}
54
55
56
}
57
// End of file DbSafeDateTime.php
58
// Location: EventEspresso\core\domain\entities/DbSafeDateTime.php