aaronmacaron/reaction

There is no license information available for the latest version (1.0.0) of this package.

Library for dealing with reactions of actions

1.0.0 2018-09-02 09:40 UTC

This package is not auto-updated.

Last update: 2024-04-22 13:42:05 UTC


README

Travis Packagist

This library makes it easy to handle reactions of actions, methods and functions. If you are dealing with methods that can either be successful or fail and want more information about the result of the method, you can use a reaction.

Installation

You can install reaction using Composer:

$ composer require aaronmacaron/reaction

To install and use the reaction library you need PHP version 7.1 or newer.

Example

You can use this library as follows:

<?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.