Skip to content

Commit

Permalink
Merge pull request #46 from jorgeavilacardenosa/do-if-function
Browse files Browse the repository at this point in the history
do_if function
  • Loading branch information
jorgeavilacardenosa committed Mar 2, 2016
2 parents 909eddb + 6d7e41b commit 9b9ec64
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [complement](functions/complement.md): Returns another function that takes the same arguments and has the opposite truth value.
* [compose](functions/compose.md): Combine multiple function calls in one function
* [dissoc](functions/dissoc.md): Dissociate a value of a key in a collection
* [do_if](functions/do_if.md): 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
* [each](functions/each.md): Apply a function over all the items of a collection
* [filter](functions/filter.md): Discriminate the items of a collection for which function is false
* [first](functions/first.md): Returns the first element of a collection
Expand Down
27 changes: 27 additions & 0 deletions docs/functions/do_if.md
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]);

```
1 change: 1 addition & 0 deletions src/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require __DIR__ . '/complement.php';
require __DIR__ . '/compose.php';
require __DIR__ . '/dissoc.php';
require __DIR__ . '/do_if.php';
require __DIR__ . '/each.php';
require __DIR__ . '/filter.php';
require __DIR__ . '/first.php';
Expand Down
25 changes: 25 additions & 0 deletions src/do_if.php
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;
};
}
43 changes: 43 additions & 0 deletions tests/DoIfTest.php
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;
};
}
}

0 comments on commit 9b9ec64

Please sign in to comment.