PHPackages                             noini/optional - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. noini/optional

ActiveLibrary

noini/optional
==============

Optional class for various if-null-else purposes

1.1.0(8y ago)06MITPHPPHP ^7.1

Since May 12Pushed 8y ago1 watchersCompare

[ Source](https://github.com/noini/optional)[ Packagist](https://packagist.org/packages/noini/optional)[ RSS](/packages/noini-optional/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

Optional
========

[](#optional)

Optional is multipurpose tool to reduce if-null checking in PHP code.

Installation
------------

[](#installation)

Install via composer

```
composer require noini/optional

```

Usage
-----

[](#usage)

### Static create

[](#static-create)

```
Optional::create($payload)->then(...);

```

### Function

[](#function)

```
optional($payload)->then(...);

```

### Simple example

[](#simple-example)

Following example will call callable because $content value is not null.

```
$payload = "content string";
$optional = new Optional($payload);
$optional->then(function($data) {
    echo "Had content: " . $data;
})->otherwise(function($data) {
    echo "I had null value";
});

```

### has()

[](#has)

Has() method can be used to check if Optional payload meets requirement(s).

**Null payload**

If Optional payload is null, then has() will fail and create false result.

```
optional(null)->has(1)->getResult(); // false

```

Null value can be checked with type if needed

```
optional(null)->has(Noini\Optional\Helpers\Types::NULL)->getResult(); // true

```

**Using strict equals comparison**

Compares payload to has() parameter.

```
optional(50)->has(50)->getResult(); // true
optional("50")->has(50)->getResult(); // false

```

**Using type check**

Types class constants can be used to check if payload data type meets requirement.

```
optional("50")->has(Noini\Optional\Helpers\Types::INTEGER)->getResult(); // false
optional("50")->has(Noini\Optional\Helpers\Types::STRING)->getResult(); // true

```

**Class checking**

Passing class will check if payload is instance of given class.

```
optional(new \stdClass())->has(\stdClass::class)->getResult(); // true;

```

**Using callback**

Use callback for customized payload checking. Callback must return boolean.

```
optional(50)->has(function($data) {
    return $data > 10;
})->then(function($data) {
    echo "Value is over 10";
});

```

### then()

[](#then)

Callback function will be called if new instance of Optional has non null value:

```
optional(1)->then(function($data) {
    echo "I had non null value: " . $data;
});

```

Then() can be invoked in constructor by providing callable.

```
optional(50, function($data) {
    echo "Having non null value: " . $data;
});

```

Chained with has() method then() will call callable only if latest has() comparison was successful.

```
optional(50)
    ->has(Types::INTEGER)
    ->then(function($data) {
        echo "Data is a integer";
    });

```

### otherwise()

[](#otherwise)

Otherwise callback will be called if latest has comparison fails.

```
optional(50)
    ->has(function($data) {
        return $data > 9000;
    })->otherwise(function($data) {
        echo "Data was not over 9000";
    });

```

### if-else

[](#if-else)

Has-then method chaining can also be done with if() -method. If() should contain both condition and callback function with one method:

```
optional("data")->if(Types::STRING, function($data) {
    echo "I had a string value: " . $data;
});

```

Use else() method chained with if()

```
optional(null)
    ->if(Types::STRING, function($data) {
        echo "I should not be called this time";
    })
    ->else(function($data) {
        echo "This means optional payload was not a string";
    });

```

**Notice** has-then cannot ne used after if.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

2

Last Release

2922d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/75f6fb1dda8d27c9b5368c2f86a2ecf9976921d367a5699b97c41ebe77395c5f?d=identicon)[noini](/maintainers/noini)

---

Top Contributors

[![noini](https://avatars.githubusercontent.com/u/10671307?v=4)](https://github.com/noini "noini (8 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/noini-optional/health.svg)

```
[![Health](https://phpackages.com/badges/noini-optional/health.svg)](https://phpackages.com/packages/noini-optional)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
