/
ArticleDatetime.php
223 lines (188 loc) · 6.19 KB
/
ArticleDatetime.php
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
<?php
namespace Newscoop;
/**
* The datetime object class used in the ArticleDatetime to pass date formats
* @author mihaibalaceanu
*/
class ArticleDatetime
{
protected $startDate = null;
protected $endDate = null;
protected $startTime = null;
protected $endTime = null;
protected $recurring = null;
protected $spawns = array();
/**
* @param mixed $format
* array of start_date, end_date, etc. keys
* array with 1 element: start_date => end_date strings - Date and Time formats: http://php.net/manual/en/datetime.formats.php
* string formatted like above and start separated by end with a - (dash) padded with spaces
* also in case of array you can add recurring => [daily|weekly|etc.]
* @param string $recurring
* @todo check start < end
*/
public function __construct($format, $recurring=null)
{
if (is_array($format))
{
if (array_key_exists('start_date', $format))
{
$this->setStartDate($format['start_date']);
$this->setEndDate(isset($format['end_date']) ? $format['end_date'] : null);
$this->setStartTime(isset($format['start_time']) ? $format['start_time'] : null);
$this->setEndTime(isset($format['end_time']) ? $format['end_time'] : null);
if (isset($format['recurring'])) {
$this->recurring = $format['recurring'];
}
return;
}
$start = key($format);
$end = current($format);
}
elseif (is_string($format))
{
list($start, $end) = explode(" - ", $format);
if (in_array( ($end = trim($end)), array( '1', '0', 'true', 'false'))) {
$end = (bool) $end;
}
}
$this->setRecurring($recurring);
if (!( $startTimestamp = strtotime($start))) {
return;
}
$parsedStart = date_parse($start);
$startHasDate = $parsedStart['month']!==false && $parsedStart['day']!==false;
$startHasTime = $parsedStart['hour']!==false && $parsedStart['minute']!==false;
$startDate = strftime('%F', $startTimestamp);
switch (true)
{
// TODO fix midnight for relative formats
case is_bool($end) && $end : // full day
$this->setStartDate($startDate);
$this->setStartTime($startHasTime ? strftime('%T', $startTimestamp) : null);
$this->setEndDate(null);
$this->setEndTime(null);
break;
case is_string($end) : // ...until date, or just a single time value for the date
$parsedEnd = date_parse($end);
$endHasDate = $parsedEnd['month']!==false && $parsedEnd['day']!==false;
$endHasTime = $parsedEnd['hour']!==false && $parsedEnd['minute']!==false;
$end = preg_replace("/-\s*recurring:(\w+)/", "", $end);
$this->setStartDate($startDate);
if ($endHasDate) {
$this->setEndDate(strftime('%F', strtotime($end)));
}
if ($endHasTime && ($parsedEnd['hour']!=0 || $parsedEnd['minute']!=0 || $parsedEnd['second']!=0)) {
$this->setEndTime(strftime('%T', strtotime($end)));
}
if ($startHasTime && ($parsedStart['hour']!=0 || $parsedStart['minute']!=0 || $parsedStart['second']!=0)) {
$this->setStartTime(strftime('%T', strtotime($start)));
}
break;
case is_array($end) : // time interval for current date
$spawn =& $this;
foreach ($end as $startTime => $endTime)
{
if ($startTime == 'recurring' ) { // TODO dirty fix for reccuring flag take out
continue;
}
$spawn->setStartDate($startDate);
$spawn->setEndDate(null);
$spawn->setStartTime(strftime('%T', strtotime($startTime)));
$spawn->setEndTime(strftime('%T', strtotime($endTime)));
$this->spawns[] = clone $spawn;
$spawn =& $this->spawns[count($this->spawns)-1];
};
array_pop($this->spawns);
break;
}
}
/**
* clears spawns
*/
public function __clone()
{
$this->spawns = array();
}
/**
* @param string $value date format
*/
public function setStartDate($value)
{
$this->startDate = is_null($value) ? null : new \DateTime($value);
}
/**
* @param string $value
*/
public function setStartTime($value)
{
$this->startTime = is_null($value) ? null : new \DateTime($value);
}
/**
* @param string $value
*/
public function setEndDate($value)
{
$this->endDate = is_null($value) ? null : new \DateTime($value);
}
/**
* @param string $value
*/
public function setEndTime($value)
{
$this->endTime = is_null($value) ? null : new \DateTime($value);
}
/**
* @return \DateTime
*/
public function getStartDate()
{
return $this->startDate;
}
/**
* @return \DateTime
*/
public function getStartTime()
{
return $this->startTime;
}
/**
* @return \DateTime
*/
public function getEndDate()
{
return $this->endDate;
}
/**
* @return \DateTime
*/
public function getEndTime()
{
return $this->endTime;
}
/**
* Set recurring flag
* @param string $value daily|weekly|monthly|yearly
*/
public function setRecurring($value)
{
if (!in_array( $value, array('daily', 'weekly', 'monthly', 'yearly'))) {
return false;
}
$this->recurring = $value;
}
/**
* Get if date object is recurring
*/
public function getRecurring()
{
return $this->recurring;
}
/**
* Get other spawned objects from a contruct
*/
public function getSpawns()
{
return $this->spawns;
}
}