ILIAS  trunk Revision v11.0_alpha-1749-g1a06bdef097
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
DateTimeTransformation.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 use Exception;
30 
36 {
39  use ProblemBuilder;
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 }
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getErrorMessage($value)
Transform a string representing a datetime-value to php&#39;s DateTimeImmutable see https://www.php.net/manual/de/datetime.formats.php.