aaronmacaron/reaction

此包最新版本(1.0.0)没有提供许可证信息。

处理动作反应的库

1.0.0 2018-09-02 09:40 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:45:27 UTC


README

Travis Packagist

此库简化了对动作、方法和函数反应的处理。如果您处理的方法可能成功或失败,并且需要更多关于方法结果的信息,您可以使用反应。

安装

您可以使用Composer安装反应。

$ composer require aaronmacaron/reaction

要安装和使用反应库,您需要PHP版本7.1或更高。

示例

您可以使用此库如下:

<?php

use Aaronmacaron\Reaction\Reaction;

include __DIR__ . "/vendor/autoload.php";

function validatePassword(string $password): Reaction
{
    if (empty($password)) {
        return Reaction::failure("The password must not be empty!");
    }

    if (strlen($password) >= 8) {
        return Reaction::success();
    }

    return Reaction::failure("The password must be at least eight chars long.");
}

validatePassword("secret")
    ->succeed(function () {
        echo "Password is valid" . PHP_EOL;
    })->fail(function (Reaction $reaction) {
        echo "The password is not valid: " . $reaction->getMessage() . PHP_EOL;
    });

// Output: The password is not valid: The password must be at least eight chars long.