ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Error.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 
4 namespace ILIAS\Data\Result;
5 
6 use ILIAS\Data;
7 
13 class Error implements Data\Result
14 {
15 
19  protected $error;
20 
21  public function __construct($error)
22  {
23  if (!is_string($error) && !($error instanceof \Exception)) {
24  throw new \InvalidArgumentException("Expected error to be a string or an Exception.");
25  }
26  $this->error = $error;
27  }
31  public function isOK()
32  {
33  return false;
34  }
35 
39  public function value()
40  {
41  if ($this->error instanceof \Exception) {
42  throw $this->error;
43  }
44 
45  throw new Data\NotOKException($this->error);
46  }
47 
51  public function isError()
52  {
53  return true;
54  }
55 
59  public function error()
60  {
61  return $this->error;
62  }
63 
67  public function valueOr($default)
68  {
69  return $default;
70  }
71 
75  public function map(callable $f)
76  {
77  return $this;
78  }
79 
83  public function then(callable $f)
84  {
85  return $this;
86  }
87 
91  public function except(callable $f)
92  {
93  $result = $f($this->error);
94 
95  if ($result === null) {
96  return $this;
97  }
98 
99  if (!$result instanceof Data\Result) {
100  throw new \UnexpectedValueException("The returned type of callable is not an instance of interface Result");
101  }
102 
103  return $result;
104  }
105 }
$result
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:11
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:83
valueOr($default)
Get the encapsulated value or the supplied default if result is an error.mixed
Definition: Error.php:67
map(callable $f)
Create a new result where the contained value is modified with $f.Does nothing if !isOK...
Definition: Error.php:75
isError()
Get to know if the result is an error.bool
Definition: Error.php:51
value()
Get the encapsulated value.if !isOK, will either throw the contained exception or a NotOKException if...
Definition: Error.php:39
$default
Definition: build.php:20
isOK()
Get to know if the result is ok.bool
Definition: Error.php:31
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:91
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Error.php:13
error()
Get the encapsulated error.if isOK Exception|string
Definition: Error.php:59
Signals that a result contains no value.