PHPackages                             omitobisam/conditional - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. omitobisam/conditional

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

omitobisam/conditional
======================

A fluent helper for object-oriented style of if-else statements

2.0.0(2y ago)43156[4 issues](https://github.com/omitobi/conditional/issues)MITPHPPHP &gt;=7.4CI failing

Since Apr 16Pushed 2y ago2 watchersCompare

[ Source](https://github.com/omitobi/conditional)[ Packagist](https://packagist.org/packages/omitobisam/conditional)[ Docs](https://omitobi.github.io/conditional/)[ RSS](/packages/omitobisam-conditional/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (8)Dependencies (2)Versions (12)Used By (0)

[![](https://github.com/omitobi/assets/raw/master/conditional/twitter_header_photo_2.png)](https://github.com/omitobi/assets/blob/master/conditional/twitter_header_photo_2.png)

[ ![Build Status](https://camo.githubusercontent.com/e040cf6063f181df4ec4d8e329319c0194311dbcb6603bfd5639a5be56bc4d58/68747470733a2f2f7472617669732d63692e636f6d2f6f6d69746f62692f636f6e646974696f6e616c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/omitobi/conditional)[ ![Latest Stable Version](https://camo.githubusercontent.com/80cf9807986f7911a1c4928c5ee8e7025e78d58e1fc4754c11125ac2d319b26f/68747470733a2f2f706f7365722e707567782e6f72672f6f6d69746f626973616d2f636f6e646974696f6e616c2f762f737461626c65)](https://packagist.org/packages/omitobisam/conditional)[ ![Total Downloads](https://camo.githubusercontent.com/0f66c1d2c96f73401dd3e4c943228dc498e6b1836497e28fdc76b37bde6155d9/68747470733a2f2f706f7365722e707567782e6f72672f6f6d69746f626973616d2f636f6e646974696f6e616c2f646f776e6c6f616473)](https://packagist.org/packages/omitobisam/conditional)[ ![Latest Unstable Version](https://camo.githubusercontent.com/58ae4f2b1c8791cc4fd893c3e4e0677056e33746e5c2e872132d96c798eae05c/68747470733a2f2f706f7365722e707567782e6f72672f6f6d69746f626973616d2f636f6e646974696f6e616c2f762f756e737461626c65)](https://packagist.org/packages/omitobisam/conditional)[ ![Latest Monthly Downloads](https://camo.githubusercontent.com/dafe55e24dfaa60c592036efffaec107f9827b7a55c4db49cdd1182091b1d518/68747470733a2f2f706f7365722e707567782e6f72672f6f6d69746f626973616d2f636f6e646974696f6e616c2f642f6d6f6e74686c79)](https://packagist.org/packages/omitobisam/conditional) [ ![License](https://camo.githubusercontent.com/2368c28f2c06345750efa153900ea0e16a92a799eb2687e01349e4da52c54b87/68747470733a2f2f706f7365722e707567782e6f72672f6f6d69746f626973616d2f636f6e646974696f6e616c2f6c6963656e7365)](https://packagist.org/packages/omitobisam/conditional)

About Conditional
-----------------

[](#about-conditional)

if-else statements in a cleaner and beautiful way.

```
conditional(isset($data))
    ->then(fn() => doThis())
    ->else(fn() => doThat());
```

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

[](#installation)

`composer require omitobisam/conditional`

Minimum Requirement
-------------------

[](#minimum-requirement)

- PHP &gt;=7.4

Usage
-----

[](#usage)

You can call it simply statically:

```
use Conditional\Conditional;
$data = null;

Conditional::if(is_null($data))
    ->then(fn() => doThis())
    ->else(fn() => doThat());
```

Conditional also comes with a helper function called `conditional()` and its used like so:

```
conditional(isset($data))
    ->then(fn() => doThis())
    ->else(fn() => doThat());
```

🎉 Now like a tenary operator. Conditional at version 1.2 `else()` immediately returns the value of the last truthy execution:

```
conditional('1' === 'a', 1, 2); //returns 2 - without calling ->value()

conditional(false, 1)

  ->else(2); //returns 2 - without calling ->value()

// Of course the normal one
conditional(false)

  ->then(1)

  ->else(2); //returns 2
```

You can also evaluate a closure call on the conditional `if` method:

```
use Conditional\Conditional;

Conditional::if(fn() =>  '1' == 1) // or conditional(fn() => 1 + 1)
    ->then(fn() => doThis()) // doThis() is executed
    ->else(fn() => doThat());
```

Conditional also allows returning values passed in the conditions. You use `value()` method to get the values either the result of the closure passed or the values as they are.

```
use Conditional\Conditional;

$value = Conditional::if(fn() => 'a' !== 1) // or conditional(fn() => 'a' !== 1)
    ->then(1)
    ->value(); // returns 2 (because a !== 1)

//do something with $value
```

Finally, `then()` and `else()` methods accepts invokable class or objects. Lets see:

```
use Conditional\Conditional;

class Invokable {

    public function __invoke()
    {
        return 'I was Invoked';
    }
}

$invokableClass = new Invokable();

$value = Conditional::if(fn() => 'a' === 1) // or conditional(fn() => 1 + 1)
    ->then(1)
    ->else($invokableClass); //Value returns 'I was Invoked'

// Do something with $value
```

You are also allowed to throw exception based on the condition like so:

```
 \conditional('foo' === 'bar')

    ->then('foo === bar')

    ->else(new TestException('Foo is not the same as bar'));  //this exception is thrown
```

### Newly released

[](#newly-released)

`elseIf()` method of Conditional like so:

```
conditional(isset($data))

    ->then(fn() => doThis())

    ->elseIf(is_int(1))

    ->then(fn() => doThat())

    ->else(2);
```

`elseIf()` can be called multiple times on an instance:

```
$value = Conditional::if(false)

    ->then('a')

    ->elseIf('b' == 1) //first one

    ->then('b')

    ->elseIf('b' !== 2) //another

    ->then('2')

    ->else(1);

// $value is '2'
```

### Coming Soon

[](#coming-soon)

`If()` and `elseIf()` statement accepting a default value when no condition is met and `else()` is not called like so:

```
Conditional::if(is_array('a'), 'ninja') //default value is ninja

    ->then(fn() => doThis())

    ->elseIf(is_int(""))

    ->then(fn() => doThat())

    ->value(); // 'ninja' is returned :scream:
```

Multiple conditional check like `a && b && c && d` or `a || b || c ||...` syntax

- Help wanted for this

Caveats (or Awareness)
----------------------

[](#caveats-or-awareness)

- As at version 1.x, Calling `if()` method returns an instance of Condtional, so do not call it twice on the same instance for example:

```
// This is Wrong!

Conditional::if(true)
    ->then(1)
    ->else(2)
    ->if('1'); // Don't do it except you intend to start a new and fresh if Conditional
```

> See: tests/ConditionalTest::testEveryIfCallCreatesNewFreshInstance test. On the last line of that test, the two conditionals are not the same.

- Conditional relies on closures to return non closure values passed to then.

> In the future release it will be optional for `then` and `else` method

Contributions
-------------

[](#contributions)

- More tests are needed
- Issues have been opened
- How about those static properties, any idea how to reduce the number of static properties used?
- Performance optimization (?)

Development
-----------

[](#development)

For new feature, checkout with prefix `feat-#issueid` e.g `feature-#100-add-auto-deloy`

-
- Clone this repository
- run `sh dockerizer.sh` or `bash dockerizer.sh`
- execute into the docker environment with `docker-compose exec conditional sh` (`sh` can be another bash)
- run tests with `vendor/bin/phpunit`

Licence
-------

[](#licence)

MIT (see LICENCE file)

Additional Information
----------------------

[](#additional-information)

Other related packages:

-  \[A functional PHP pipe in object-oriented way\]
-  \[A smart PHP array class object-oriented way\]
-  \[A smart PHP try...catch statement\]
-  \[A smart Carbon + Collection package\]
-  \[Jsonable Http Request(er) package with Collections response\]

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity62

Established project with proven stability

 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 ~147 days

Recently: every ~368 days

Total

11

Last Release

747d ago

Major Versions

v0.1-alpha → 1.0.02020-04-16

1.3.0 → 2.x-dev2024-04-30

PHP version history (2 changes)v0.1-alphaPHP &gt;=7.4

1.x-devPHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/364011bafbabff7b8c66b670b16ae372944dd4cc555531affffd901aae53d297?d=identicon)[omitobisam](/maintainers/omitobisam)

---

Top Contributors

[![omitobi](https://avatars.githubusercontent.com/u/16482234?v=4)](https://github.com/omitobi "omitobi (92 commits)")

---

Tags

fluent-helperif-statementsoopphplaravelhelpersconditionals

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/omitobisam-conditional/health.svg)

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

###  Alternatives

[transprime-research/piper

PHP Pipe method execution with values from chained method executions

174.6k2](/packages/transprime-research-piper)

PHPackages © 2026

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