ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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. More...
 
 value ()
 Get the encapsulated value.
Returns
mixed
Exceptions
More...
 
 isError ()
 Get to know if the result is an error. More...
 
 error ()
 Get the encapsulated error.
Returns
|string
Exceptions
More...
 
 valueOr ($default)
 Get the encapsulated value or the supplied default if result is an error.
Parameters
mixed$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
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
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
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 31 of file Error.php.

Constructor & Destructor Documentation

◆ __construct()

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

Definition at line 38 of file Error.php.

References ILIAS\Data\Result\Error\$error, and ILIAS\Data\Result\Error\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  }
error()
Get the encapsulated error.|string
Definition: Error.php:77
+ Here is the call graph for this function:

Member Function Documentation

◆ error()

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

Get the encapsulated error.

Returns
|string
Exceptions

Implements ILIAS\Data\Result.

Definition at line 77 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().

78  {
79  return $this->error;
80  }
+ 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

Implements ILIAS\Data\Result.

Definition at line 109 of file Error.php.

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

109  : 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  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
error()
Get the encapsulated error.|string
Definition: Error.php:77
+ Here is the call graph for this function:

◆ isError()

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

Get to know if the result is an error.

Implements ILIAS\Data\Result.

Definition at line 69 of file Error.php.

69  : bool
70  {
71  return true;
72  }

◆ isOK()

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

Get to know if the result is ok.

Implements ILIAS\Data\Result.

Definition at line 49 of file Error.php.

49  : bool
50  {
51  return false;
52  }

◆ 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

Implements ILIAS\Data\Result.

Definition at line 93 of file Error.php.

93  : Result
94  {
95  return $this;
96  }

◆ 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

Implements ILIAS\Data\Result.

Definition at line 101 of file Error.php.

101  : Result
102  {
103  return $this;
104  }

◆ value()

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

Get the encapsulated value.

Returns
mixed
Exceptions

Implements ILIAS\Data\Result.

Definition at line 57 of file Error.php.

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

58  {
59  if ($this->error instanceof \Exception) {
60  throw $this->error;
61  }
62 
63  throw new Data\NotOKException($this->error);
64  }
error()
Get the encapsulated error.|string
Definition: Error.php:77
+ 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
mixed$default
Returns
mixed

Implements ILIAS\Data\Result.

Definition at line 85 of file Error.php.

86  {
87  return $default;
88  }

Field Documentation

◆ $error

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

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