1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* spindle/types |
4
|
|
|
* |
5
|
|
|
* @license CC0-1.0 (Public Domain) https://creativecommons.org/publicdomain/zero/1.0/ |
6
|
|
|
*/ |
7
|
|
|
namespace Spindle\Types\Polyfill; |
8
|
|
|
|
9
|
|
|
//@codeCoverageIgnoreStart |
10
|
|
|
if (class_exists('DateTimeImmutable', false)) { |
11
|
|
View Code Duplication |
class DateTimeImmutable extends \DateTimeImmutable implements DateTimeInterface |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
static function createFromFormat($format, $time, /* \DateTimeZone */ $timezone=null) |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
if ($timezone) { |
16
|
|
|
$dateTime = \date_create_from_format($format, $time, $timezone); |
17
|
|
|
} else { |
18
|
|
|
$dateTime = \date_create_from_format($format, $time); |
19
|
|
|
} |
20
|
|
|
return new static($dateTime->format('c')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
static function __set_state(array $array) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$dateTime = parent::__set_state($array); |
26
|
|
|
return new static($dateTime->format('c')); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} else { |
30
|
|
|
class DateTimeImmutable extends \DateTime implements DateTimeInterface |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
function add(/* \DateInterval */ $interval) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$new = clone $this; |
35
|
|
|
return \date_add($new, $interval); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
static function createFromFormat($format, $time, /* \DateTimeZone */ $timezone=null) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
if ($timezone) { |
41
|
|
|
$dateTime = \date_create_from_format($format, $time, $timezone); |
42
|
|
|
} else { |
43
|
|
|
$dateTime = \date_create_from_format($format, $time); |
44
|
|
|
} |
45
|
|
|
return new static($dateTime->format('c')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function modify($modify) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$new = clone $this; |
51
|
|
|
return \date_modify($new, $modify); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function setDate($year, $month, $day) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$new = clone $this; |
57
|
|
|
return \date_date_set($new, $year, $month, $day); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
function setISODate($year, $month, $day = 1) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$new = clone $this; |
63
|
|
|
return \date_isodate_set($new, $year, $month, $day); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
function setTime($hour, $minute, $second = 0) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$new = clone $this; |
69
|
|
|
return \date_time_set($new, $hour, $second); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
function setTimestamp($unixtimestamp) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$new = clone $this; |
75
|
|
|
return \date_timestamp_set($new, $unixtimestamp); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function setTimezone(/* \DateTimeZone */ $timezone) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$new = clone $this; |
81
|
|
|
return \date_timezone_set($new, $timezone); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
static function __set_state(array $array) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$dateTime = parent::__set_state($array); |
87
|
|
|
return new static($dateTime->format('c')); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
//@codeCoverageIgnoreEnd |
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.