ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Error.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Data;
25 
31 class Error implements Data\Result
32 {
36  protected $error;
37 
38  public function __construct($error)
39  {
40  if (!is_string($error) && !($error instanceof \Exception)) {
41  throw new \InvalidArgumentException("Expected error to be a string or an Exception.");
42  }
43  $this->error = $error;
44  }
45 
49  public function isOK(): bool
50  {
51  return false;
52  }
53 
57  public function value()
58  {
59  if ($this->error instanceof \Exception) {
60  throw $this->error;
61  }
62 
63  throw new Data\NotOKException($this->error);
64  }
65 
69  public function isError(): bool
70  {
71  return true;
72  }
73 
77  public function error()
78  {
79  return $this->error;
80  }
81 
85  public function valueOr($default)
86  {
87  return $default;
88  }
89 
93  public function map(callable $f): Result
94  {
95  return $this;
96  }
97 
101  public function then(callable $f): Result
102  {
103  return $this;
104  }
105 
109  public function except(callable $f): Result
110  {
111  $result = $f($this->error);
112 
113  if ($result === null) {
114  return $this;
115  }
116 
117  if (!$result instanceof Data\Result) {
118  throw new \UnexpectedValueException("The returned type of callable is not an instance of interface Result");
119  }
120 
121  return $result;
122  }
123 }
then(callable $f)
Get a new result from the callable or do nothing if this is an error.If null is returned from $f...
Definition: Error.php:101
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
valueOr($default)
Get the encapsulated value or the supplied default if result is an error.mixed
Definition: Error.php:85
map(callable $f)
Create a new result where the contained value is modified with $f.Does nothing if !isOK...
Definition: Error.php:93
isError()
Get to know if the result is an error.
Definition: Error.php:69
value()
Get the encapsulated value.mixed
Definition: Error.php:57
isOK()
Get to know if the result is ok.
Definition: Error.php:49
except(callable $f)
Feed the error into a callable and replace this with the result or do nothing if this is a value...
Definition: Error.php:109
error()
Get the encapsulated error.|string
Definition: Error.php:77