ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
Ok.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /* Copyright (c) 2017 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
6 
7 namespace ILIAS\Data\Result;
8 
10 
16 class Ok implements Result
17 {
21  protected $value;
22 
23  public function __construct($value)
24  {
25  $this->value = $value;
26  }
27 
31  public function isOK(): bool
32  {
33  return true;
34  }
35 
39  public function value()
40  {
41  return $this->value;
42  }
43 
47  public function isError(): bool
48  {
49  return false;
50  }
51 
55  public function error()
56  {
57  throw new \LogicException("This is a OK result. No error message available");
58  }
59 
63  public function valueOr($default)
64  {
65  return $this->value;
66  }
67 
71  public function map(callable $f): Result
72  {
73  $clone = clone $this;
74  $value = $f($this->value);
75  $clone->value = $value;
76  return $clone;
77  }
78 
82  public function then(callable $f): 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  }
96 
100  public function except(callable $f): Result
101  {
102  return $this;
103  }
104 }
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:82
valueOr($default)
Get the encapsulated value or the supplied default if result is an error.mixed
Definition: Ok.php:63
isOK()
Get to know if the result is ok.
Definition: Ok.php:31
__construct($value)
Definition: Ok.php:23
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:14
isError()
Get to know if the result is an error.
Definition: Ok.php:47
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:100
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:16
error()
Get the encapsulated error.|string
Definition: Ok.php:55
value()
Get the encapsulated value.mixed
Definition: Ok.php:39
map(callable $f)
Create a new result where the contained value is modified with $f.Does nothing if !isOK...
Definition: Ok.php:71