ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
4namespace ILIAS\Data\Result;
5
7
13class 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}
$result
An exception for terminatinating execution or to throw for unit testing.
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:14
error()
Get the encapsulated error.Exception|string
Definition: Ok.php:53
isOK()
Get to know if the result is ok.bool
Definition: Ok.php:29
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
isError()
Get to know if the result is an error.bool
Definition: Ok.php:45
map(callable $f)
Create a new result where the contained value is modified with $f.Does nothing if !...
Definition: Ok.php:69
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
value()
Get the encapsulated value.mixed
Definition: Ok.php:37
valueOr($default)
Get the encapsulated value or the supplied default if result is an error.mixed
Definition: Ok.php:61
__construct($value)
Definition: Ok.php:21
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:12