ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Ok.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 2017 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3 
4 namespace ILIAS\Data\Result;
5 
7 
13 class Ok implements Result
14 {
15 
19  protected $value;
20 
21  public function __construct($value)
22  {
23  $this->value = $value;
24  }
25 
29  public function isOK()
30  {
31  return true;
32  }
33 
37  public function value()
38  {
39  return $this->value;
40  }
41 
45  public function isError()
46  {
47  return false;
48  }
49 
53  public function error()
54  {
55  throw new \LogicException("This is a OK result. No error message available");
56  }
57 
61  public function valueOr($default)
62  {
63  return $this->value;
64  }
65 
69  public function map(callable $f)
70  {
71  $clone = clone $this;
72  $value = $f($this->value);
73  $clone->value = $value;
74  return $clone;
75  }
76 
80  public function then(callable $f)
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  }
94 
98  public function except(callable $f)
99  {
100  return $this;
101  }
102 }
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:80
$result
valueOr($default)
Get the encapsulated value or the supplied default if result is an error.mixed
Definition: Ok.php:61
isOK()
Get to know if the result is ok.bool
Definition: Ok.php:29
__construct($value)
Definition: Ok.php:21
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:11
isError()
Get to know if the result is an error.bool
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:98
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:13
error()
Get the encapsulated error.if isOK Exception|string
Definition: Ok.php:53
$default
Definition: build.php:20
value()
Get the encapsulated value.if !isOK, will either throw the contained exception or a NotOKException if...
Definition: Ok.php:37
map(callable $f)
Create a new result where the contained value is modified with $f.Does nothing if !isOK...
Definition: Ok.php:69