Skip to content

Commit

Permalink
Merge pull request #53 from omitobi/2.x
Browse files Browse the repository at this point in the history
2.x
  • Loading branch information
omitobi authored Apr 30, 2024
2 parents ab0c145 + 4fd0c1e commit f09865e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 59 deletions.
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
}
],
"require": {
"php": ">=7.2"
"php": ">=7.4"
},
"require-dev": {
"phpunit/phpunit": "~8.0"
"phpunit/phpunit": "~8.0",
"symfony/var-dumper": "^6.4"
},
"autoload": {
"psr-4": {
Expand All @@ -36,5 +37,8 @@
"psr-4": {
"Conditional\\Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
}
39 changes: 10 additions & 29 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,21 @@

## About Conditional

A (optional but) fluent helper for object-oriented style of if-else statements.
if-else statements in a cleaner and beautiful way.

It helps you construct conditionals as you speak it object Oriented way.

> Some use cases are not yet covered so you can default to `if() else {}` statement.
## Minimum Requirement

- PHP 7.2 +
- Composer
```php
conditional(isset($data))
->then(fn() => doThis())
->else(fn() => doThat());
```

## Installation

Using composer:

`composer require omitobisam/conditional`

or add to the require object of `composer.json` file with the version number:

```json
{
"require": {
"omitobisam/conditional": "^1.2"
}
}
```
## Minimum Requirement

After this run `composer update`
- PHP >=7.4

## Usage

Expand All @@ -56,7 +43,6 @@ Conditional::if(is_null($data))
->else(fn() => doThat());

```
> PS: You can still use the old `function() { return v; }`, `fn()` is the new short arrow function in PHP 7.4+ See: https://www.php.net/manual/en/functions.arrow.php

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

Expand Down Expand Up @@ -207,11 +193,8 @@ Conditional::if(true)
->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 uses `if..else` statements in implementation, how cool is that? :smile:
- 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
- This project at the initial phase is a proof of concept so performance and best practices (?)
> It might be part of something great in the future (especially as a Laravel helper) how cool that would be!
## Contributions

Expand All @@ -222,7 +205,7 @@ Conditional::if(true)

## Development

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

-
- Clone this repository
Expand All @@ -236,9 +219,7 @@ MIT (see LICENCE file)

## Additional Information

Be aware that this package is part of a series of "The Proof of Concept".

See other packages in this series here:
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]
Expand Down
33 changes: 17 additions & 16 deletions src/Conditional.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Conditional;

use Closure;
Expand All @@ -12,21 +14,21 @@
*/
class Conditional
{
private $truthy = false;
private bool $truthy = false;

private $conditionsExists = false;
private bool $conditionsExists = false;

private $ifCalled = false;
private bool $ifCalled = false;

private $thenCalled = false;
private bool $thenCalled = false;

private $elseCalled = false;
private bool $elseCalled = false;

private $elseIfCalled = false;
private bool $elseIfCalled = false;

private $finalValue;

private $finalValueChanged = null;
private ?bool $finalValueChanged = null;

public static function if($condition)
{
Expand Down Expand Up @@ -70,7 +72,7 @@ public function else($action)
return $this->then($action)->value();
}

public function then($action)
public function then($action): self
{
if (!$this->allowThen()) {
throw new InvalidConditionOrderException(
Expand All @@ -79,7 +81,6 @@ public function then($action)
}

if ($this->truthy) {

if ($this->isExceptionClass($action)) {
throw $action;
}
Expand All @@ -103,7 +104,7 @@ public function then($action)
}


public function elseIf($condition)
public function elseIf($condition): self
{
if (!$this->allowElseIf()) {
throw new InvalidConditionOrderException(
Expand All @@ -126,37 +127,37 @@ public function elseIf($condition)
return $this;
}

private function isExceptionClass($action)
private function isExceptionClass($action): bool
{
return is_a($action, \Exception::class);
return is_a($action, \Throwable::class);
}

public function value()
{
return $this->finalValue;
}

private function allowElseIf()
private function allowElseIf(): bool
{
return $this->thenCalled &&
!$this->conditionsExists &&
!$this->elseCalled;
}

private function allowThen()
private function allowThen(): bool
{
return $this->conditionsExists && ($this->ifCalled || $this->elseIfCalled);
}

protected function canBeCalled($value)
protected function canBeCalled($value): bool
{
return (
(is_object($value) && method_exists($value, '__invoke')) ||
($value instanceof Closure)
);
}

private function toggleTruthy()
private function toggleTruthy(): void
{
$this->truthy = !$this->truthy;
}
Expand Down
15 changes: 4 additions & 11 deletions tests/ConditionalTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Conditional\Tests;

use Closure;
Expand Down Expand Up @@ -43,10 +45,10 @@ public function testExecutionFollowsConditions()

Conditional::if($firstResponse === $secondResponse)
->then(function () use ($firstResponse, $secondResponse) {
return $this->assertEquals($firstResponse, $secondResponse);
$this->assertEquals($firstResponse, $secondResponse);
})
->else(function () use ($firstResponse, $secondResponse) {
return $this->assertNotEquals($firstResponse, $secondResponse);
$this->assertNotEquals($firstResponse, $secondResponse);
});
}

Expand Down Expand Up @@ -253,15 +255,6 @@ public function testConditionalTwice()
// ->value();
// }

private function dump(...$expression)
{
var_dump($expression);
}

private function dd()
{
die($this->dump(...func_get_args()));
}
}

class Invokable
Expand Down

0 comments on commit f09865e

Please sign in to comment.