1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Fuel\Common |
4
|
|
|
* @version 2.0 |
5
|
|
|
* @author Fuel Development Team |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2010 - 2015 Fuel Development Team |
8
|
|
|
* @link http://fuelphp.com |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fuel\Common; |
12
|
|
|
|
13
|
|
|
use DatePeriod; |
14
|
|
|
use ArrayAccess; |
15
|
|
|
use DateInterval; |
16
|
|
|
use DateTime; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DatePeriod drop-in replacement, which provides read-only ArrayAccess to the object |
20
|
|
|
* |
21
|
|
|
* @package Fuel\Common |
22
|
|
|
* |
23
|
|
|
* @since 2.0 |
24
|
|
|
*/ |
25
|
|
|
class DateRange extends DatePeriod implements ArrayAccess |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Make the DatePeriod constructor more flexible |
29
|
|
|
*/ |
30
|
|
|
public function __construct($start, $interval = null, $end = null, $options = null) |
31
|
|
|
{ |
32
|
|
|
// deal with ISO string calls first |
33
|
|
|
if (func_num_args() < 3 and is_string($start)) |
34
|
|
|
{ |
35
|
|
|
parent::__construct($start, $interval); |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// make sure $start is a DateTime object |
40
|
|
|
if ( ! $start instanceOf DateTime) |
41
|
|
|
{ |
42
|
|
|
// use Date instead of DateTime, it is more flexible |
43
|
|
|
$start = new Date($start); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// we need a DateTime object to continue |
47
|
|
|
if ($start instanceOf Date) |
48
|
|
|
{ |
49
|
|
|
$start = $start->getDateTime(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// make sure $interval is a DateInterval object |
53
|
|
|
if ( ! $interval instanceOf DateInterval) |
54
|
|
|
{ |
55
|
|
|
$interval = DateInterval::createFromDateString($interval); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// make sure $end is a DateTime object |
59
|
|
|
if ( ! $end instanceOf DateTime) |
60
|
|
|
{ |
61
|
|
|
// recurrences limited to 10000, any larger and we assume its a timestamp |
62
|
|
|
if (is_numeric($end) and $end > 10000) |
63
|
|
|
{ |
64
|
|
|
// use Date instead of DateTime, it is more flexible |
65
|
|
|
$end = new Date('@'.$end); |
66
|
|
|
} |
67
|
|
|
elseif (is_string($end)) |
68
|
|
|
{ |
69
|
|
|
$end = new Date($end); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// we need a DateTime object to continue |
74
|
|
|
if ($end instanceOf Date) |
75
|
|
|
{ |
76
|
|
|
$end = $end->getDateTime(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
parent::__construct($start, $interval, $end, $options); |
80
|
|
|
} |
81
|
|
|
/** |
82
|
|
|
* Not implemented |
83
|
|
|
*/ |
84
|
|
|
public function offsetSet($offset, $value) |
85
|
|
|
{ |
86
|
|
|
// this object is read-only! |
87
|
|
|
throw new \RuntimeException('You can not set a value on a read-only DateRange object.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Check if a given object exists |
92
|
|
|
*/ |
93
|
|
|
public function offsetExists($offset) |
94
|
|
|
{ |
95
|
|
|
foreach ($this as $key => $value) |
96
|
|
|
{ |
97
|
|
|
if ($key == $offset) |
98
|
|
|
{ |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Not implemented |
107
|
|
|
*/ |
108
|
|
|
public function offsetUnset($offset) |
109
|
|
|
{ |
110
|
|
|
// this object is read-only! |
111
|
|
|
throw new \RuntimeException('You can not unset a value from a read-only DateRange object.'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* |
116
|
|
|
*/ |
117
|
|
|
public function offsetGet($offset) |
118
|
|
|
{ |
119
|
|
|
foreach ($this as $key => $value) |
120
|
|
|
{ |
121
|
|
|
if ($key == $offset) |
122
|
|
|
{ |
123
|
|
|
return $value; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|