ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
DateFormat.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28{
29 public const DOT = '.';
30 public const COMMA = ',';
31 public const DASH = '-';
32 public const SLASH = '/';
33 public const SPACE = ' ';
34 public const DAY = 'd';
35 public const DAY_ORDINAL = 'jS';
36 public const WEEKDAY = 'l';
37 public const WEEKDAY_SHORT = 'D';
38 public const WEEK = 'W';
39 public const MONTH = 'm';
40 public const MONTH_SPELLED = 'F';
41 public const MONTH_SPELLED_SHORT = 'M';
42 public const YEAR = 'Y';
43 public const YEAR_TWO_DIG = 'y';
44 public const HOURS12 = 'h';
45 public const HOURS24 = 'H';
46 public const MINUTES = 'i';
47 public const SECONDS = 's';
48 public const MERIDIEM = 'a';
49 public const COLON = ':';
50
51 public const TOKENS = [
73 ];
74
76 protected array $format = [];
77
78 public function __construct(array $format)
79 {
80 $this->validateFormatElelements($format);
81 $this->format = $format;
82 }
83
84 public function validateFormatElelements(array $format): void
85 {
86 foreach ($format as $entry) {
87 if (!in_array($entry, self::TOKENS, true)) {
88 throw new \InvalidArgumentException("not a valid token for date-format", 1);
89 }
90 }
91 }
92
97 public function toArray(): array
98 {
99 return $this->format;
100 }
101
105 public function toString(): string
106 {
107 return implode('', $this->format);
108 }
109
110 public function __toString(): string
111 {
112 return $this->toString();
113 }
114
115 public function applyTo(\DateTimeImmutable $datetime): string
116 {
117 return $datetime->format($this->toString());
118 }
119}
$datetime
A Date Format provides a format definition akin to PHP's date formatting options, but stores the sing...
Definition: DateFormat.php:28
toArray()
Get the elements of the format as array.
Definition: DateFormat.php:97
toString()
Get the format as string.
Definition: DateFormat.php:105
validateFormatElelements(array $format)
Definition: DateFormat.php:84
applyTo(\DateTimeImmutable $datetime)
Definition: DateFormat.php:115