3 declare(strict_types
=1);
5 namespace Shaarli\Helper
;
7 use Shaarli\Bookmark\Bookmark
;
12 public const MONTH
= 'month';
13 public const WEEK
= 'week';
14 public const DAY
= 'day';
17 * Extracts the type of the daily to display from the HTTP request parameters
19 * @param Request $request HTTP request
21 * @return string month/week/day
23 public static function extractRequestedType(Request
$request): string
25 if ($request->getQueryParam(static::MONTH
) !== null) {
27 } elseif ($request->getQueryParam(static::WEEK
) !== null) {
35 * Extracts a DateTimeImmutable from provided HTTP request.
36 * If no parameter is provided, we rely on the creation date of the latest provided created bookmark.
37 * If the datastore is empty or no bookmark is provided, we use the current date.
39 * @param string $type month/week/day
40 * @param string|null $requestedDate Input string extracted from the request
41 * @param Bookmark|null $latestBookmark Latest bookmark found in the datastore (by date)
43 * @return \DateTimeImmutable from input or latest bookmark.
45 * @throws \Exception Type not supported.
47 public static function extractRequestedDateTime(
49 ?string $requestedDate,
50 Bookmark
$latestBookmark = null
51 ): \DateTimeImmutable
{
52 $format = static::getFormatByType($type);
53 if (empty($requestedDate)) {
54 return $latestBookmark instanceof Bookmark
55 ? new \
DateTimeImmutable($latestBookmark->getCreated()->format(\DateTime
::ATOM
))
56 : new \
DateTimeImmutable()
60 // W is not supported by createFromFormat...
61 if ($type === static::WEEK
) {
62 return (new \
DateTimeImmutable())
63 ->setISODate((int) substr($requestedDate, 0, 4), (int) substr($requestedDate, 4, 2))
67 return \DateTimeImmutable
::createFromFormat($format, $requestedDate);
71 * Get the DateTime format used by provided type
73 * - day: 20201016 (<year><month><day>)
74 * - week: 202041 (<year><week number>)
75 * - month: 202010 (<year><month>)
77 * @param string $type month/week/day
79 * @return string DateTime compatible format
81 * @see https://www.php.net/manual/en/datetime.format.php
83 * @throws \Exception Type not supported.
85 public static function getFormatByType(string $type): string
95 throw new \
Exception('Unsupported daily format type');
100 * Get the first DateTime of the time period depending on given datetime and type.
101 * Note: DateTimeImmutable is required because we rely heavily on DateTime->modify() syntax
102 * and we don't want to alter original datetime.
104 * @param string $type month/week/day
105 * @param \DateTimeImmutable $requested DateTime extracted from request input
106 * (should come from extractRequestedDateTime)
108 * @return \DateTimeInterface First DateTime of the time period
110 * @throws \Exception Type not supported.
112 public static function getStartDateTimeByType(string $type, \DateTimeImmutable
$requested): \DateTimeInterface
116 return $requested->modify('first day of this month midnight');
118 return $requested->modify('Monday this week midnight');
120 return $requested->modify('Today midnight');
122 throw new \
Exception('Unsupported daily format type');
127 * Get the last DateTime of the time period depending on given datetime and type.
128 * Note: DateTimeImmutable is required because we rely heavily on DateTime->modify() syntax
129 * and we don't want to alter original datetime.
131 * @param string $type month/week/day
132 * @param \DateTimeImmutable $requested DateTime extracted from request input
133 * (should come from extractRequestedDateTime)
135 * @return \DateTimeInterface Last DateTime of the time period
137 * @throws \Exception Type not supported.
139 public static function getEndDateTimeByType(string $type, \DateTimeImmutable
$requested): \DateTimeInterface
143 return $requested->modify('last day of this month 23:59:59');
145 return $requested->modify('Sunday this week 23:59:59');
147 return $requested->modify('Today 23:59:59');
149 throw new \
Exception('Unsupported daily format type');
154 * Get localized description of the time period depending on given datetime and type.
155 * Example: for a month period, it returns `October, 2020`.
157 * @param string $type month/week/day
158 * @param \DateTimeImmutable $requested DateTime extracted from request input
159 * (should come from extractRequestedDateTime)
160 * @param bool $includeRelative Include relative date description (today, yesterday, etc.)
162 * @return string Localized time period description
164 * @throws \Exception Type not supported.
166 public static function getDescriptionByType(
168 \DateTimeImmutable
$requested,
169 bool $includeRelative = true
173 return $requested->format('F') . ', ' . $requested->format('Y');
175 $requested = $requested->modify('Monday this week');
176 return t('Week') . ' ' . $requested->format('W') . ' (' . format_date($requested, false) . ')';
179 if ($includeRelative && $requested->format('Ymd') === date('Ymd')) {
180 $out = t('Today') . ' - ';
181 } elseif ($includeRelative && $requested->format('Ymd') === date('Ymd', strtotime('-1 days'))) {
182 $out = t('Yesterday') . ' - ';
184 return $out . format_date($requested, false);
186 throw new \
Exception('Unsupported daily format type');
191 * Get the number of items to display in the RSS feed depending on the given type.
193 * @param string $type month/week/day
195 * @return int number of elements
197 * @throws \Exception Type not supported.
199 public static function getRssLengthByType(string $type): int
205 return 26; // ~6 months
207 return 30; // ~1 month
209 throw new \
Exception('Unsupported daily format type');