A container for keyed booleans.

0.1.1 2017-12-02 17:04 UTC

This package is auto-updated.

Last update: 2021-09-21 13:52:04 UTC


Build Status Latest Stable Version License


Optionally is a keyed store of booleans. It can be used for anything but usually to store options.


Via Composer:

composer require aviator/optionally


Via Composer:

composer test


Instantiate Optionally with or without an array. You can add the array later with add() or replaceWith() (see below).

The array should have string keys and boolean values. It's important to note that any pairs with non-string keys and non-boolean values will be silently discarded—they will not be coerced.

$options = Optionally::make([
    'option1' => true, 
    'option2' => false, 
    0 => false, 
    'test' => 'value'

// [0 => false] and ['test' => 'value'] will be discarded.

Get the underlying array with all():


// ['option1' => true, 'option2' => false]

Get the keys of the underlying array with keys():


// ['option1', 'option2']

Get the value of a key if it exists (or null if it doesn't) with get():


// true


// null

Find whether or not a key exists with has():


// true


// false

Trash the existing options and replace them with replaceWith():

$options->replaceWith(['option3' => true, 'option4' => false]);


// ['option3' => true, 'option4' => false]

Add a new array to the existing options array, overwriting existing keys with add():

$options->add(['option1' => false, 'option3' => true]);


// ['option1' => false, 'option2' => false, 'option3' => true]

Set a single key value pair with set():

$options->set('option3', false);


// ['option1' => true, 'option2' => false, 'option3' => false]

Trash a single key value pair with remove():



// ['option2' => true]

An instance of Optionally is iterable:

foreach ($options as $key => $value) {
    /* ... */

It's also countable:


// 2



This package is licensed with the MIT License (MIT).