-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathResultInterface.php
146 lines (134 loc) · 4.34 KB
/
ResultInterface.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Psl\Result;
use Closure;
use Psl;
use Throwable;
/**
* Represents a result of operation that either has a successful result or the throwable object if
* that operation failed.
*
* This is an interface. You get generally `ResultInterface<T>` by calling `wrap<T>()`, passing in
* the `(Closure(): T)`, and a `Success<T>` or `Failure<Te>` is returned.
*
* @template T
*
* @extends Psl\Promise\PromiseInterface<T>
*/
interface ResultInterface extends Psl\Promise\PromiseInterface
{
/**
* Transforms a promise's value by applying a function to the promise's fulfillment
* or rejection value.
*
* It is a shortcut for:
*
* ```php
* $promise->then($success, $failure);
* // same as:
* $promise->map($success)->catch($failure);
* ```
*
* @template Ts
*
* @param (Closure(T): Ts) $success
* @param (Closure(Throwable): Ts) $failure
*
* @return ResultInterface<Ts>
*/
public function then(Closure $success, Closure $failure): ResultInterface;
/**
* Attaches a callback that is invoked if this promise is fulfilled.
*
* The returned promise is resolved with the return value of the callback,
* or is rejected with a throwable thrown from the callback.
*
* @template Ts
*
* @param (Closure(T): Ts) $success
*
* @return ResultInterface<Ts>
*/
public function map(Closure $success): ResultInterface;
/**
* Attaches a callback that is invoked if this promise is rejected.
*
* The returned promise is resolved with the return value of the callback,
* or is rejected with a throwable thrown from the callback.
*
* @template Ts
*
* @param (Closure(Throwable): Ts) $failure
*
* @return ResultInterface<T|Ts>
*/
public function catch(Closure $failure): ResultInterface;
/**
* Attaches a callback that is always invoked when the promise is resolved.
*
* The returned promise resolves with the same value as this promise once the callback has finished execution.
*
* If the callback throws, the returned promise will be rejected with the thrown throwable.
*
* @param (Closure(): void) $always
*
* @return ResultInterface<T>
*/
public function always(Closure $always): ResultInterface;
/**
* Return the result of the operation, or throw underlying throwable.
*
* - if the operation succeeded: return its result.
* - if the operation failed: throw the throwable inciting failure.
*
* @return T - The result of the operation upon success
*
* @psalm-mutation-free
*/
public function getResult();
/**
* Return the underlying throwable, or fail with a invariant violation exception.
*
* - if the operation succeeded: fails with a invariant violation exception.
* - if the operation failed: returns the throwable indicating failure.
*
* @throws Psl\Exception\InvariantViolationException - When the operation succeeded
*
* @psalm-mutation-free
*/
public function getThrowable(): Throwable;
/**
* Indicates whether the operation associated with this wrapper existed normally.
*
* if `isSucceeded()` returns `true`, `isFailed()` returns false.
*
* @return bool - `true` if the operation succeeded; `false` otherwise
*
* @psalm-mutation-free
*/
public function isSucceeded(): bool;
/**
* Indicates whether the operation associated with this wrapper exited abnormally via a throwable of some sort.
*
* if `isFailed()` returns `true`, `isSucceeded()` returns false.
*
* @return bool - `true` if the operation failed; `false` otherwise
*
* @psalm-mutation-free
*/
public function isFailed(): bool;
/**
* Unwrapping and transforming a result can be done by using the proceed method.
* The implementation will either run the `$on_success` or `$on_failure` callback.
* The callback will receive the result or Throwable as an argument,
* so that you can transform it to anything you want.
*
* @template Ts
*
* @param (Closure(T): Ts) $success
* @param (Closure(Throwable): Ts) $failure
*
* @return Ts
*/
public function proceed(Closure $success, Closure $failure): mixed;
}