|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Someniatko\ResultType; |
| 6 | + |
| 7 | +/** |
| 8 | + * @template-covariant TSuccess |
| 9 | + * @template-covariant TError |
| 10 | + * @psalm-immutable |
| 11 | + */ |
| 12 | +interface ResultInterface |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Takes a callable which maps **success** value to a new value, returns new ResultInterface. |
| 16 | + * The callable will be called only if this result is Success. |
| 17 | + * |
| 18 | + * If this result is Success, returns new Success with changed value. |
| 19 | + * If this result is Error, returns it as is. |
| 20 | + * |
| 21 | + * @template TNewSuccess |
| 22 | + * |
| 23 | + * @param callable(TSuccess):TNewSuccess $map |
| 24 | + * @return self<TNewSuccess, TError> |
| 25 | + */ |
| 26 | + public function map(callable $map): self; |
| 27 | + |
| 28 | + /** |
| 29 | + * Takes a callable which maps **error** value to a new value, returns new ResultInterface. |
| 30 | + * The callable will be called only if this result is Error. |
| 31 | + * |
| 32 | + * If this result is Success, returns it as is. |
| 33 | + * If this result is Error, returns new Error with changed value. |
| 34 | +
|
| 35 | + * @template TNewError |
| 36 | + * |
| 37 | + * @param callable(TError):TNewError $map |
| 38 | + * @return self<TSuccess, TNewError> |
| 39 | + */ |
| 40 | + public function mapError(callable $map): self; |
| 41 | + |
| 42 | + /** |
| 43 | + * Chains Success path processing. May either just change Success value, or change the result type to Error. |
| 44 | + * Takes a callable which takes **success** value and returns new ResultInterface. |
| 45 | + * The callable will be called only if this result is Success. |
| 46 | + * |
| 47 | + * @template TNewSuccess |
| 48 | + * @template TNewError |
| 49 | + * |
| 50 | + * @param callable(TSuccess):self<TNewSuccess, TNewError> $map |
| 51 | + * @return self<TNewSuccess, TError|TNewError> |
| 52 | + */ |
| 53 | + public function chain(callable $map): self; |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the final value of this result. |
| 57 | + * The value will be returned for both Success and Error cases. |
| 58 | + * |
| 59 | + * @return TSuccess|TError |
| 60 | + */ |
| 61 | + public function get(); |
| 62 | +} |
0 commit comments