marvin255/optional

Optional container for PHP.

v0.1.6 2024-06-08 13:08 UTC

This package is auto-updated.

Last update: 2024-10-31 08:03:54 UTC


README

Latest Stable Version Total Downloads License Build Status

PHP implementation of Java's Optional object.

A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.

Installation

Install via composer:

composer req marvin255/optional

Usage

use Marvin255\Optional\Optional;

$optional = Optional::of($input);
if ($optional->isPresent()) {
    $value = $optional->get();
    // do something
}

With lambda

use Marvin255\Optional\Optional;

Optional::of($input)->ifPresent(function ($item): void {/* do something */});