-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from jorgeavilacardenosa/do-if-function
do_if function
- Loading branch information
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# do_if | ||
|
||
## Description | ||
Returns a function that will call the given function if the result of applying the callable arguments to the predicates is true for all of them | ||
If the result of applying the callable arguments to the predicates is false for any of them, the given function will not be executed (returning null). | ||
|
||
## Parameters | ||
|
||
<dl> | ||
<dt>fn</dt> | ||
<dd>Function to call if all predicates are valid.</dd> | ||
|
||
<dt>predicates</dt> | ||
<dd>Predicates to be validated.</dd> | ||
</dl> | ||
|
||
## Examples | ||
|
||
```php | ||
<?php | ||
|
||
use function Akamon\Phunctional\do_if; | ||
use function Akamon\Phunctional\apply; | ||
|
||
apply(do_if($this->sendBirthdayGift(), [$this->isBirthday(), $this->isAdult()]), [$user]); | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Akamon\Phunctional; | ||
|
||
/** | ||
* Returns a callable that will call the given function if the result of applying | ||
* the callable arguments to the predicates is true for all of them. | ||
* | ||
* @since 0.1 | ||
* | ||
* @param callable $fn Function to call if all predicates are valid. | ||
* @param callable[] $predicates Predicates to validate. | ||
* | ||
* @return callable|null | ||
*/ | ||
function do_if(callable $fn, array $predicates) | ||
{ | ||
return function (...$args) use ($fn, $predicates) { | ||
$isValid = function ($predicate) use ($args) { | ||
return $predicate(...$args); | ||
}; | ||
|
||
return all($isValid, $predicates) ? $fn(...$args) : null; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Akamon\Phunctional\Tests; | ||
|
||
use function Akamon\Phunctional\apply; | ||
use function Akamon\Phunctional\do_if; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
final class DoIfTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** @test */ | ||
public function it_should_call_the_fn_if_predicates_are_true() | ||
{ | ||
$this->assertEquals(5, apply(do_if($this->sumOne(), [$this->isOdd()]), [4])); | ||
} | ||
|
||
/** @test */ | ||
public function it_should_return_null_if_all_predicates_are_not_true() | ||
{ | ||
$this->assertNull(apply(do_if($this->sumOne(), [$this->isGreaterThanOne(), $this->isOdd()]), [3])); | ||
} | ||
|
||
private function sumOne() | ||
{ | ||
return function ($num) { | ||
return $num + 1; | ||
}; | ||
} | ||
|
||
private function isOdd() | ||
{ | ||
return function ($num) { | ||
return $num % 2 === 0; | ||
}; | ||
} | ||
|
||
private function isGreaterThanOne() | ||
{ | ||
return function ($num) { | ||
return $num > 1; | ||
}; | ||
} | ||
} |