Download the PHP package brimmar/phpoption without Composer
On this page you can find all versions of the php package brimmar/phpoption. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brimmar/phpoption
More information about brimmar/phpoption
Files in brimmar/phpoption
Package phpoption
Short Description Rust-like Option Type implementation for PHP
License MIT
Informations about the package phpoption
PHP Option Type Documentation
This documentation covers the implementation of a Rust-like Option Type for PHP. The Option type is used for representing optional values. It has two variants: Some
, representing the presence of a value, and None
, representing the absence of a value.
Table of Contents
- Option Interface
- Usage
- Methods
- Complementary Packages
- Static Analysis
- Contributing
- Security Vulnerabilities
- License
Option Interface
The Option
interface defines the contract for both Some
and None
classes.
Usage
Here are several examples showcasing the use and utility of the Option type:
Example 1: User Profile Management
Example 2: Configuration Management with Option and Result
This example demonstrates how Option and Result types can work together for robust configuration management.
Example 3: Optional Chaining with Option Type
This example demonstrates how the Option type can be used to safely chain method calls, similar to optional chaining in other languages.
Methods
isSome(): bool
Returns true
if the option is a Some
value.
Example:
isSomeAnd(callable $fn): bool
Returns true
if the option is a Some
value and the value inside of it matches a predicate.
Example:
isNone(): bool
Returns true
if the option is a None
value.
Example:
iter(): Iterator
Returns an iterator over the possibly contained value.
Example:
unwrap(): mixed
Returns the contained Some
value or throws an exception if the value is None
.
Example:
expect(string $msg): mixed
Returns the contained Some
value or throws an exception with a provided custom message if the value is None
.
Example:
flatten(): Option
Converts from Option<Option<T>>
to Option<T>
.
Example:
unwrapOr(mixed $default): mixed
Returns the contained Some
value or a provided default.
Example:
unwrapOrElse(callable $default): mixed
Returns the contained Some
value or computes it from a closure.
Example:
map(callable $fn): Option
Maps an Option<T>
to Option<U>
by applying a function to a contained value.
Example:
mapOr(mixed $default, callable $fn): mixed
Applies a function to the contained value (if any), or returns a default (if not).
Example:
mapOrElse(callable $default, callable $fn): mixed
Applies a function to the contained value (if any), or computes a default (if not).
Example:
inspect(callable $fn): Option
Calls the provided closure with a reference to the contained value (if Some
).
Example:
okOr(mixed $error, ?string $okClassName = '\Brimmar\PhpResult\Ok'): mixed
Transforms the Option<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(error)
.
Example:
okOrElse(callable $error, ?string $okClassName = '\Brimmar\PhpResult\Ok'): mixed
Transforms the Option<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(error())
.
Example:
and(Option $opt): Option
Returns None
if the option is None
, otherwise returns opt
.
Example:
andThen(callable $fn): Option
Returns None
if the option is None
, otherwise calls fn
with the wrapped value and returns the result.
Example:
or(Option $opt): Option
Returns the option if it contains a value, otherwise returns opt
.
Example:
orElse(callable $fn): Option
Returns the option if it contains a value, otherwise calls fn
and returns the result.
Example:
transpose(?string $okClassName = '\Brimmar\PhpResult\Ok', ?string $errClassName = '\Brimmar\PhpResult\Err'): mixed
Transposes an Option
of a Result
into a Result
of an Option
.
Example:
xor(Option $opt): Option
Returns Some
if exactly one of $this
, $opt
is Some
, otherwise returns None
.
Example:
zip(Option $other): Option
Zips $this
with another Option
.
Example:
zipWith(Option $other, callable $fn): Option
Zips $this
and another Option
with function $fn
.
Example:
unzip(): array
Unzips an option containing a tuple of two options.
Example:
match(callable $Some, callable $None): mixed
Applies a function to retrieve a contained value.
Example:
filter(callable $predicate): Option
Returns None
if the option is None
, otherwise calls predicate
with the wrapped value and returns:
Some(t)
ifpredicate
returnstrue
(wheret
is the wrapped value)None
ifpredicate
returnsfalse
Example:
Complementary Packages
This package works well with the PHP Result Type package, which implements the Result Type. Some methods in this package, such as okOr
and okOrElse
, return Result types.
Static Analysis
We recommend using PHPStan for static code analysis. This package includes custom PHPStan rules to enhance type checking for Option types. To enable these rules, add the following to your PHPStan configuration:
Contributing
Please see CONTRIBUTING.md for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
This project is licensed under the MIT License. Please see LICENSE.md for more information.