ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
DateTimeTransformation.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getErrorMessage($value)
Get the problem message.
Transform a string representing a datetime-value to php&#39;s DateTimeImmutable see https://www.php.net/manual/de/datetime.formats.php.