为PHP提供结果类型实现

1.3.0 2023-07-06 01:18 UTC

This package is auto-updated.

Last update: 2024-08-31 19:14:56 UTC


README

Automation Supported PHP Version Mutation Coverage Code Coverage Type Coverage Latest Version on Packagist Downloads

使用 ghostwriter/option 为PHP提供 Result 类型实现

安装

您可以通过composer安装此包

composer require ghostwriter/result

用法

use Ghostwriter\Result\Error;
use Ghostwriter\Result\Success;

// --- Success ---
$success = Success::create('Hello world!');
$success->unwrap(); // 'Hello world!'

// --- Error ---
$error = Error::create(new ExampleException());
$error->unwrap(); // throws: ResultException
$error->unwrapOr('Fallback'); // 'Fallback'
$error->unwrapError(); // returns: instance of ExampleException

// --- Example ---
function divide(int $x, int $y): ResultInterface
{
    if ($y === 0) {
        return Error::create(new DivisionByZeroError);
    }

    return Success::create($x / $y);
}

divide(1, 0); // Error(DivisionByZeroError)
divide(1, 1); // Success(1)

API

成功接口

/**
 * @template TValue
 *
 * @implements ResultInterface<TValue>
 */
interface SuccessInterface extends ResultInterface
{
    /**
     * Create a new success value.
     *
     * @template TSuccess
     *
     * @param TSuccess $value
     *
     * @return self<TSuccess>
     */
    public static function create(mixed $value): self;
}

错误接口

use Throwable;

/**
 * @template TValue of Throwable
 *
 * @implements ResultInterface<Throwable>
 */
interface ErrorInterface extends ResultInterface
{
    /**
     * Create a new error value.
     *
     * @return self<Throwable>
     */
    public static function create(Throwable $throwable): self;
}

结果接口

use Ghostwriter\Option\OptionInterface;
use Throwable;

/**
 * @template TValue
 */
interface ResultInterface
{
    /**
     * Returns $result if the result is Success, otherwise returns the Error value of self.
     *
     * @template TAndValue
     * @param self<TAndValue> $result
     *
     * @return self<TAndValue>
     */
    public function and(self $result): self;

    /**
     * Calls $function if the result is Success, otherwise returns the Error value of self.
     *
     * @template TNewValue
     *
     * @param callable(TValue):TNewValue $function
     *
     * @return self<TValue>
     */
    public function andThen(callable $function): self;

    /**
     * Converts from Result<TValue> to Option<TValue>.
     */
    public function error(): OptionInterface;

    /**
     * Unwraps a result, yielding the content of a Success.
     *
     * @throws Throwable
     */
    public function expect(Throwable $throwable): mixed;

    /**
     * Unwraps a result, yielding the content of an Error.
     *
     * @throws Throwable
     */
    public function expectError(Throwable $throwable): Throwable;

    /**
     * Returns true if the result is Error.
     */
    public function isError(): bool;

    /**
     * Returns true if the result is Success.
     */
    public function isSuccess(): bool;

    /**
     * Maps a Result<T,E> to Result<U,E> by applying a function to a contained Success value, leaving an Error value
     * untouched.
     *
     * @template TMap
     *
     * @param callable(TValue):TMap $function
     *
     * @return self<TMap>
     */
    public function map(callable $function): self;

    /**
     * Maps a Result<T,E> to Result<T,F> by applying a function to a contained Error value, leaving a Success value
     * untouched.
     *
     * @template TMapError
     *
     * @param callable(TValue):TMapError $function
     *
     * @return self<TMapError|TValue>
     */
    public function mapError(callable $function): self;

    /**
     * Returns $result if the result is Error, otherwise returns the Success value of self.
     */
    public function or(self $result): self;

    /**
     * Calls $function if the result is Error, otherwise returns the Success value of self.
     *
     * @template TOrElse
     *
     * @param callable(TValue):TOrElse $function
     *
     * @return self<TOrElse|TValue>
     */
    public function orElse(callable $function): self;

    /**
     * Converts from Result<TValue> to Option<TValue>.
     *
     * @return OptionInterface<TValue>
     */
    public function success(): OptionInterface;

    /**
     * Unwraps a result, yielding the content of a Success.
     *
     * @return TValue
     */
    public function unwrap(): mixed;

    /**
     * Unwraps a result, yielding the content of an Error.
     *
     * @return TValue
     */
    public function unwrapError(): mixed;

    /**
     * Unwraps a result, yielding the content of a Success. Else, it returns $fallback.
     *
     * @template TUnwrapOr
     *
     * @param TUnwrapOr $fallback
     *
     * @return TUnwrapOr|TValue
     */
    public function unwrapOr(mixed $fallback): mixed;

    /**
     * Unwraps a result, yielding the content of a Success. If the value is an Error then it calls $function with its
     * value.
     *
     * @template TUnwrapOrElse
     *
     * @param callable(TValue):TUnwrapOrElse $function
     *
     * @return TUnwrapOrElse|TValue
     */
    public function unwrapOrElse(callable $function): mixed;
}

测试

composer test

变更日志

请参阅 CHANGELOG.md 了解最近更改的详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件 nathanael.esayeas@protonmail.com 联系,而不是使用问题跟踪器。

赞助商

[成为GitHub赞助商]

鸣谢

许可证

BSD-3-Clause许可证。有关更多信息,请参阅 许可证文件