https://github.com/danielsdeboer/optionally
A keyed store of boolean options.
https://github.com/danielsdeboer/optionally
booleans keyvaluestore options php php7
Last synced: 6 months ago
JSON representation
A keyed store of boolean options.
- Host: GitHub
- URL: https://github.com/danielsdeboer/optionally
- Owner: danielsdeboer
- License: mit
- Created: 2017-12-02T02:34:48.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-02T17:04:56.000Z (almost 8 years ago)
- Last Synced: 2025-01-30T06:10:00.060Z (8 months ago)
- Topics: booleans, keyvaluestore, options, php, php7
- Language: PHP
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/danielsdeboer/optionally)
[](https://packagist.org/packages/aviator/optionally)
[](https://packagist.org/packages/aviator/optionally)## Overview
Optionally is a keyed store of booleans. It can be used for anything but usually to store options.
### Installation
Via Composer:
```
composer require aviator/optionally
```### Testing
Via Composer:
```
composer test
```### Usage
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.```php
$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()`:
```php
$options->all();// ['option1' => true, 'option2' => false]
```Get the keys of the underlying array with `keys()`:
```php
$options->keys();// ['option1', 'option2']
```Get the value of a key if it exists (or null if it doesn't) with `get()`:
```php
$options->get('option1');// true
$options->get('someOptionThatDoesntExist');
// null
```Find whether or not a key exists with `has()`:
```php
$options->has('option2');// true
$options->has('someOptionThatDoesntExist');
// false
```Trash the existing options and replace them with `replaceWith()`:
```php
$options->replaceWith(['option3' => true, 'option4' => false]);$options->all();
// ['option3' => true, 'option4' => false]
```Add a new array to the existing options array, overwriting existing keys with `add()`:
```php
$options->add(['option1' => false, 'option3' => true]);$options->all();
// ['option1' => false, 'option2' => false, 'option3' => true]
```Set a single key value pair with `set()`:
```php
$options->set('option3', false);$options->all();
// ['option1' => true, 'option2' => false, 'option3' => false]
```Trash a single key value pair with `remove()`:
```php
$options->remove('option1');$options->all();
// ['option2' => true]
```An instance of `Optionally` is iterable:
```php
foreach ($options as $key => $value) {
/* ... */
}
```It's also countable:
```php
count($foreach);// 2
```## Other
### License
This package is licensed with the [MIT License (MIT)](LICENSE).