ILIAS  release_8 Revision v8.23
Constraint.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
26 use ILIAS\Data;
29 use ilLanguage;
30 
32 {
35  use ProblemBuilder;
36 
38  protected ilLanguage $lng;
40  protected $is_ok;
42  protected $error;
43 
56  public function __construct(callable $is_ok, $error, Data\Factory $data_factory, ilLanguage $lng)
57  {
58  $this->is_ok = $is_ok;
59  $this->error = $error;
60  $this->data_factory = $data_factory;
61  $this->lng = $lng;
62  }
63 
67  protected function getError()
68  {
69  return $this->error;
70  }
71 
75  final public function check($value)
76  {
77  if (!$this->accepts($value)) {
78  throw new \UnexpectedValueException($this->getErrorMessage($value));
79  }
80 
81  return null;
82  }
83 
87  final public function accepts($value): bool
88  {
89  return call_user_func($this->is_ok, $value);
90  }
91 
95  final public function problemWith($value): ?string
96  {
97  if (!$this->accepts($value)) {
98  return $this->getErrorMessage($value);
99  }
100 
101  return null;
102  }
103 
107  final public function applyTo(Result $result): Result
108  {
109  if ($result->isError()) {
110  return $result;
111  }
112 
113  $problem = $this->problemWith($result->value());
114  if ($problem !== null) {
115  $error = $this->data_factory->error($problem);
116  return $error;
117  }
118 
119  return $result;
120  }
121 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Constraint.php:21
value()
Get the encapsulated value.
isError()
Get to know if the result is an error.
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:14
__construct(callable $is_ok, $error, Data\Factory $data_factory, ilLanguage $lng)
If $error is a callable it needs to take two parameters:
Definition: Constraint.php:56
Builds data types.
Definition: Factory.php:20
getErrorMessage($value)
Get the problem message.