ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
Constraint.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 2017 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3 
5 
9 use ILIAS\Data;
12 
14 {
17  use ProblemBuilder;
18 
22  protected $data_factory;
23 
27  protected $lng;
28 
32  protected $is_ok;
33 
37  protected $error;
38 
48  public function __construct(callable $is_ok, $error, Data\Factory $data_factory, \ilLanguage $lng)
49  {
50  $this->is_ok = $is_ok;
51  $this->error = $error;
52  $this->data_factory = $data_factory;
53  $this->lng = $lng;
54  }
55 
59  protected function getError()
60  {
61  return $this->error;
62  }
63 
67  final public function check($value)
68  {
69  if (!$this->accepts($value)) {
70  throw new \UnexpectedValueException($this->getErrorMessage($value));
71  }
72 
73  return null;
74  }
75 
79  final public function accepts($value)
80  {
81  return call_user_func($this->is_ok, $value);
82  }
83 
87  final public function problemWith($value)
88  {
89  if (!$this->accepts($value)) {
90  return $this->getErrorMessage($value);
91  }
92 
93  return null;
94  }
95 
99  final public function applyTo(Result $result) : Result
100  {
101  if ($result->isError()) {
102  return $result;
103  }
104 
105  $problem = $this->problemWith($result->value());
106  if ($problem !== null) {
107  $error = $this->data_factory->error($problem);
108  return $error;
109  }
110 
111  return $result;
112  }
113 }
applyTo(Result $result)
Restricts a Result.Must do nothing with the result if $result->isError(). Must replace the result wit...
Definition: Constraint.php:99
accepts($value)
Tells if the provided value complies.bool
Definition: Constraint.php:79
value()
Get the encapsulated value.
isError()
Get to know if the result is an error.
$result
check($value)
Checks the provided value.Should not throw if accepts($value).
Definition: Constraint.php:67
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:11
Builds data types.
Definition: Factory.php:19
__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:48
getErrorMessage($value)
Get the problem message.
problemWith($value)
Tells what the problem with the provided value is.Should return null if accepts($value).string|null
Definition: Constraint.php:87