option-result - Rust-style Option and Result Classes for PHP#
This library contains two classes: Option and Result.
Option<T> represents an optional value. An option may be some or none, where some(T) contains a value and none does not.
Result<T,E> represents a success (ok(T)) or an error (err(E)).
Installation#
composer require ciarancoza/option-result
Usage#
Option#
function findUser(int $id): Option {
$user = User::find($id);
return $user ? Option::Some($user) : Option::None();
}
function getUserTheme(int $userId): string {
return findUser($userId)
->map(fn ($user) => $user->theme)
->map(fn ($theme) => strtolower($theme))
->unwrapOr('auto');
}
Result#
function fetchOrgData(int $id): Result {
try {
$response = Http::get("/api/orgs/{$id}");
if ($response->failed()) return Result::Err("API request failed: " . $response->status());
return Result::Ok($response->json());
} catch (Exception $e) {
return Result::Err("Connection error: " . $e->getMessage());
}
}
function getActiveEmails(int $orgId): array {
return fetchOrgData($orgId)
->map(fn ($org) => $org->getEmails())
->map(fn ($emails) => $emails['active'])
->mapErr(fn ($error) => "Failed to get active emails: " . $error)
->mapErr(fn ($error) => Log::error($error))
->unwrapOr([]);
}
You can view the generated documentation for more usage details.
Road Map#
- Add useful methods
- Option
-
filter() -
inspect() -
reduce() -
replace() -
take() -
takeIf() -
None() -
Some() -
and() -
andThen() -
expect() -
isNone() -
isSome() -
map() -
mapOr() -
unwrap() -
unwrapOr()
-
- Result
-
inspect() -
inspectErr() -
or() -
tryCatch() -
Err() -
Ok() -
and() -
andThen() -
expect() -
expectErr() -
getErr() -
getOk() -
isErr() -
isOk() -
map() -
mapErr() -
mapOr() -
unwrap() -
unwrapErr() -
unwrapOr(mixed $default)
-
- Option
- Refactor
- Use clear if statements everywhere match doesn't make sense
- Review compared to rust types
- Input / Output types
- Type Docs
- Re-generate docs
Contributing#
Go for it! There are plenty of useful Option / Result features in Rust we could implement in this library.