hereldar / results
Allows developers to handle the results in any way they choose.
0.9.0
2023-10-16 21:32 UTC
Requires
- php: ^8.1.17|^8.2.4|^8.3.0
Requires (Dev)
- fakerphp/faker: ^1.21
- phpunit/phpunit: ^10.1
README
This package includes an opinionated version of the Result
type of
Rust. It is not intended to replicate the original type one-to-one,
but to allow developers to handle the results in any way they choose.
Use examples
This Result
type allows ignoring errors without a try-catch block:
$value = getValue()->or($default);
It also allows throwing the error as a regular exception:
doSomething()->orFail();
Or throwing a custom exception:
doSomething()->orThrow(new MyException());
More complex flows can be handled by concatenating operations:
$record = fetchRecord() ->andThen(updateIt(...)) ->orElse(insertIt(...)) ->orFail();
And much more:
doSomething() ->onFailure(logFailure(...)) ->onSuccess(logSuccess(...)) ->onSuccess(doSomethingElse(...));
Installation
Via Composer:
composer require hereldar/results
Development
Run the following commands from the project folder:
make tests make static-analysis make coding-standards
To execute:
- A PHPUnit test suite.
- PHPStan and Psalm for static code analysis.
- Easy Coding Standard to fix coding standards.
Documentation
Credits
License
The MIT License (MIT). Please see LICENSE for more information.