forked from ILIAS-eLearning/ILIAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResult.php
88 lines (79 loc) · 2.22 KB
/
Result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/* Copyright (c) 2017 Richard Klees <[email protected]> Extended GPL, see docs/LICENSE */
namespace ILIAS\Data;
/**
* A result encapsulates a value or an error and simplifies the handling of those.
*
* To be implemented as immutable object.
*/
interface Result
{
/**
* Get to know if the result is ok.
*
* @return bool
*/
public function isOK();
/**
* Get the encapsulated value.
*
* @throws Exception if !isOK, will either throw the contained exception or
* a NotOKException if a string is contained as error.
* @return mixed
*/
public function value();
/**
* Get to know if the result is an error.
*
* @return bool
*/
public function isError();
/**
* Get the encapsulated error.
*
* @throws LogicException if isOK
* @return Exception|string
*/
public function error();
/**
* Get the encapsulated value or the supplied default if result is an error.
*
* @param default
* @return mixed
*/
public function valueOr($default);
/**
* Create a new result where the contained value is modified with $f.
*
* Does nothing if !isOK.
*
* @param callable $f mixed -> mixed
* @return Result
*/
public function map(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.
*
* @param callable $f mixed -> Result|null
* @throws UnexpectedValueException If callable returns no instance of Result
* @return Result
*/
public function then(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.
*
* @param callable $f string|\Exception -> Result|null
* @throws UnexpectedValueException If callable returns no instance of Result
* @return Result
*/
public function except(callable $f);
}