https://github.com/omitobi/conditional
PHP if-else re-imagined
https://github.com/omitobi/conditional
fluent-helper if-statements oop php
Last synced: 3 months ago
JSON representation
PHP if-else re-imagined
- Host: GitHub
- URL: https://github.com/omitobi/conditional
- Owner: omitobi
- License: mit
- Created: 2020-04-13T17:07:49.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-30T21:11:41.000Z (almost 2 years ago)
- Last Synced: 2025-12-09T01:10:09.645Z (5 months ago)
- Topics: fluent-helper, if-statements, oop, php
- Language: PHP
- Homepage: https://omitobi.github.io/conditional/
- Size: 59.6 KB
- Stars: 43
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
## About Conditional
if-else statements in a cleaner and beautiful way.
```php
conditional(isset($data))
->then(fn() => doThis())
->else(fn() => doThat());
```
## Installation
`composer require omitobisam/conditional`
## Minimum Requirement
- PHP >=7.4
## Usage
You can call it simply statically:
```php
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:
```php
conditional(isset($data))
->then(fn() => doThis())
->else(fn() => doThat());
```
:tada: Now like a tenary operator. Conditional at version 1.2 `else()` immediately returns the value of the last truthy execution:
```php
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:
```php
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.
```php
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:
```php
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:
```php
\conditional('foo' === 'bar')
->then('foo === bar')
->else(new TestException('Foo is not the same as bar')); //this exception is thrown
```
### Newly released
`elseIf()` method of Conditional like so:
```php
conditional(isset($data))
->then(fn() => doThis())
->elseIf(is_int(1))
->then(fn() => doThat())
->else(2);
```
`elseIf()` can be called multiple times on an instance:
```php
$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
`If()` and `elseIf()` statement accepting a default value when no condition is met and `else()` is not called like so:
```php
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)
- 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:
```php
// 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
- 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
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
MIT (see LICENCE file)
## Additional Information
Other related packages:
- https://github.com/transprime-research/piper [A functional PHP pipe in object-oriented way]
- https://github.com/transprime-research/arrayed [A smart PHP array class object-oriented way]
- https://github.com/transprime-research/attempt [A smart PHP try...catch statement]
- https://github.com/omitobi/carbonate [A smart Carbon + Collection package]
- https://github.com/omitobi/laravel-habitue [Jsonable Http Request(er) package with Collections response]