-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathDateValueObject.php
167 lines (144 loc) · 3.82 KB
/
DateValueObject.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
<?php
declare(strict_types=1);
namespace Drupal\oe_theme\ValueObject;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem;
/**
* Handle information about a date/date interval, as expected by the ECL.
*/
class DateValueObject extends ValueObjectBase implements DateValueObjectInterface {
/**
* Start date.
*
* @var \Drupal\Core\Datetime\DrupalDateTime
*/
protected $start;
/**
* End date.
*
* @var \Drupal\Core\Datetime\DrupalDateTime
*/
protected $end;
/**
* DateValueObject constructor.
*
* @param int $start
* Start date as UNIX timestamp.
* @param int $end
* End date as UNIX timestamp.
* @param string|null $timezone
* Timezone string, e.g. "Europe/Brussels".
*/
private function __construct(int $start, ?int $end = NULL, ?string $timezone = NULL) {
$this->start = DrupalDateTime::createFromTimestamp($start, $timezone);
if ($end !== NULL) {
$this->end = DrupalDateTime::createFromTimestamp($end, $timezone);
}
}
/**
* {@inheritdoc}
*/
public static function fromArray(array $parameters = []): ValueObjectInterface {
$parameters += ['start' => NULL, 'end' => NULL, 'timezone' => NULL];
return new static(
$parameters['start'],
$parameters['end'],
$parameters['timezone']
);
}
/**
* {@inheritdoc}
*/
public static function fromDateRangeItem(DateRangeItem $dateRangeItem): DateValueObjectInterface {
return new static(
$dateRangeItem->get('start_date')->getValue()->getTimeStamp(),
$dateRangeItem->get('end_date')->getValue()->getTimeStamp(),
NULL
);
}
/**
* {@inheritdoc}
*/
public static function fromDateTimeItem(DateTimeItem $dateTimeItem): DateValueObjectInterface {
return new static(
$dateTimeItem->get('date')->getValue()->getTimeStamp(),
NULL,
NULL
);
}
/**
* {@inheritdoc}
*/
public function getArray(): array {
return [
'day' => $this->getDay(),
'month' => $this->getMonth(),
'year' => $this->getYear(),
'week_day' => $this->getWeekDay(),
'month_name' => $this->getMonthName(),
'month_fullname' => $this->getMonthFullName(),
];
}
/**
* {@inheritdoc}
*/
public function getDay(): string {
return $this->getDateInterval('d', 'm');
}
/**
* {@inheritdoc}
*/
public function getMonth(): string {
return $this->getDateInterval('m', 'Y');
}
/**
* {@inheritdoc}
*/
public function getYear(): string {
return $this->getDateInterval('Y', 'Y');
}
/**
* {@inheritdoc}
*/
public function getWeekDay(): string {
return $this->getDateInterval('D', 'm');
}
/**
* {@inheritdoc}
*/
public function getMonthName(): string {
return $this->getDateInterval('M', 'Y');
}
/**
* {@inheritdoc}
*/
public function getMonthFullName(): string {
return $this->getDateInterval('F', 'Y');
}
/**
* Get date interval as expected by the ECL.
*
* @param string $format
* Format to be used to print the interval.
* @param string $extra
* Run extra check to make sure we should not print an interval.
* This is useful, for example, in case of same days on different months
* which should still be printed as an interval.
*
* @return string
* The formatted interval.
*/
protected function getDateInterval(string $format, string $extra): string {
$date_interval = $this->start->format($format);
if (!empty($this->end) &&
(
$this->start->format($format) !== $this->end->format($format)
|| $this->start->format($extra) !== $this->end->format($extra)
)
) {
$date_interval .= '-' . $this->end->format($format);
}
return $date_interval;
}
}