-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce Result abstract class with a few helper static methods
- Loading branch information
1 parent
41e8a4f
commit bcc17aa
Showing
4 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Someniatko\ResultType; | ||
|
||
/** | ||
* @template-covariant TSuccess | ||
* @template-covariant TError | ||
* @template-implements ResultInterface<TSuccess, TError> | ||
* @psalm-immutable | ||
*/ | ||
abstract class Result implements ResultInterface | ||
{ | ||
/** | ||
* @template T | ||
* @param T $value | ||
* @return Success<T> | ||
*/ | ||
public static function success($value): Success | ||
{ | ||
return new Success($value); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param T $value | ||
* @return Error<T> | ||
*/ | ||
public static function error($value): Error | ||
{ | ||
return new Error($value); | ||
} | ||
|
||
/** | ||
* Filters and extracts values only of Success results from the given array. | ||
* | ||
* @template S | ||
* @param list<ResultInterface<S, mixed>> $results | ||
* @return list<S> | ||
*/ | ||
public static function extractSuccesses(array $results): array | ||
{ | ||
return array_reduce( | ||
$results, | ||
/** | ||
* @param list<S> $carry | ||
* @param ResultInterface<S, mixed> $result | ||
* @return list<S> | ||
*/ | ||
static fn(array $carry, ResultInterface $result) => $result | ||
->mapError(fn() => $carry) | ||
->map( | ||
/** | ||
* @param S $t | ||
* @return list<S> | ||
*/ | ||
fn($t) => array_merge($carry, [ $t ]) | ||
) | ||
->get(), | ||
[], | ||
); | ||
} | ||
|
||
/** | ||
* Filters and extracts values only of Error results from the given array. | ||
* | ||
* @template E | ||
* @param list<ResultInterface<mixed, E>> $results | ||
* @return list<E> | ||
*/ | ||
public static function extractErrors(array $results): array | ||
{ | ||
return array_reduce( | ||
$results, | ||
/** | ||
* @param list<E> $carry | ||
* @param ResultInterface<mixed, E> $result | ||
* @return list<E> | ||
*/ | ||
static fn(array $carry, ResultInterface $result) => $result | ||
->map(fn() => $carry) | ||
->mapError( | ||
/** | ||
* @param E $t | ||
* @return list<E> | ||
*/ | ||
fn($t) => array_merge($carry, [ $t ]) | ||
) | ||
->get(), | ||
[], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters