azjezz / roew
Hack-like ResultOrExceptionWrapper
Requires
- php: ^7.2
Requires (Dev)
- phpunit/phpunit: ^8.2
- vimeo/psalm: ^3.4
This package is auto-updated.
Last update: 2021-04-11 21:30:17 UTC
README
Hack-like result or exception wrapper for PHP.
Installation
This package can be installed with Composer.
$ composer require azjezz/roew
Usage
<?php use Roew as w; $wrapper = w\result(5); assert($wrapper->isSucceeded()); $result = $wrapper->getResult(); $wrapper = w\exception(new Exception('failed')); assert($wrapper->isFailed()); $e = $wrapper->getException(); $wrapper = w\wrap( fn() => bin2hex(random_bytes(32)) ); if ($wrapper->isSucceeded()) { echo 'Succeeded : '.$wrapper->getResult(); } else { echo 'Failed : '.$wrapper->getException()->getMessage(); }
API
abstract Roew\ResultOrExceptionWrapper<T>
Represents a result of operation that either has a successful result or the exception object if that operation failed.
This is an abstract class. You get generally
ResultOrExceptionWrapper<T>
by callingwrap<T>()
, passing in thecallable(): T
, and aWrappedResult<T>
orWrappedException<Te, Tr>
is returned.
@template T
public function Roew\ResultOrExceptionWrapper<T>::getResult(): T
Return the result of the operation, or throw underlying exception.
- if the operation succeeded: return its result.
- if the operation failed: throw the exception inciting failure.
@return T
- The result of the operation upon success
public Roew\ResultOrExceptionWrapper::getException(): Exception
Return the underlying exception, or fail with a logic exception.
- if the operation succeeded: fails with a logic exception.
- if the operation failed: returns the exception indicating failure.
@throws \LogicException
- When the operation succeeded
public Roew\ResultOrExceptionWrapper::isSucceeded(): bool
Indicates whether the operation associated with this wrapper existed normally.
if
isSucceeded()
returnstrue
,isFailed()
returns false.
@return bool
-true
if the operation succeeded;false
otherwise
public Roew\ResultOrExceptionWrapper::isFailed(): bool
Indicates whether the operation associated with this wrapper exited abnormally via an exception of some sort.
if
isFailed()
returnstrue
,isSucceeded()
returns false.
@return bool
-true
if the operation failed;false
otherwise
References
- https://docs.hhvm.com/hack/reference/interface/HH.Asio.ResultOrExceptionWrapper/
- https://docs.hhvm.com/hack/reference/class/HH.Asio.WrappedResult/
- https://docs.hhvm.com/hack/reference/class/HH.Asio.WrappedException/
License
The Roew Project is open-sourced software licensed under the MIT license.