MutableDateTime.php
3.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
<?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;
use DateTime;
use DateTimeZone;
use InvalidArgumentException;
/**
* A mutable datetime instance that implements the ChronosInterface.
*
* This object can be mutated in place using any setter method,
* or __set().
*/
class MutableDateTime extends DateTime implements ChronosInterface
{
use Traits\ComparisonTrait;
use Traits\DifferenceTrait;
use Traits\FactoryTrait;
use Traits\FormattingTrait;
use Traits\MagicPropertyTrait;
use Traits\ModifierTrait;
use Traits\RelativeKeywordTrait;
use Traits\TestingAidTrait;
use Traits\TimezoneTrait;
/**
* Format to use for __toString method when type juggling occurs.
*
* @var string
*/
protected static $toStringFormat = ChronosInterface::DEFAULT_TO_STRING_FORMAT;
/**
* Create a new MutableDateTime instance.
*
* Please see the testing aids section (specifically static::setTestNow())
* for more on the possibility of this constructor returning a test instance.
*
* @param string|null $time Fixed or relative time
* @param DateTimeZone|string|null $tz The timezone for the instance
*/
public function __construct($time = 'now', $tz = null)
{
if ($tz !== null) {
$tz = $tz instanceof DateTimeZone ? $tz : new DateTimeZone($tz);
}
if (static::$testNow === null) {
return parent::__construct($time === null ? 'now' : $time, $tz);
}
$relative = static::hasRelativeKeywords($time);
if (!empty($time) && $time !== 'now' && !$relative) {
return parent::__construct($time, $tz);
}
$testInstance = clone static::getTestNow();
if ($relative) {
$testInstance = $testInstance;
$testInstance = $testInstance->modify($time);
}
if ($tz !== $testInstance->getTimezone()) {
$testInstance = $testInstance->setTimezone($tz === null ? date_default_timezone_get() : $tz);
}
$time = $testInstance->format('Y-m-d H:i:s.u');
parent::__construct($time, $tz);
}
/**
* Create a new immutable instance from current mutable instance.
*
* @return Chronos
*/
public function toImmutable()
{
return Chronos::instance($this);
}
/**
* Set a part of the ChronosInterface object
*
* @param string $name The property to set.
* @param string|int|DateTimeZone $value The value to set.
* @throws InvalidArgumentException
* @return void
*/
public function __set($name, $value)
{
switch ($name) {
case 'year':
$this->year($value);
break;
case 'month':
$this->month($value);
break;
case 'day':
$this->day($value);
break;
case 'hour':
$this->hour($value);
break;
case 'minute':
$this->minute($value);
break;
case 'second':
$this->second($value);
break;
case 'timestamp':
$this->timestamp($value);
break;
case 'timezone':
case 'tz':
$this->timezone($value);
break;
default:
throw new InvalidArgumentException(sprintf("Unknown setter '%s'", $name));
}
}
}