|
2 | 2 | /** |
3 | 3 | * @package ActiveRecord |
4 | 4 | */ |
| 5 | + |
5 | 6 | namespace ActiveRecord; |
6 | 7 |
|
7 | 8 | /** |
@@ -44,169 +45,156 @@ class DateTime extends \DateTime implements DateTimeInterface, \JsonSerializable |
44 | 45 | * Pre-defined format strings. |
45 | 46 | */ |
46 | 47 | public static $FORMATS = array( |
47 | | - 'db' => 'Y-m-d H:i:s', |
48 | | - 'number' => 'YmdHis', |
49 | | - 'time' => 'H:i', |
50 | | - 'short' => 'd M H:i', |
51 | | - 'long' => 'F d, Y H:i', |
52 | | - 'atom' => \DateTime::ATOM, |
53 | | - 'cookie' => \DateTime::COOKIE, |
| 48 | + 'db' => 'Y-m-d H:i:s', |
| 49 | + 'number' => 'YmdHis', |
| 50 | + 'time' => 'H:i', |
| 51 | + 'short' => 'd M H:i', |
| 52 | + 'long' => 'F d, Y H:i', |
| 53 | + 'atom' => \DateTime::ATOM, |
| 54 | + 'cookie' => \DateTime::COOKIE, |
54 | 55 | 'iso8601' => \DateTime::ISO8601, |
55 | | - 'rfc822' => \DateTime::RFC822, |
56 | | - 'rfc850' => \DateTime::RFC850, |
| 56 | + 'rfc822' => \DateTime::RFC822, |
| 57 | + 'rfc850' => \DateTime::RFC850, |
57 | 58 | 'rfc1036' => \DateTime::RFC1036, |
58 | 59 | 'rfc1123' => \DateTime::RFC1123, |
59 | 60 | 'rfc2822' => \DateTime::RFC2822, |
60 | 61 | 'rfc3339' => \DateTime::RFC3339, |
61 | | - 'rss' => \DateTime::RSS, |
62 | | - 'w3c' => \DateTime::W3C); |
| 62 | + 'rss' => \DateTime::RSS, |
| 63 | + 'w3c' => \DateTime::W3C); |
63 | 64 |
|
64 | 65 | private $model; |
65 | 66 | private $attribute_name; |
66 | 67 |
|
67 | | - public function attribute_of($model, $attribute_name) |
68 | | - { |
69 | | - $this->model = $model; |
70 | | - $this->attribute_name = $attribute_name; |
71 | | - } |
| 68 | + // ----------------------------------------------------------------------------------------------------------------- |
| 69 | + // Implementation overrides |
72 | 70 |
|
73 | | - /** |
74 | | - * Formats the DateTime to the specified format. |
75 | | - * |
76 | | - * <code> |
77 | | - * $datetime->format(); # uses the format defined in DateTime::$DEFAULT_FORMAT |
78 | | - * $datetime->format('short'); # d M H:i |
79 | | - * $datetime->format('Y-m-d'); # Y-m-d |
80 | | - * </code> |
81 | | - * |
82 | | - * @see FORMATS |
83 | | - * @see get_format |
84 | | - * @param string $format A format string accepted by get_format() |
85 | | - * @return string formatted date and time string |
86 | | - */ |
87 | | - public function format($format=null) |
| 71 | + public function add($interval): \DateTime |
88 | 72 | { |
89 | | - return parent::format(self::get_format($format)); |
| 73 | + $this->flag_dirty(); |
| 74 | + return parent::add($interval); |
90 | 75 | } |
91 | 76 |
|
92 | | - /** |
93 | | - * Returns the format string. |
94 | | - * |
95 | | - * If $format is a pre-defined format in $FORMATS it will return that otherwise |
96 | | - * it will assume $format is a format string itself. |
97 | | - * |
98 | | - * @see FORMATS |
99 | | - * @param string $format A pre-defined string format or a raw format string |
100 | | - * @return string a format string |
101 | | - */ |
102 | | - public static function get_format($format=null) |
103 | | - { |
104 | | - // use default format if no format specified |
105 | | - if (!$format) |
106 | | - $format = self::$DEFAULT_FORMAT; |
107 | | - |
108 | | - // format is a friendly |
109 | | - if (array_key_exists($format, self::$FORMATS)) |
110 | | - return self::$FORMATS[$format]; |
111 | | - |
112 | | - // raw format |
113 | | - return $format; |
114 | | - } |
115 | | - |
116 | | - /** |
117 | | - * This needs to be overriden so it returns an instance of this class instead of PHP's \DateTime. |
118 | | - * See http://php.net/manual/en/datetime.createfromformat.php |
119 | | - */ |
120 | | - public static function createFromFormat($format, $time, $tz = null) |
| 77 | + public static function createFromFormat($format, $time, $tz = null): \DateTime|false |
121 | 78 | { |
122 | 79 | $phpDate = $tz ? parent::createFromFormat($format, $time, $tz) : parent::createFromFormat($format, $time); |
123 | 80 | if (!$phpDate) |
124 | 81 | return false; |
125 | 82 | // convert to this class using the timestamp |
126 | | - $ourDate = new static(null, $phpDate->getTimezone()); |
| 83 | + $ourDate = new static('', $phpDate->getTimezone()); |
127 | 84 | $ourDate->setTimestamp($phpDate->getTimestamp()); |
128 | 85 | return $ourDate; |
129 | 86 | } |
130 | 87 |
|
131 | | - /** |
132 | | - * @inheritDoc |
133 | | - */ |
134 | | - public function jsonSerialize() |
135 | | - { |
136 | | - return $this->format("c"); |
137 | | - } |
| 88 | + // createFromImmutable |
138 | 89 |
|
139 | | - public function __toString() |
140 | | - { |
141 | | - return $this->format(); |
142 | | - } |
| 90 | + // createFromInterface |
143 | 91 |
|
144 | | - /** |
145 | | - * Handle PHP object `clone`. |
146 | | - * |
147 | | - * This makes sure that the object doesn't still flag an attached model as |
148 | | - * dirty after cloning the DateTime object and making modifications to it. |
149 | | - * |
150 | | - * @return void |
151 | | - */ |
152 | | - public function __clone() |
153 | | - { |
154 | | - $this->model = null; |
155 | | - $this->attribute_name = null; |
156 | | - } |
| 92 | + // getLastErrors |
157 | 93 |
|
158 | | - private function flag_dirty() |
| 94 | + public function modify($modify): \DateTime |
159 | 95 | { |
160 | | - if ($this->model) |
161 | | - $this->model->flag_dirty($this->attribute_name); |
| 96 | + $this->flag_dirty(); |
| 97 | + return parent::modify($modify); |
162 | 98 | } |
163 | 99 |
|
164 | | - public function setDate($year, $month, $day) |
| 100 | + // __set_state |
| 101 | + |
| 102 | + public function setDate($year, $month, $day): \DateTime |
165 | 103 | { |
166 | 104 | $this->flag_dirty(); |
167 | 105 | return parent::setDate($year, $month, $day); |
168 | 106 | } |
169 | 107 |
|
170 | | - public function setISODate($year, $week , $day = 1) |
| 108 | + public function setISODate($year, $week, $day = 1): \DateTime |
171 | 109 | { |
172 | 110 | $this->flag_dirty(); |
173 | 111 | return parent::setISODate($year, $week, $day); |
174 | 112 | } |
175 | 113 |
|
176 | | - public function setTime($hour, $minute, $second = NULL, $microseconds = NULL) |
| 114 | + public function setTime($hour, $minute, $second = NULL, $microseconds = NULL): \DateTime |
177 | 115 | { |
178 | 116 | $this->flag_dirty(); |
179 | 117 | return parent::setTime($hour, $minute, $second, $microseconds); |
180 | 118 | } |
181 | 119 |
|
182 | | - public function setTimestamp($unixtimestamp) |
| 120 | + public function setTimestamp($unixtimestamp): \DateTime |
183 | 121 | { |
184 | 122 | $this->flag_dirty(); |
185 | 123 | return parent::setTimestamp($unixtimestamp); |
186 | 124 | } |
187 | 125 |
|
188 | | - public function setTimezone($timezone) |
| 126 | + public function setTimezone($timezone): \DateTime |
189 | 127 | { |
190 | 128 | $this->flag_dirty(); |
191 | 129 | return parent::setTimezone($timezone); |
192 | 130 | } |
193 | 131 |
|
194 | | - public function modify($modify) |
| 132 | + public function sub($interval): \DateTime |
195 | 133 | { |
196 | 134 | $this->flag_dirty(); |
197 | | - return parent::modify($modify); |
| 135 | + return parent::sub($interval); |
198 | 136 | } |
199 | 137 |
|
200 | | - public function add($interval) |
| 138 | + // diff |
| 139 | + |
| 140 | + public function format($format = null): string |
201 | 141 | { |
202 | | - $this->flag_dirty(); |
203 | | - return parent::add($interval); |
| 142 | + return parent::format(self::get_format($format)); |
204 | 143 | } |
205 | 144 |
|
206 | | - public function sub($interval) |
| 145 | + // getOffset |
| 146 | + |
| 147 | + // getTimestamp |
| 148 | + |
| 149 | + // getTimezone |
| 150 | + |
| 151 | + // __wakeUp |
| 152 | + |
| 153 | + // ----------------------------------------------------------------------------------------------------------------- |
| 154 | + // Activerecord extensions |
| 155 | + |
| 156 | + public static function get_format($format = null) |
207 | 157 | { |
208 | | - $this->flag_dirty(); |
209 | | - return parent::sub($interval); |
| 158 | + // use default format if no format specified |
| 159 | + if (!$format) |
| 160 | + $format = self::$DEFAULT_FORMAT; |
| 161 | + |
| 162 | + // format is a friendly |
| 163 | + if (array_key_exists($format, self::$FORMATS)) |
| 164 | + return self::$FORMATS[$format]; |
| 165 | + |
| 166 | + // raw format |
| 167 | + return $format; |
210 | 168 | } |
211 | 169 |
|
| 170 | + public function attribute_of($model, $attribute_name) |
| 171 | + { |
| 172 | + $this->model = $model; |
| 173 | + $this->attribute_name = $attribute_name; |
| 174 | + } |
| 175 | + |
| 176 | + private function flag_dirty() |
| 177 | + { |
| 178 | + if ($this->model) |
| 179 | + $this->model->flag_dirty($this->attribute_name); |
| 180 | + } |
| 181 | + |
| 182 | + public function __toString() |
| 183 | + { |
| 184 | + return $this->format(); |
| 185 | + } |
| 186 | + |
| 187 | + public function __clone() |
| 188 | + { |
| 189 | + $this->model = null; |
| 190 | + $this->attribute_name = null; |
| 191 | + } |
| 192 | + |
| 193 | + // ----------------------------------------------------------------------------------------------------------------- |
| 194 | + // JsonSerializable |
| 195 | + |
| 196 | + public function jsonSerialize(): mixed |
| 197 | + { |
| 198 | + return $this->format("c"); |
| 199 | + } |
212 | 200 | } |
0 commit comments