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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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

  1. Option Interface
  2. Usage
  3. Methods
  4. Complementary Packages
  5. Static Analysis
  6. Contributing
  7. Security Vulnerabilities
  8. 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:

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.

PhpResult

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.


All versions of phpoption with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package brimmar/phpoption contains the following files

Loading the files please wait ....