ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Ok.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Data\Result;
22
24
30class Ok implements Result
31{
35 protected $value;
36
37 public function __construct($value)
38 {
39 $this->value = $value;
40 }
41
45 public function isOK(): bool
46 {
47 return true;
48 }
49
53 public function value()
54 {
55 return $this->value;
56 }
57
61 public function isError(): bool
62 {
63 return false;
64 }
65
69 public function error()
70 {
71 throw new \LogicException("This is a OK result. No error message available");
72 }
73
77 public function valueOr($default)
78 {
79 return $this->value;
80 }
81
85 public function map(callable $f): Result
86 {
87 $clone = clone $this;
88 $value = $f($this->value);
89 $clone->value = $value;
90 return $clone;
91 }
92
96 public function then(callable $f): Result
97 {
98 $result = $f($this->value);
99
100 if ($result === null) {
101 return $this;
102 }
103
104 if (!$result instanceof Result) {
105 throw new \UnexpectedValueException("The returned type of callable is not an instance of interface Result");
106 }
107
108 return $result;
109 }
110
114 public function except(callable $f): Result
115 {
116 return $this;
117 }
118}
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:31
error()
Get the encapsulated error.\Exception|string LogicException if isOK
Definition: Ok.php:69
isOK()
Get to know if the result is ok.
Definition: Ok.php:45
except(callable $f)
Feed the error into a callable and replace this with the result or do nothing if this is a value....
Definition: Ok.php:114
isError()
Get to know if the result is an error.
Definition: Ok.php:61
map(callable $f)
Create a new result where the contained value is modified with $f.Does nothing if !...
Definition: Ok.php:85
then(callable $f)
Get a new result from the callable or do nothing if this is an error.If null is returned from $f,...
Definition: Ok.php:96
value()
Get the encapsulated value.mixed Exception if !isOK, will either throw the contained exception or a N...
Definition: Ok.php:53
valueOr($default)
Get the encapsulated value or the supplied default if result is an error.mixed
Definition: Ok.php:77
__construct($value)
Definition: Ok.php:37
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29