ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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.
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

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

Constructor & Destructor Documentation

◆ __construct()

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

Definition at line 21 of file Ok.php.

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

22  {
23  $this->value = $value;
24  }
value()
Get the encapsulated value.if !isOK, will either throw the contained exception or a NotOKException if...
Definition: Ok.php:37
+ Here is the call graph for this function:

Member Function Documentation

◆ error()

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

Get the encapsulated error.

Exceptions
LogicExceptionif isOK
Returns
Exception|string

Implements ILIAS\Data\Result.

Definition at line 53 of file Ok.php.

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

◆ 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
UnexpectedValueExceptionIf callable returns no instance of Result
Returns
Result

Implements ILIAS\Data\Result.

Definition at line 98 of file Ok.php.

99  {
100  return $this;
101  }

◆ isError()

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

Get to know if the result is an error.

Returns
bool

Implements ILIAS\Data\Result.

Definition at line 45 of file Ok.php.

46  {
47  return false;
48  }

◆ isOK()

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

Get to know if the result is ok.

Returns
bool

Implements ILIAS\Data\Result.

Definition at line 29 of file Ok.php.

30  {
31  return true;
32  }

◆ 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
Returns
Result

Implements ILIAS\Data\Result.

Definition at line 69 of file Ok.php.

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

70  {
71  $clone = clone $this;
72  $value = $f($this->value);
73  $clone->value = $value;
74  return $clone;
75  }
value()
Get the encapsulated value.if !isOK, will either throw the contained exception or a NotOKException if...
Definition: Ok.php:37
+ 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
UnexpectedValueExceptionIf callable returns no instance of Result
Returns
Result

Implements ILIAS\Data\Result.

Definition at line 80 of file Ok.php.

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

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

◆ value()

ILIAS\Data\Result\Ok::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 37 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().

38  {
39  return $this->value;
40  }
+ 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
default
Returns
mixed

Implements ILIAS\Data\Result.

Definition at line 61 of file Ok.php.

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

62  {
63  return $this->value;
64  }

Field Documentation

◆ $value

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

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