ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
5
6use ILIAS\Data;
7
13class 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
An exception for terminatinating execution or to throw for unit testing.
Signals that a result contains no value.
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Error.php:14
map(callable $f)
Create a new result where the contained value is modified with $f.Does nothing if !...
Definition: Error.php:75
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
error()
Get the encapsulated error.Exception|string
Definition: Error.php:59
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
value()
Get the encapsulated value.mixed
Definition: Error.php:39
valueOr($default)
Get the encapsulated value or the supplied default if result is an error.mixed
Definition: Error.php:67
isError()
Get to know if the result is an error.bool
Definition: Error.php:51
isOK()
Get to know if the result is ok.bool
Definition: Error.php:31
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:12