ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DataHelper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24{
25 public function matchesDurationPattern(string $string): bool
26 {
27 return (bool) preg_match(Constants::DURATION_REGEX, $string);
28 }
29
30 public function matchesDatetimePattern(string $string): bool
31 {
32 return (bool) preg_match(Constants::DATETIME_REGEX, $string);
33 }
34
38 public function durationToIterator(string $duration): \Generator
39 {
40 if (!preg_match(
43 $matches,
44 PREG_UNMATCHED_AS_NULL
45 )) {
46 return;
47 }
48 yield from array_slice($matches, 1);
49 }
50
51 public function durationToSeconds(string $duration): int
52 {
53 $factors = [1, 12, 30, 24, 60, 60];
54 $factor_index = 0;
55 $result = 0;
56 foreach ($this->durationToIterator($duration) as $number) {
57 $result = $factors[$factor_index] * $result + $number;
58 $factor_index++;
59 }
60 return $result;
61 }
62
66 public function datetimeToIterator(string $datetime): \Generator
67 {
68 if (!preg_match(
71 $matches,
72 PREG_UNMATCHED_AS_NULL
73 )) {
74 return;
75 }
76 yield from array_slice($matches, 1);
77 }
78
79 public function datetimeToObject(string $datetime): \DateTimeImmutable
80 {
81 preg_match(
84 $matches,
85 PREG_UNMATCHED_AS_NULL
86 );
87 return new \DateTimeImmutable(
88 ($matches[1] ?? '0000') . '-' .
89 ($matches[2] ?? '01') . '-' .
90 ($matches[3] ?? '01')
91 );
92 }
93
94 public function durationFromIntegers(
95 ?int $years,
96 ?int $months,
97 ?int $days,
98 ?int $hours,
99 ?int $minutes,
100 ?int $seconds
101 ): string {
102 $has_time = !is_null($hours) || !is_null($minutes) || !is_null($seconds);
103
104 if (is_null($years) && is_null($months) && is_null($days) && !$has_time) {
105 return '';
106 }
107
108 $string = 'P';
109 if (!is_null($years)) {
110 $string .= max($years, 0) . 'Y';
111 }
112 if (!is_null($months)) {
113 $string .= max($months, 0) . 'M';
114 }
115 if (!is_null($days)) {
116 $string .= max($days, 0) . 'D';
117 }
118
119 if (!$has_time) {
120 return $string;
121 }
122 $string .= 'T';
123 if (!is_null($hours)) {
124 $string .= max($hours, 0) . 'H';
125 }
126 if (!is_null($minutes)) {
127 $string .= max($minutes, 0) . 'M';
128 }
129 if (!is_null($seconds)) {
130 $string .= max($seconds, 0) . 'S';
131 }
132
133 return $string;
134 }
135
136 public function datetimeFromObject(\DateTimeImmutable $object): string
137 {
138 return $object->format('Y-m-d');
139 }
140
144 public function getAllLanguages(): \Generator
145 {
147 }
148}
$datetime
$duration
const string DATETIME_REGEX
This monstrosity makes sure datetimes conform to the format given by LOM, and picks out the relevant ...
Definition: Constants.php:40
const array LANGUAGES
Note that 'xx' should be translated to 'none'.
Definition: Constants.php:47
const string DURATION_REGEX
This monstrosity makes sure durations conform to the format given by LOM, and picks out the relevant ...
Definition: Constants.php:30
durationFromIntegers(?int $years, ?int $months, ?int $days, ?int $hours, ?int $minutes, ?int $seconds)
Definition: DataHelper.php:94
datetimeFromObject(\DateTimeImmutable $object)
Note that LOM in ILIAS ignores the time part of any datetimes.
Definition: DataHelper.php:136
datetimeToIterator(string $datetime)
Definition: DataHelper.php:66
durationToIterator(string $duration)
Definition: DataHelper.php:38