dxw / result
Result values
Installs: 94 623
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 13
Forks: 1
Open Issues: 0
Requires
- php: ^7.4||^8.1
Requires (Dev)
- dxw/php-cs-fixer-config: ^2.1
- kahlan/kahlan: ^5.2
- vimeo/psalm: ^4
This package is auto-updated.
Last update: 2024-12-19 14:03:33 UTC
README
Meant to be a drop-in replacement for nikita2206/result, with one addition: wrap()
.
wrap()
was inspired by github.com/pkg/errors.
Installation
composer require dxw/result
Usage
Returning values:
function myfunc(): \Dxw\Result\Result
{
if (/* error */) {
// getErr() will return 'something went wrong'
return \Dxw\Result\Result::err('something went wrong');
}
return \Dxw\Result\Result::ok($value);
}
Handling Result
values:
$result = myfunc();
if ($result->isErr()) {
echo sprintf("Error: %s\n", $result->getErr());
exit(1);
}
$value = $result->unwrap();
Returning errors from other errors:
function anotherfunc(): \Dxw\Result\Result
{
$result = myfunc();
if ($result->isErr()) {
// getErr() will return 'got invalid value: something went wrong'
return $result->wrap('got invalid value');
}
// do something with $result->unwrap()
}