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.
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(...));Via Composer:
composer require hereldar/resultsRun the following commands from the project folder:
make tests
make static-analysis
make coding-standardsTo execute:
- A PHPUnit test suite.
- PHPStan and Psalm for static code analysis.
- Easy Coding Standard to fix coding standards.
The MIT License (MIT). Please see LICENSE for more information.