-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeline.php
185 lines (147 loc) · 5.11 KB
/
Timeline.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
<?php
declare(strict_types=1);
namespace Drupal\omnipedia_date\Service;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\omnipedia_date\Service\DateCollectionInterface;
use Drupal\omnipedia_date\Service\DateResolverInterface;
use Drupal\omnipedia_date\Service\DefinedDatesInterface;
use Drupal\omnipedia_date\Service\TimelineInterface;
use Drupal\omnipedia_date\Value\OmnipediaDateRange;
/**
* The Omnipedia timeline service.
*/
class Timeline implements TimelineInterface {
use StringTranslationTrait;
/**
* Service constructor; saves dependencies.
*
* @param \Drupal\omnipedia_date\Service\DateCollectionInterface $dateCollection
* The Omnipedia date collection service.
*
* @param \Drupal\omnipedia_date\Service\DateResolverInterface $dateResolver
* The Omnipedia date resolver service.
*
* @param \Drupal\omnipedia_date\Service\DefinedDatesInterface $definedDates
* The Omnipedia defined dates service.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $stringTranslation
* The Drupal string translation service.
*/
public function __construct(
protected readonly DateCollectionInterface $dateCollection,
protected readonly DateResolverInterface $dateResolver,
protected readonly DefinedDatesInterface $definedDates,
protected $stringTranslation,
) {}
/**
* {@inheritdoc}
*/
public function getDefinedDates(bool $includeUnpublished = false): array {
return $this->definedDates->get($includeUnpublished);
}
/**
* Convenience method to get a date object or resolve a string date/keyword.
*
* @param string|\Drupal\Component\Datetime\DateTimePlus $dateOrKeyword
* Must be one of:
*
* - A string date or keyword that can be resolved by
* \Drupal\omnipedia_date\Service\DateResolverInterface::resolve().
*
* - An instance of \Drupal\Component\Datetime\DateTimePlus.
*
* @param bool $includeUnpublished
* Whether to include dates that have only unpublished content.
*
* @return \Drupal\Component\Datetime\DateTimePlus
* A date object representing $date. If $date was provided as a date object,
* it will be returned as-is.
*
* @see \Drupal\omnipedia_date\Service\DateResolverInterface::resolve()
*/
protected function getDateObject(
string|DateTimePlus $dateOrKeyword, bool $includeUnpublished = false
): DateTimePlus {
if ($dateOrKeyword instanceof DateTimePlus) {
return $dateOrKeyword;
}
return $this->dateResolver->resolve(
$dateOrKeyword, $includeUnpublished
)->getDateObject();
}
/**
* {@inheritdoc}
*/
public function getDateFormatted(
string|DateTimePlus $dateOrKeyword = 'current', string $format = 'long'
): string|TranslatableMarkup {
if ($dateOrKeyword === 'first') {
return $this->t('First date');
} else if ($dateOrKeyword === 'last') {
return $this->t('Last date');
} else if ($dateOrKeyword instanceof DateTimePlus) {
/** @var \Drupal\omnipedia_date\Plugin\Omnipedia\Date\OmnipediaDateInterface */
$instance = $this->dateCollection->getFromDateTimeObject($dateOrKeyword);
} else {
/** @var \Drupal\omnipedia_date\Plugin\Omnipedia\Date\OmnipediaDateInterface */
$instance = $this->dateResolver->resolve($dateOrKeyword);
}
return $instance->format($format);
}
/**
* {@inheritdoc}
*/
public function isDateBetween(
string $date,
string $startDate,
string $endDate,
bool $includeUnpublished = false
): bool {
if (empty($date)) {
return true;
}
/** @var \Drupal\Component\Datetime\DateTimePlus */
$dateObject = $this->getDateObject($date, $includeUnpublished);
/** @var \Drupal\Component\Datetime\DateTimePlus */
$startDateObject = $this->getDateObject($startDate, $includeUnpublished);
/** @var \Drupal\Component\Datetime\DateTimePlus */
$endDateObject = $this->getDateObject($endDate, $includeUnpublished);
return (new OmnipediaDateRange(
$startDateObject, $endDateObject,
))->overlapsDate($dateObject);
}
/**
* {@inheritdoc}
*/
public function doDateRangesOverlap(
string $startDate1,
string $endDate1,
string $startDate2,
string $endDate2,
bool $includeUnpublished = false
): bool {
/** @var \Drupal\Component\Datetime\DateTimePlus */
$startDate1Object = $this->getDateObject(
$startDate1, $includeUnpublished
);
/** @var \Drupal\Component\Datetime\DateTimePlus */
$endDate1Object = $this->getDateObject(
$endDate1, $includeUnpublished
);
/** @var \Drupal\Component\Datetime\DateTimePlus */
$startDate2Object = $this->getDateObject(
$startDate2, $includeUnpublished
);
/** @var \Drupal\Component\Datetime\DateTimePlus */
$endDate2Object = $this->getDateObject(
$endDate2, $includeUnpublished
);
return (new OmnipediaDateRange(
$startDate1Object, $endDate1Object,
))->overlapsWithRange(new OmnipediaDateRange(
$startDate2Object, $endDate2Object,
));
}
}