ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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| -> Result|null
Exceptions
UnexpectedValueExceptionIf callable returns no instance of Result
Returns
Result
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.

References ILIAS\Data\Result\Error\$error, and ILIAS\Data\Result\Error\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  }
error()
Get the encapsulated error.if isOK Exception|string
Definition: Error.php:59
+ 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.

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

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

60  {
61  return $this->error;
62  }
+ 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| -> Result|null
Exceptions
UnexpectedValueExceptionIf callable returns no instance of Result
Returns
Result

Implements ILIAS\Data\Result.

Definition at line 91 of file Error.php.

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

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

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

40  {
41  if ($this->error instanceof \Exception) {
42  throw $this->error;
43  }
44 
45  throw new Data\NotOKException($this->error);
46  }
error()
Get the encapsulated error.if isOK Exception|string
Definition: Error.php:59
+ 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.

References $default.

68  {
69  return $default;
70  }
$default
Definition: build.php:20

Field Documentation

◆ $error

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

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