Download the PHP package brimmar/phpresult without Composer
On this page you can find all versions of the php package brimmar/phpresult. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package phpresult
PHP Result Type Documentation
This documentation covers the implementation of a Rust-like Result Type for PHP. The Result type is used for returning and propagating errors. It has two variants: Ok
, representing success and containing a value, and Err
, representing error and containing an error value.
Table of Contents
- Result Interface
- Usage
- Methods
- Complementary Packages
- Static Analysis
- Contributing
- Security Vulnerabilities
- License
Result Interface
The Result
interface defines the contract for both Ok
and Err
classes.
Usage
First Example
Second Example
Third Example
Fourth Example
Methods
isOk(): bool
Returns true
if the result is Ok
.
Example:
isOkAnd(callable $fn): bool
Returns true
if the result is Ok
and the value inside of it matches a predicate.
Example:
isErr(): bool
Returns true
if the result is Err
.
Example:
isErrAnd(callable $fn): bool
Returns true
if the result is Err
and the value inside of it matches a predicate.
Example:
ok(?string $className): mixed
Converts from Result<T, E>
to Option<T>
.
Example:
err(?string $className): mixed
Converts from Result<T, E>
to Option<E>
.
Example:
unwrap(): mixed
Returns the contained Ok
value. Throws an exception if the value is an Err
.
Example:
expect(string $msg): mixed
Returns the contained Ok
value. Throws an exception with a provided message if the value is an Err
.
Example:
expectErr(string $msg): mixed
Returns the contained Err
value. Throws an exception with a provided message if the value is an Ok
.
Example:
flatten(): Result
Converts from Result<Result<T, E>, E>
to Result<T, E>
.
Example:
intoErr(): mixed
Returns the contained Err
value. Throws an exception if the value is an Ok
.
Example:
intoOk(): mixed
Returns the contained Ok
value. Throws an exception if the value is an Err
.
Example:
iter(): Iterator
Returns an iterator over the possibly contained value.
Example:
unwrapOr(mixed $default): mixed
Returns the contained Ok
value or a provided default.
Example:
unwrapOrElse(callable $fn): mixed
Returns the contained Ok
value or computes it from a closure.
Example:
map(callable $fn): Result
Maps a Result<T, E>
to Result<U, E>
by applying a function to a contained Ok
value, leaving an Err
value untouched.
Example:
mapErr(callable $fn): Result
Maps a Result<T, E>
to Result<T, F>
by applying a function to a contained Err
value, leaving an Ok
value untouched.
Example:
mapOr(mixed $default, callable $fn): mixed
Returns the provided default (if Err
), or applies a function to the contained value (if Ok
).
Example:
mapOrElse(callable $default, callable $fn): mixed
Maps a Result<T, E>
to U
by applying fallback function default
to a contained Err
value, or function fn
to a contained Ok
value.
Example:
inspect(callable $fn): self
Calls the provided closure with a reference to the contained value (if Ok
).
Example:
inspectErr(callable $fn): self
Calls the provided closure with a reference to the contained error (if Err
).
Example:
and(Result $res): Result
Returns res
if the result is Ok
, otherwise returns the Err
value of self
.
Example:
andThen(callable $fn): Result
Calls fn
if the result is Ok
, otherwise returns the Err
value of self
.
Example:
or(Result $res): Result
Returns self
if it is Ok
, otherwise returns res
.
Example:
orElse(callable $fn): Result
Calls fn
if the result is Err
, otherwise returns the Ok
value of self
.
Example:
transpose(?string $noneClassName, ?string $someClassName): mixed
Transposes a Result
of an Option
into an Option
of a Result
.
Example:
match(callable $Ok, callable $Err): mixed
Matches the result and returns a value based on the provided patterns.
Example:
Complementary Packages
This package works well with the PHP Option Type package, which implements the Option Type. Some methods in this package, such as transpose, depend on the Option Type implementation.
Static Analysis
We recommend using PHPStan for static code analysis. This package includes custom PHPStan rules to enhance type checking for Result types. To enable these rules, add the following to your PHPStan configuration:
Contributing
Please see CONTRIBUTING.md for details.
Security Vulnerabilities
Pleas review our security policy on how to report security vulnerabilities.
License
Please see LICENSE.md for more information.