ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DateTimeTransformation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27use UnexpectedValueException;
28use DateTimeImmutable;
29use Exception;
30
36{
40
41 private string $error = '';
42
46 public function transform($from): DateTimeImmutable
47 {
48 $this->check($from);
49 return new DateTimeImmutable($from);
50 }
51
55 public function getError(): string
56 {
57 return $this->error;
58 }
59
63 public function check($value)
64 {
65 if (!$this->accepts($value)) {
66 throw new UnexpectedValueException($this->getErrorMessage($value));
67 }
68
69 return null;
70 }
71
75 public function accepts($value): bool
76 {
77 try {
78 new DateTimeImmutable($value);
79 } catch (Exception $e) {
80 $this->error = $e->getMessage();
81 return false;
82 }
83 return true;
84 }
85
89 public function problemWith($value): ?string
90 {
91 if (!$this->accepts($value)) {
92 return $this->getErrorMessage($value);
93 }
94
95 return null;
96 }
97}
Transform a string representing a datetime-value to php's DateTimeImmutable see https://www....
error(string $a_errmsg)
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
getErrorMessage($value)