ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ILIAS\Data\Result\Error Class Reference

A result encapsulates a value or an error and simplifies the handling of those. More...

+ Inheritance diagram for ILIAS\Data\Result\Error:
+ Collaboration diagram for ILIAS\Data\Result\Error:

Public Member Functions

 __construct ($error)
 
 isOK ()
 Get to know if the result is ok.
Returns
bool
More...
 
 value ()
 Get the encapsulated value.
Exceptions
Exceptionif !isOK, will either throw the contained exception or a NotOKException if a string is contained as error.
Returns
mixed
More...
 
 isError ()
 Get to know if the result is an error.
Returns
bool
More...
 
 error ()
 Get the encapsulated error.
Exceptions
LogicExceptionif isOK
Returns
Exception|string
More...
 
 valueOr ($default)
 Get the encapsulated value or the supplied default if result is an error.
Parameters
default
Returns
mixed
More...
 
 map (callable $f)
 Create a new result where the contained value is modified with $f.Does nothing if !isOK.
Parameters
callable$fmixed -> mixed
Returns
Result
More...
 
 then (callable $f)
 Get a new result from the callable or do nothing if this is an error.If null is returned from $f, the result is not touched.Does nothing if !isOK. This is monadic bind.
Parameters
callable$fmixed -> Result|null
Exceptions
UnexpectedValueExceptionIf callable returns no instance of Result
Returns
Result
More...
 
 except (callable $f)
 Feed the error into a callable and replace this with the result or do nothing if this is a value.If null is returned from $f, the error in the result is not touched.Does nothing if !isError.
Parameters
callable$fstring|\Exception -> Result|null
Exceptions
UnexpectedValueExceptionIf callable returns no instance of Result
Returns
Result
More...
 
 isOK ()
 Get to know if the result is ok. More...
 
 value ()
 Get the encapsulated value. More...
 
 isError ()
 Get to know if the result is an error. More...
 
 error ()
 Get the encapsulated error. More...
 
 valueOr ($default)
 Get the encapsulated value or the supplied default if result is an error. More...
 
 map (callable $f)
 Create a new result where the contained value is modified with $f. More...
 
 then (callable $f)
 Get a new result from the callable or do nothing if this is an error. More...
 
 except (callable $f)
 Feed the error into a callable and replace this with the result or do nothing if this is a value. More...
 

Protected Attributes

 $error
 

Detailed Description

A result encapsulates a value or an error and simplifies the handling of those.

Author
Stefan Hecken stefa.nosp@m.n.he.nosp@m.cken@.nosp@m.conc.nosp@m.epts-.nosp@m.and-.nosp@m.train.nosp@m.ing..nosp@m.de

Definition at line 13 of file Error.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Data\Result\Error::__construct (   $error)

Definition at line 21 of file Error.php.

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 }
error()
Get the encapsulated error.Exception|string
Definition: Error.php:59

References ILIAS\Data\Result\Error\$error, and ILIAS\Data\Result\Error\error().

+ Here is the call graph for this function:

Member Function Documentation

◆ error()

ILIAS\Data\Result\Error::error ( )

Get the encapsulated error.

Exceptions
LogicExceptionif isOK
Returns
Exception|string

Implements ILIAS\Data\Result.

Definition at line 59 of file Error.php.

60 {
61 return $this->error;
62 }

References ILIAS\Data\Result\Error\$error.

Referenced by ILIAS\Data\Result\Error\__construct(), ILIAS\Data\Result\Error\except(), and ILIAS\Data\Result\Error\value().

+ Here is the caller graph for this function:

◆ except()

ILIAS\Data\Result\Error::except ( callable  $f)

Feed the error into a callable and replace this with the result or do nothing if this is a value.If null is returned from $f, the error in the result is not touched.Does nothing if !isError.

Parameters
callable$fstring|\Exception -> Result|null
Exceptions
UnexpectedValueExceptionIf callable returns no instance of Result
Returns
Result

Implements ILIAS\Data\Result.

Definition at line 91 of file Error.php.

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 }
$result

References Vendor\Package\$f, $result, and ILIAS\Data\Result\Error\error().

+ Here is the call graph for this function:

◆ isError()

ILIAS\Data\Result\Error::isError ( )

Get to know if the result is an error.

Returns
bool

Implements ILIAS\Data\Result.

Definition at line 51 of file Error.php.

52 {
53 return true;
54 }

◆ isOK()

ILIAS\Data\Result\Error::isOK ( )

Get to know if the result is ok.

Returns
bool

Implements ILIAS\Data\Result.

Definition at line 31 of file Error.php.

32 {
33 return false;
34 }

◆ map()

ILIAS\Data\Result\Error::map ( callable  $f)

Create a new result where the contained value is modified with $f.Does nothing if !isOK.

Parameters
callable$fmixed -> mixed
Returns
Result

Implements ILIAS\Data\Result.

Definition at line 75 of file Error.php.

76 {
77 return $this;
78 }

◆ then()

ILIAS\Data\Result\Error::then ( callable  $f)

Get a new result from the callable or do nothing if this is an error.If null is returned from $f, the result is not touched.Does nothing if !isOK. This is monadic bind.

Parameters
callable$fmixed -> Result|null
Exceptions
UnexpectedValueExceptionIf callable returns no instance of Result
Returns
Result

Implements ILIAS\Data\Result.

Definition at line 83 of file Error.php.

84 {
85 return $this;
86 }

◆ value()

ILIAS\Data\Result\Error::value ( )

Get the encapsulated value.

Exceptions
Exceptionif !isOK, will either throw the contained exception or a NotOKException if a string is contained as error.
Returns
mixed

Implements ILIAS\Data\Result.

Definition at line 39 of file Error.php.

40 {
41 if ($this->error instanceof \Exception) {
42 throw $this->error;
43 }
44
45 throw new Data\NotOKException($this->error);
46 }

References ILIAS\Data\Result\Error\$error, and ILIAS\Data\Result\Error\error().

+ Here is the call graph for this function:

◆ valueOr()

ILIAS\Data\Result\Error::valueOr (   $default)

Get the encapsulated value or the supplied default if result is an error.

Parameters
default
Returns
mixed

Implements ILIAS\Data\Result.

Definition at line 67 of file Error.php.

68 {
69 return $default;
70 }

Field Documentation

◆ $error

ILIAS\Data\Result\Error::$error
protected

The documentation for this class was generated from the following file: