Completed
Branch BUG-11117-DbSafeDateTime (bcfb61)
by
unknown
42:47 queued 32:32
created
core/domain/entities/DbSafeDateTime.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -70,7 +70,7 @@
 block discarded – undo
70 70
 
71 71
 
72 72
     /**
73
-     * @return array
73
+     * @return string[]
74 74
      */
75 75
     public function __sleep()
76 76
     {
Please login to merge, or discard this patch.
Indentation   +149 added lines, -149 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
 use DomainException;
8 8
 
9 9
 if (! defined('EVENT_ESPRESSO_VERSION')) {
10
-    exit('No direct script access allowed');
10
+	exit('No direct script access allowed');
11 11
 }
12 12
 
13 13
 
@@ -25,154 +25,154 @@  discard block
 block discarded – undo
25 25
 class DbSafeDateTime extends DateTime
26 26
 {
27 27
 
28
-    /**
29
-     * @type string db_safe_timestamp_format
30
-     */
31
-    const db_safe_timestamp_format = 'Y-m-d H:i:s O e';
32
-
33
-    /**
34
-     * DateTime object converted to a string that includes the date, time, UTC offset, and timezone identifier
35
-     *
36
-     * @type string $_datetime_string
37
-     */
38
-    protected $_datetime_string = '';
39
-
40
-    /**
41
-     * where to write the error log to
42
-     *
43
-     * @type string $_error_log_dir
44
-     */
45
-    protected $_error_log_dir = '';
46
-
47
-
48
-
49
-    /**
50
-     * @param string $error_log_dir
51
-     */
52
-    public function setErrorLogDir($error_log_dir)
53
-    {
54
-        // if the folder path is writable, then except the path + filename, else keep empty
55
-        $this->_error_log_dir = is_writable(str_replace(basename($error_log_dir), '', $error_log_dir))
56
-            ? $error_log_dir
57
-            : '';
58
-    }
59
-
60
-
61
-
62
-    /**
63
-     * @return string
64
-     */
65
-    public function __toString()
66
-    {
67
-        return $this->format(DbSafeDateTime::db_safe_timestamp_format);
68
-    }
69
-
70
-
71
-
72
-    /**
73
-     * @return array
74
-     */
75
-    public function __sleep()
76
-    {
77
-        $this->_datetime_string = $this->format(DbSafeDateTime::db_safe_timestamp_format);
78
-        $date                   = DateTime::createFromFormat(DbSafeDateTime::db_safe_timestamp_format,
79
-            $this->_datetime_string);
80
-        if (! $date instanceof DateTime) {
81
-            try {
82
-                // we want a stack trace to determine where the malformed date came from, so...
83
-                throw new DomainException('');
84
-            } catch (DomainException $e) {
85
-                $stack_trace = $e->getTraceAsString();
86
-            }
87
-            $this->writeToErrorLog(
88
-                sprintf(
89
-                    __(
90
-                        'A valid DateTime could not be generated from "%1$s" because the following errors occurred: %2$s %3$s %2$s PHP version: %4$s %2$s Stack Trace: %5$s',
91
-                        'event_espresso'
92
-                    ),
93
-                    $this->_datetime_string,
94
-                    '<br />',
95
-                    print_r(DateTime::getLastErrors(), true),
96
-                    PHP_VERSION,
97
-                    $stack_trace
98
-                )
99
-            );
100
-        }
101
-        return array('_datetime_string');
102
-    }
103
-
104
-
105
-
106
-    /**
107
-     * if an empty or null value got saved to the db for a datetime,
108
-     * then some servers and/or PHP itself will incorrectly convert that date string
109
-     * resulting in "-0001-11-30" for the year-month-day.
110
-     * see the Notes section
111
-     *
112
-     * @link http://php.net/manual/en/datetime.formats.date.php
113
-     * We'll replace those with "0000-00-00" which will allow a valid DateTime object to be created,
114
-     * but still result in the internal date for that object being set to "-0001-11-30 10:00:00.000000".
115
-     * so we're no better off, but at least things won't go fatal on us.
116
-     */
117
-    public function __wakeup()
118
-    {
119
-        $this->_datetime_string = str_replace(
120
-            array('-0001-11-29', '-0001-11-30'),
121
-            '0000-00-00',
122
-            $this->_datetime_string
123
-        );
124
-        $date                   = DateTime::createFromFormat(DbSafeDateTime::db_safe_timestamp_format,
125
-            $this->_datetime_string);
126
-        if (! $date instanceof DateTime) {
127
-            $this->writeToErrorLog(
128
-                sprintf(
129
-                    __(
130
-                        'A valid DateTime could not be recreated from "%1$s" because the following errors occurred: %2$s %3$s %2$s PHP version: %4$s',
131
-                        'event_espresso'
132
-                    ),
133
-                    $this->_datetime_string,
134
-                    '<br />',
135
-                    print_r(DateTime::getLastErrors(), true),
136
-                    PHP_VERSION
137
-                )
138
-            );
139
-        } else {
140
-            $this->__construct(
141
-                $date->format(\EE_Datetime_Field::mysql_timestamp_format),
142
-                new DateTimeZone($date->format('e'))
143
-            );
144
-        }
145
-    }
146
-
147
-
148
-    /**
149
-     * Creates a DbSafeDateTime from ye old DateTime
150
-     *
151
-     * @param DateTime $datetime
152
-     * @return \EventEspresso\core\domain\entities\DbSafeDateTime
153
-     */
154
-    public static function createFromDateTime(DateTime $datetime)
155
-    {
156
-        return new DbSafeDateTime(
157
-            $datetime->format(\EE_Datetime_Field::mysql_timestamp_format),
158
-            new DateTimeZone($datetime->format('e'))
159
-        );
160
-    }
161
-
162
-
163
-    /**
164
-     * @param string $message
165
-     */
166
-    private function writeToErrorLog($message)
167
-    {
168
-        if (! empty($this->_error_log_dir)) {
169
-            /** @noinspection ForgottenDebugOutputInspection */
170
-            error_log($message, 3, $this->_error_log_dir);
171
-        } else {
172
-            /** @noinspection ForgottenDebugOutputInspection */
173
-            error_log($message);
174
-        }
175
-    }
28
+	/**
29
+	 * @type string db_safe_timestamp_format
30
+	 */
31
+	const db_safe_timestamp_format = 'Y-m-d H:i:s O e';
32
+
33
+	/**
34
+	 * DateTime object converted to a string that includes the date, time, UTC offset, and timezone identifier
35
+	 *
36
+	 * @type string $_datetime_string
37
+	 */
38
+	protected $_datetime_string = '';
39
+
40
+	/**
41
+	 * where to write the error log to
42
+	 *
43
+	 * @type string $_error_log_dir
44
+	 */
45
+	protected $_error_log_dir = '';
46
+
47
+
48
+
49
+	/**
50
+	 * @param string $error_log_dir
51
+	 */
52
+	public function setErrorLogDir($error_log_dir)
53
+	{
54
+		// if the folder path is writable, then except the path + filename, else keep empty
55
+		$this->_error_log_dir = is_writable(str_replace(basename($error_log_dir), '', $error_log_dir))
56
+			? $error_log_dir
57
+			: '';
58
+	}
59
+
60
+
61
+
62
+	/**
63
+	 * @return string
64
+	 */
65
+	public function __toString()
66
+	{
67
+		return $this->format(DbSafeDateTime::db_safe_timestamp_format);
68
+	}
69
+
70
+
71
+
72
+	/**
73
+	 * @return array
74
+	 */
75
+	public function __sleep()
76
+	{
77
+		$this->_datetime_string = $this->format(DbSafeDateTime::db_safe_timestamp_format);
78
+		$date                   = DateTime::createFromFormat(DbSafeDateTime::db_safe_timestamp_format,
79
+			$this->_datetime_string);
80
+		if (! $date instanceof DateTime) {
81
+			try {
82
+				// we want a stack trace to determine where the malformed date came from, so...
83
+				throw new DomainException('');
84
+			} catch (DomainException $e) {
85
+				$stack_trace = $e->getTraceAsString();
86
+			}
87
+			$this->writeToErrorLog(
88
+				sprintf(
89
+					__(
90
+						'A valid DateTime could not be generated from "%1$s" because the following errors occurred: %2$s %3$s %2$s PHP version: %4$s %2$s Stack Trace: %5$s',
91
+						'event_espresso'
92
+					),
93
+					$this->_datetime_string,
94
+					'<br />',
95
+					print_r(DateTime::getLastErrors(), true),
96
+					PHP_VERSION,
97
+					$stack_trace
98
+				)
99
+			);
100
+		}
101
+		return array('_datetime_string');
102
+	}
103
+
104
+
105
+
106
+	/**
107
+	 * if an empty or null value got saved to the db for a datetime,
108
+	 * then some servers and/or PHP itself will incorrectly convert that date string
109
+	 * resulting in "-0001-11-30" for the year-month-day.
110
+	 * see the Notes section
111
+	 *
112
+	 * @link http://php.net/manual/en/datetime.formats.date.php
113
+	 * We'll replace those with "0000-00-00" which will allow a valid DateTime object to be created,
114
+	 * but still result in the internal date for that object being set to "-0001-11-30 10:00:00.000000".
115
+	 * so we're no better off, but at least things won't go fatal on us.
116
+	 */
117
+	public function __wakeup()
118
+	{
119
+		$this->_datetime_string = str_replace(
120
+			array('-0001-11-29', '-0001-11-30'),
121
+			'0000-00-00',
122
+			$this->_datetime_string
123
+		);
124
+		$date                   = DateTime::createFromFormat(DbSafeDateTime::db_safe_timestamp_format,
125
+			$this->_datetime_string);
126
+		if (! $date instanceof DateTime) {
127
+			$this->writeToErrorLog(
128
+				sprintf(
129
+					__(
130
+						'A valid DateTime could not be recreated from "%1$s" because the following errors occurred: %2$s %3$s %2$s PHP version: %4$s',
131
+						'event_espresso'
132
+					),
133
+					$this->_datetime_string,
134
+					'<br />',
135
+					print_r(DateTime::getLastErrors(), true),
136
+					PHP_VERSION
137
+				)
138
+			);
139
+		} else {
140
+			$this->__construct(
141
+				$date->format(\EE_Datetime_Field::mysql_timestamp_format),
142
+				new DateTimeZone($date->format('e'))
143
+			);
144
+		}
145
+	}
146
+
147
+
148
+	/**
149
+	 * Creates a DbSafeDateTime from ye old DateTime
150
+	 *
151
+	 * @param DateTime $datetime
152
+	 * @return \EventEspresso\core\domain\entities\DbSafeDateTime
153
+	 */
154
+	public static function createFromDateTime(DateTime $datetime)
155
+	{
156
+		return new DbSafeDateTime(
157
+			$datetime->format(\EE_Datetime_Field::mysql_timestamp_format),
158
+			new DateTimeZone($datetime->format('e'))
159
+		);
160
+	}
161
+
162
+
163
+	/**
164
+	 * @param string $message
165
+	 */
166
+	private function writeToErrorLog($message)
167
+	{
168
+		if (! empty($this->_error_log_dir)) {
169
+			/** @noinspection ForgottenDebugOutputInspection */
170
+			error_log($message, 3, $this->_error_log_dir);
171
+		} else {
172
+			/** @noinspection ForgottenDebugOutputInspection */
173
+			error_log($message);
174
+		}
175
+	}
176 176
 
177 177
 
178 178
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@  discard block
 block discarded – undo
6 6
 use DateTimeZone;
7 7
 use DomainException;
8 8
 
9
-if (! defined('EVENT_ESPRESSO_VERSION')) {
9
+if ( ! defined('EVENT_ESPRESSO_VERSION')) {
10 10
     exit('No direct script access allowed');
11 11
 }
12 12
 
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
         $this->_datetime_string = $this->format(DbSafeDateTime::db_safe_timestamp_format);
78 78
         $date                   = DateTime::createFromFormat(DbSafeDateTime::db_safe_timestamp_format,
79 79
             $this->_datetime_string);
80
-        if (! $date instanceof DateTime) {
80
+        if ( ! $date instanceof DateTime) {
81 81
             try {
82 82
                 // we want a stack trace to determine where the malformed date came from, so...
83 83
                 throw new DomainException('');
@@ -121,9 +121,9 @@  discard block
 block discarded – undo
121 121
             '0000-00-00',
122 122
             $this->_datetime_string
123 123
         );
124
-        $date                   = DateTime::createFromFormat(DbSafeDateTime::db_safe_timestamp_format,
124
+        $date = DateTime::createFromFormat(DbSafeDateTime::db_safe_timestamp_format,
125 125
             $this->_datetime_string);
126
-        if (! $date instanceof DateTime) {
126
+        if ( ! $date instanceof DateTime) {
127 127
             $this->writeToErrorLog(
128 128
                 sprintf(
129 129
                     __(
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
      */
166 166
     private function writeToErrorLog($message)
167 167
     {
168
-        if (! empty($this->_error_log_dir)) {
168
+        if ( ! empty($this->_error_log_dir)) {
169 169
             /** @noinspection ForgottenDebugOutputInspection */
170 170
             error_log($message, 3, $this->_error_log_dir);
171 171
         } else {
Please login to merge, or discard this patch.