-
Notifications
You must be signed in to change notification settings - Fork 1
/
LargeTimestampsPlainTextFormatter.php
136 lines (116 loc) · 3.92 KB
/
LargeTimestampsPlainTextFormatter.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
<?php
/**
* Created by PhpStorm.
* User: nicolas
* Date: 27/03/15
* Time: 17:42
*/
namespace CultuurNet\CalendarSummary\Timestamps;
use IntlDateFormatter;
class LargeTimestampsPlainTextFormatter
{
private $fmt;
private $fmtWeekDayLong;
private $fmtWeekDayShort;
private $fmtTime;
public function __construct()
{
$this->fmt = new IntlDateFormatter(
'nl_BE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
date_default_timezone_get(),
IntlDateFormatter::GREGORIAN,
'd MMMM yyyy'
);
$this->fmtWeekDayLong = new IntlDateFormatter(
'nl_BE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
date_default_timezone_get(),
IntlDateFormatter::GREGORIAN,
'EEEE'
);
$this->fmtWeekDayShort = new IntlDateFormatter(
'nl_BE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
date_default_timezone_get(),
IntlDateFormatter::GREGORIAN,
'EEEEEE'
);
$this->fmtTime = new IntlDateFormatter(
'nl_BE',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
date_default_timezone_get(),
IntlDateFormatter::GREGORIAN,
'HH:mm'
);
}
public function format(
\CultureFeed_Cdb_Data_Calendar_TimestampList $timestampList
) {
$timestamps_count = iterator_count($timestampList);
$timestampList->rewind();
if ($timestamps_count == 1) {
$timestamp = $timestampList->current();
return $this->formatSingleTimestamp($timestamp);
} else {
return $this->formatMultipleTimestamps($timestampList, $timestamps_count);
}
}
public function formatSingleTimestamp($timestamp)
{
$date = $timestamp->getDate();
$intlDate = $this->fmt->format(strtotime($date));
$intlWeekDay = $this->fmtWeekDayLong->format(strtotime($date));
$startTime = $timestamp->getStartTime();
$intlStartTime = $this->fmtTime->format(strtotime($startTime));
$endTime = $timestamp->getEndTime();
$intlEndTime = $this->fmtTime->format(strtotime($endTime));
$output = $intlWeekDay . ' ' . $intlDate . PHP_EOL;
if (!empty($endTime)) {
$output .= 'van ';
} else {
$output .= 'om ';
}
$output .= $intlStartTime;
if (!empty($endTime)) {
$output .= ' tot ' . $intlEndTime;
}
return $output;
}
public function formatMultipleTimestamps($timestampList, $timestamps_count)
{
$today = strtotime(date('Y-m-d') . ' 00:00:00');
$output = '';
for ($i = 0; $i < $timestamps_count; $i++) {
$timestamp = $timestampList->current();
$date = $timestamp->getDate();
$intlDate = $this->fmt->format(strtotime($date));
$intlWeekDay = $this->fmtWeekDayShort->format(strtotime($date));
$startTime = $timestamp->getStartTime();
$intlStartTime = $this->fmtTime->format(strtotime($startTime));
$endTime = $timestamp->getEndTime();
$intlEndTime = $this->fmtTime->format(strtotime($endTime));
if (strtotime($date) >= $today) {
$output .= $intlWeekDay . ' ' . $intlDate . PHP_EOL;
if (!empty($endTime)) {
$output .= 'van ';
} else {
$output .= 'om ';
}
$output .= $intlStartTime;
if (!empty($endTime)) {
$output .= ' tot ' . $intlEndTime;
}
if ($i != $timestamps_count-1) {
$output .= PHP_EOL;
}
}
$timestampList->next();
}
return $output;
}
}