3 declare(strict_types
=1);
5 namespace Shaarli\Helper
;
10 use Shaarli\Bookmark\Bookmark
;
11 use Slim\Http\Request
;
15 public const MONTH
= 'month';
16 public const WEEK
= 'week';
17 public const DAY
= 'day';
20 * Extracts the type of the daily to display from the HTTP request parameters
22 * @param Request $request HTTP request
24 * @return string month/week/day
26 public static function extractRequestedType(Request
$request): string
28 if ($request->getQueryParam(static::MONTH
) !== null) {
30 } elseif ($request->getQueryParam(static::WEEK
) !== null) {
38 * Extracts a DateTimeImmutable from provided HTTP request.
39 * If no parameter is provided, we rely on the creation date of the latest provided created bookmark.
40 * If the datastore is empty or no bookmark is provided, we use the current date.
42 * @param string $type month/week/day
43 * @param string|null $requestedDate Input string extracted from the request
44 * @param Bookmark|null $latestBookmark Latest bookmark found in the datastore (by date)
46 * @return DateTimeImmutable from input or latest bookmark.
48 * @throws Exception Type not supported.
50 public static function extractRequestedDateTime(
52 ?string $requestedDate,
53 Bookmark
$latestBookmark = null
54 ): DateTimeImmutable
{
55 $format = static::getFormatByType($type);
56 if (empty($requestedDate)) {
57 return $latestBookmark instanceof Bookmark
58 ? new DateTimeImmutable($latestBookmark->getCreated()->format(\DateTime
::ATOM
))
59 : new DateTimeImmutable()
63 // W is not supported by createFromFormat...
64 if ($type === static::WEEK
) {
65 return (new DateTimeImmutable())
66 ->setISODate((int) substr($requestedDate, 0, 4), (int) substr($requestedDate, 4, 2))
70 return DateTimeImmutable
::createFromFormat($format, $requestedDate);
74 * Get the DateTime format used by provided type
76 * - day: 20201016 (<year><month><day>)
77 * - week: 202041 (<year><week number>)
78 * - month: 202010 (<year><month>)
80 * @param string $type month/week/day
82 * @return string DateTime compatible format
84 * @see https://www.php.net/manual/en/datetime.format.php
86 * @throws Exception Type not supported.
88 public static function getFormatByType(string $type): string
98 throw new Exception('Unsupported daily format type');
103 * Get the first DateTime of the time period depending on given datetime and type.
104 * Note: DateTimeImmutable is required because we rely heavily on DateTime->modify() syntax
105 * and we don't want to alter original datetime.
107 * @param string $type month/week/day
108 * @param DateTimeImmutable $requested DateTime extracted from request input
109 * (should come from extractRequestedDateTime)
111 * @return \DateTimeInterface First DateTime of the time period
113 * @throws Exception Type not supported.
115 public static function getStartDateTimeByType(string $type, DateTimeImmutable
$requested): \DateTimeInterface
119 return $requested->modify('first day of this month midnight');
121 return $requested->modify('Monday this week midnight');
123 return $requested->modify('Today midnight');
125 throw new Exception('Unsupported daily format type');
130 * Get the last DateTime of the time period depending on given datetime and type.
131 * Note: DateTimeImmutable is required because we rely heavily on DateTime->modify() syntax
132 * and we don't want to alter original datetime.
134 * @param string $type month/week/day
135 * @param DateTimeImmutable $requested DateTime extracted from request input
136 * (should come from extractRequestedDateTime)
138 * @return \DateTimeInterface Last DateTime of the time period
140 * @throws Exception Type not supported.
142 public static function getEndDateTimeByType(string $type, DateTimeImmutable
$requested): \DateTimeInterface
146 return $requested->modify('last day of this month 23:59:59');
148 return $requested->modify('Sunday this week 23:59:59');
150 return $requested->modify('Today 23:59:59');
152 throw new Exception('Unsupported daily format type');
157 * Get localized description of the time period depending on given datetime and type.
158 * Example: for a month period, it returns `October, 2020`.
160 * @param string $type month/week/day
161 * @param \DateTimeImmutable $requested DateTime extracted from request input
162 * (should come from extractRequestedDateTime)
163 * @param bool $includeRelative Include relative date description (today, yesterday, etc.)
165 * @return string Localized time period description
167 * @throws Exception Type not supported.
169 public static function getDescriptionByType(
171 \DateTimeImmutable
$requested,
172 bool $includeRelative = true
176 return $requested->format('F') . ', ' . $requested->format('Y');
178 $requested = $requested->modify('Monday this week');
179 return t('Week') . ' ' . $requested->format('W') . ' (' . format_date($requested, false) . ')';
182 if ($includeRelative && $requested->format('Ymd') === date('Ymd')) {
183 $out = t('Today') . ' - ';
184 } elseif ($includeRelative && $requested->format('Ymd') === date('Ymd', strtotime('-1 days'))) {
185 $out = t('Yesterday') . ' - ';
187 return $out . format_date($requested, false);
189 throw new Exception('Unsupported daily format type');
194 * Get the number of items to display in the RSS feed depending on the given type.
196 * @param string $type month/week/day
198 * @return int number of elements
200 * @throws Exception Type not supported.
202 public static function getRssLengthByType(string $type): int
208 return 26; // ~6 months
210 return 30; // ~1 month
212 throw new Exception('Unsupported daily format type');
217 * Get the number of items to display in the RSS feed depending on the given type.
219 * @param string $type month/week/day
220 * @param ?DateTimeImmutable $requested Currently only used for UT
222 * @return DatePeriod number of elements
224 * @throws Exception Type not supported.
226 public static function getCacheDatePeriodByType(string $type, DateTimeImmutable
$requested = null): DatePeriod
228 $requested = $requested ?? new DateTimeImmutable();
230 return new DatePeriod(
231 static::getStartDateTimeByType($type, $requested),
232 new \
DateInterval('P1D'),
233 static::getEndDateTimeByType($type, $requested)