-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResultInterface.php
95 lines (85 loc) · 2.75 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
<?php
declare(strict_types=1);
namespace Someniatko\ResultType;
/**
* @template-covariant TSuccess
* @template-covariant TError
* @psalm-immutable
*/
interface ResultInterface
{
/**
* Takes a callable which maps **success** value to a new value, returns new ResultInterface.
* The callable will be called only if this result is Success.
*
* If this result is Success, returns new Success with changed value.
* If this result is Error, returns it as is.
*
* @template TNewSuccess
*
* @param callable(TSuccess):TNewSuccess $map
* @return self<TNewSuccess, TError>
*/
public function map(callable $map): self;
/**
* Takes a callable which maps **error** value to a new value, returns new ResultInterface.
* The callable will be called only if this result is Error.
*
* If this result is Success, returns it as is.
* If this result is Error, returns new Error with changed value.
* @template TNewError
*
* @param callable(TError):TNewError $map
* @return self<TSuccess, TNewError>
*/
public function mapError(callable $map): self;
/**
* Chains Success path processing. May either just change Success value, or change the result type to Error.
* Takes a callable which takes **success** value and returns new ResultInterface.
* The callable will be called only if this result is Success.
*
* @template TNewSuccess
* @template TNewError
*
* @param callable(TSuccess):self<TNewSuccess, TNewError> $map
* @return self<TNewSuccess, TError|TNewError>
*/
public function chain(callable $map): self;
/**
* Returns the final value of this result.
* The value will be returned for both Success and Error cases.
*
* @return TSuccess|TError
*/
public function get();
/**
* Returns the value in case of Success,
* or computes it from given callable in case of Error.
*
* Equivalent to `$result->mapError($map)->get()`.
*
* @template TNewError
*
* @param callable(TError):TNewError $map
* @return TSuccess|TNewError
*/
public function getOr(callable $map);
/**
* @param \Throwable $e
* @return TSuccess|never-return
*/
public function getOrThrow(\Throwable $e);
/**
* Ensures that the Success value also validates against the given condition,
* Otherwise returns an Error with a given value.
*
* If this Result is already an Error, nothing will change.
*
* @template TNewError
*
* @param callable(TSuccess):bool $condition
* @param TNewError $else
* @return ResultInterface<TSuccess, TError|TNewError>
*/
public function ensure(callable $condition, $else): self;
}