ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Error.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data;
25
31class 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}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Error.php:32
map(callable $f)
Create a new result where the contained value is modified with $f.Does nothing if !...
Definition: Error.php:93
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.\Exception|string LogicException if isOK
Definition: Error.php:77
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
value()
Get the encapsulated value.mixed Exception if !isOK, will either throw the contained exception or a N...
Definition: Error.php:57
valueOr($default)
Get the encapsulated value or the supplied default if result is an error.mixed
Definition: Error.php:85
isError()
Get to know if the result is an error.
Definition: Error.php:69
isOK()
Get to know if the result is ok.
Definition: Error.php:49
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29