Skip to content

Commit

Permalink
Merge pull request #45 from omitobi/1.x
Browse files Browse the repository at this point in the history
1.x
  • Loading branch information
omitobi authored Apr 20, 2020
2 parents 9d4b7f5 + 1ed0dcb commit 6e4de92
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
16 changes: 14 additions & 2 deletions Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@
* New up an instance of readied Conditional
*
* @param $condition
* @param mixed $then
* @param mixed $else
* @return Conditional
*/
function conditional($condition)
function conditional($condition, $then = null, $else = null)
{
return Conditional::if($condition);
$conditional = Conditional::if($condition);

if (func_num_args() === 2) {
return $conditional->then($then);
}

if (func_num_args() === 3) {
return $conditional->then($then)->else($else);
}

return $conditional;
}
}
28 changes: 20 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ conditional(isset($data))
->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
Expand All @@ -81,9 +98,8 @@ You use `value()` method to get the values either the result of the closure pass
```php
use Conditional\Conditional;

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

//do something with $value
Expand All @@ -106,8 +122,7 @@ $invokableClass = new Invokable();

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

// Do something with $value
```
Expand Down Expand Up @@ -153,9 +168,7 @@ $value = Conditional::if(false)

->then('2')

->else(1)

->value();
->else(1);

// $value is '2'
```
Expand Down Expand Up @@ -203,7 +216,6 @@ Conditional::if(true)

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

Expand Down
2 changes: 1 addition & 1 deletion src/Conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function else($action)

self::$elseCalled = true;

return $this->then($action);
return $this->then($action)->value();
}

public function then($action)
Expand Down
33 changes: 20 additions & 13 deletions tests/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public function testThatValueIsReceived()

$result = Conditional::if($value === $firstResponse)
->then($firstResponse)
->else($secondResponse)
->value();
->else($secondResponse);

$this->assertEquals($value, $result);
}
Expand All @@ -65,8 +64,7 @@ public function testGetFunctionValue()
{
$result = Conditional::if(fn() => true)
->then(fn() => fn() => 'fn 1')
->else(fn() => fn() => 'fn 2')
->value();
->else(fn() => fn() => 'fn 2');

$this->assertInstanceOf(Closure::class, $result);
}
Expand All @@ -83,8 +81,7 @@ public function __invoke($value = '')

$result1 = Conditional::if($invokable() === 'Invocable')
->then($invokable)
->else($invokable(1))
->value();
->else($invokable(1));

$result2 = Conditional::if(strlen('abcd') === 4)
->then(new Invokable())
Expand Down Expand Up @@ -123,8 +120,7 @@ public function testElseAfterElseIfConditional()
->then(1)
->elseIf('1' === false)
->then(3)
->else(4)
->value();
->else(4);

$this->assertEquals(4, $value);
}
Expand All @@ -147,10 +143,8 @@ public function testElseIfCannotBeCalledAfterElse()
$this->expectExceptionMessage('At least then() condition must be called before calling elseIf');

Conditional::if('1' === true)
->then(1)
->elseIf('1' === false)
->then(3)
->else(5)
->elseIf(true)
->then('abc');
}
Expand Down Expand Up @@ -198,13 +192,26 @@ public function testElseIfCanBeCalledMultipleTimesInAnInstance()

->then('2')

->else(1)

->value();
->else(1);

$this->assertEquals($value, 2);
}

public function testConditionalHelperFunctionAcceptsThenAndElseValues()
{
$this->assertSame('3', conditional(1 === '1', '2', '3'));

$this->assertSame('2', conditional(1 == '1', '2')->value());

$this->assertSame(
conditional(1 === 2, '2')
->elseIf(is_string('a'))
->then('a')
->value(),
'a'
);
}

// public function testIfCannotBeCalledAfterElseIf()
// {
// $this->expectException(InvalidConditionOrderException::class);
Expand Down

0 comments on commit 6e4de92

Please sign in to comment.