FactoryTrait.php
9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Traits;
use DateTimeInterface;
use DateTimeZone;
use InvalidArgumentException;
/**
* Provides a number of datetime related factory methods.
*/
trait FactoryTrait
{
/**
* Holds the last error generated by createFromFormat
*
* @var array
*/
protected static $_lastErrors = [];
/**
* Create a ChronosInterface instance from a DateTimeInterface one
*
* @param DateTimeInterface $dt The datetime instance to convert.
* @return static
*/
public static function instance(DateTimeInterface $dt)
{
if ($dt instanceof static) {
return clone $dt;
}
return new static($dt->format('Y-m-d H:i:s.u'), $dt->getTimeZone());
}
/**
* Create a ChronosInterface instance from a string. This is an alias for the
* constructor that allows better fluent syntax as it allows you to do
* ChronosInterface::parse('Monday next week')->fn() rather than
* (new Chronos('Monday next week'))->fn()
*
* @param string $time The strtotime compatible string to parse
* @param DateTimeZone|string|null $tz The DateTimeZone object or timezone name.
* @return $this
*/
public static function parse($time = 'now', $tz = null)
{
return new static($time, $tz);
}
/**
* Get a ChronosInterface instance for the current date and time
*
* @param DateTimeZone|string|null $tz The DateTimeZone object or timezone name.
* @return static
*/
public static function now($tz = null)
{
return new static('now', $tz);
}
/**
* Create a ChronosInterface instance for today
*
* @param DateTimeZone|string|null $tz The timezone to use.
* @return static
*/
public static function today($tz = null)
{
return new static('midnight', $tz);
}
/**
* Create a ChronosInterface instance for tomorrow
*
* @param DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function tomorrow($tz = null)
{
return new static('tomorrow, midnight', $tz);
}
/**
* Create a ChronosInterface instance for yesterday
*
* @param DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function yesterday($tz = null)
{
return new static('yesterday, midnight', $tz);
}
/**
* Create a ChronosInterface instance for the greatest supported date.
*
* @return \Cake\Chronos\ChronosInterface
*/
public static function maxValue()
{
return static::createFromTimestamp(PHP_INT_MAX);
}
/**
* Create a ChronosInterface instance for the lowest supported date.
*
* @return \Cake\Chronos\ChronosInterface
*/
public static function minValue()
{
$max = PHP_INT_SIZE === 4 ? PHP_INT_MAX : PHP_INT_MAX / 10;
return static::createFromTimestamp(~$max);
}
/**
* Create a new ChronosInterface instance from a specific date and time.
*
* If any of $year, $month or $day are set to null their now() values
* will be used.
*
* If $hour is null it will be set to its now() value and the default values
* for $minute and $second will be their now() values.
* If $hour is not null then the default values for $minute and $second
* will be 0.
*
* @param int|null $year The year to create an instance with.
* @param int|null $month The month to create an instance with.
* @param int|null $day The day to create an instance with.
* @param int|null $hour The hour to create an instance with.
* @param int|null $minute The minute to create an instance with.
* @param int|null $second The second to create an instance with.
* @param DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function create($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null)
{
$year = ($year === null) ? date('Y') : $year;
$month = ($month === null) ? date('n') : $month;
$day = ($day === null) ? date('j') : $day;
if ($hour === null) {
$hour = date('G');
$minute = ($minute === null) ? date('i') : $minute;
$second = ($second === null) ? date('s') : $second;
} else {
$minute = ($minute === null) ? 0 : $minute;
$second = ($second === null) ? 0 : $second;
}
return static::createFromFormat('Y-n-j G:i:s', sprintf('%s-%s-%s %s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $tz);
}
/**
* Create a ChronosInterface instance from just a date. The time portion is set to now.
*
* @param int $year The year to create an instance with.
* @param int $month The month to create an instance with.
* @param int $day The day to create an instance with.
* @param DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromDate($year = null, $month = null, $day = null, $tz = null)
{
return static::create($year, $month, $day, null, null, null, $tz);
}
/**
* Create a ChronosInterface instance from just a time. The date portion is set to today.
*
* @param int|null $hour The hour to create an instance with.
* @param int|null $minute The minute to create an instance with.
* @param int|null $second The second to create an instance with.
* @param DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromTime($hour = null, $minute = null, $second = null, $tz = null)
{
return static::create(null, null, null, $hour, $minute, $second, $tz);
}
/**
* Create a ChronosInterface instance from a specific format
*
* @param string $format The date() compatible format string.
* @param string $time The formatted date string to interpret.
* @param DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
* @throws InvalidArgumentException
*/
public static function createFromFormat($format, $time, $tz = null)
{
if ($tz !== null) {
$dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
} else {
$dt = parent::createFromFormat($format, $time);
}
$errors = parent::getLastErrors();
if ($dt == false) {
throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors']));
}
$dt = static::instance($dt);
static::$_lastErrors = $errors;
return $dt;
}
/**
* Create a ChronosInterface instance from a timestamp
*
* @param int $timestamp The timestamp to create an instance from.
* @param DateTimeZone|string|null $tz The DateTimeZone object or timezone name the new instance should use.
* @return static
*/
public static function createFromTimestamp($timestamp, $tz = null)
{
return static::now($tz)->setTimestamp($timestamp);
}
/**
* Create a ChronosInterface instance from an UTC timestamp
*
* @param int $timestamp The UTC timestamp to create an instance from.
* @return static
*/
public static function createFromTimestampUTC($timestamp)
{
return new static('@' . $timestamp);
}
/**
* Creates a DateTimeZone from a string or a DateTimeZone
*
* @param DateTimeZone|string|null $object The value to convert.
* @return DateTimeZone
* @throws InvalidArgumentException
*/
protected static function safeCreateDateTimeZone($object)
{
if ($object === null) {
return new DateTimeZone(date_default_timezone_get());
}
if ($object instanceof DateTimeZone) {
return $object;
}
return new DateTimeZone($object);
}
/**
* Returns any errors or warnings that were found during the parsing
* of the last object created by this class.
*
* @return array
*/
public static function getLastErrors()
{
if (empty(static::$_lastErrors)) {
return parent::getLastErrors();
}
return static::$_lastErrors;
}
}