ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
ILIAS\Data\Result\Ok Class Reference

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

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

Public Member Functions

 __construct ($value)
 
 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

 $value
 

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 16 of file Ok.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Data\Result\Ok::__construct (   $value)

Definition at line 23 of file Ok.php.

References ILIAS\Data\Result\Ok\$value, and ILIAS\Data\Result\Ok\value().

24  {
25  $this->value = $value;
26  }
value()
Get the encapsulated value.mixed
Definition: Ok.php:39
+ Here is the call graph for this function:

Member Function Documentation

◆ error()

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

Get the encapsulated error.

Returns
|string
Exceptions

Implements ILIAS\Data\Result.

Definition at line 55 of file Ok.php.

56  {
57  throw new \LogicException("This is a OK result. No error message available");
58  }

◆ except()

ILIAS\Data\Result\Ok::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 100 of file Ok.php.

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

◆ isError()

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

Get to know if the result is an error.

Implements ILIAS\Data\Result.

Definition at line 47 of file Ok.php.

47  : bool
48  {
49  return false;
50  }

◆ isOK()

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

Get to know if the result is ok.

Implements ILIAS\Data\Result.

Definition at line 31 of file Ok.php.

31  : bool
32  {
33  return true;
34  }

◆ map()

ILIAS\Data\Result\Ok::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 71 of file Ok.php.

References Vendor\Package\$f, ILIAS\Data\Result\Ok\$value, and ILIAS\Data\Result\Ok\value().

71  : Result
72  {
73  $clone = clone $this;
74  $value = $f($this->value);
75  $clone->value = $value;
76  return $clone;
77  }
value()
Get the encapsulated value.mixed
Definition: Ok.php:39
+ Here is the call graph for this function:

◆ then()

ILIAS\Data\Result\Ok::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 82 of file Ok.php.

References Vendor\Package\$f, and ILIAS\Data\Result\Ok\value().

82  : Result
83  {
84  $result = $f($this->value);
85 
86  if ($result === null) {
87  return $this;
88  }
89 
90  if (!$result instanceof Result) {
91  throw new \UnexpectedValueException("The returned type of callable is not an instance of interface Result");
92  }
93 
94  return $result;
95  }
value()
Get the encapsulated value.mixed
Definition: Ok.php:39
+ Here is the call graph for this function:

◆ value()

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

Get the encapsulated value.

Returns
mixed
Exceptions

Implements ILIAS\Data\Result.

Definition at line 39 of file Ok.php.

References ILIAS\Data\Result\Ok\$value.

Referenced by ILIAS\Data\Result\Ok\__construct(), ILIAS\Data\Result\Ok\map(), and ILIAS\Data\Result\Ok\then().

40  {
41  return $this->value;
42  }
+ Here is the caller graph for this function:

◆ valueOr()

ILIAS\Data\Result\Ok::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 63 of file Ok.php.

References ILIAS\Data\Result\Ok\$value.

64  {
65  return $this->value;
66  }

Field Documentation

◆ $value

ILIAS\Data\Result\Ok::$value
protected

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