-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
141 additions
and
9 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
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,20 @@ | ||
<?php | ||
|
||
namespace fphp\Utils; | ||
|
||
use ReflectionFunction; | ||
use ReflectionMethod; | ||
|
||
function reflexify($f) { | ||
if (is_string($f) && strpos($f, '::', 1) !== false) { | ||
$reflection = new ReflectionMethod($f); | ||
} elseif (is_array($f) && count($f) === 2) { | ||
$reflection = new ReflectionMethod($f[0], $f[1]); | ||
} elseif (is_object($f) && method_exists($f, '__invoke')) { | ||
$reflection = new ReflectionMethod($f, '__invoke'); | ||
} else { | ||
$reflection = new ReflectionFunction($f); | ||
} | ||
|
||
return $reflection; | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace fphp; | ||
|
||
/** | ||
* Return true for every falsy value and false for every truthy value. | ||
* | ||
* @param mixed $value value | ||
* @return mixed | ||
* @author Rémy Peru <peru.remy@gmail.com> | ||
*/ | ||
function not($value) { | ||
return !$value; | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace fphp; | ||
|
||
/** | ||
* Complement of filter | ||
* | ||
* @param callable $f function | ||
* @param array $collection collection | ||
* @return $array | ||
* @author Rémy Peru <peru.remy@gmail.com> | ||
*/ | ||
function reject(...$args) { | ||
$reject = function (callable $f, $collection) { | ||
$result = []; | ||
foreach ($collection as $element) { | ||
if (!$f($element)) { | ||
$result[] = $element; | ||
} | ||
} | ||
|
||
return $result; | ||
}; | ||
|
||
return curry($reject)(...$args); | ||
} |
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,34 @@ | ||
<?php | ||
|
||
use function fphp\Utils\reflexify; | ||
|
||
class TestClass { | ||
public function __invoke() { | ||
echo 'test'; | ||
} | ||
|
||
static public function staticTest() { | ||
return true; | ||
} | ||
} | ||
|
||
describe('reflexify', function () { | ||
it('should get reflection of callable', function () { | ||
$closure = reflexify('TestClass::staticTest'); | ||
expect($closure)->toBeAnInstanceOf('ReflectionMethod'); | ||
|
||
$closure = reflexify(['DateTime', 'createFromFormat']); | ||
expect($closure)->toBeAnInstanceOf('ReflectionMethod'); | ||
|
||
$invoke = new TestClass(); | ||
$closure = reflexify($invoke); | ||
expect($closure)->toBeAnInstanceOf('ReflectionMethod'); | ||
|
||
$closure = reflexify('explode'); | ||
expect($closure)->toBeAnInstanceOf('ReflectionFunction'); | ||
|
||
$f = function($a, $b) { return $a + $b; }; | ||
$closure = reflexify($f); | ||
expect($closure)->toBeAnInstanceOf('ReflectionMethod'); | ||
}); | ||
}); |
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,17 @@ | ||
<?php | ||
|
||
use function fphp\not; | ||
|
||
describe('not', function () { | ||
it('should return true', function () { | ||
$res = not(0); | ||
|
||
expect($res)->toBe(true); | ||
}); | ||
|
||
it('should return false', function () { | ||
$res = not(true); | ||
|
||
expect($res)->toBe(false); | ||
}); | ||
}); |
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,22 @@ | ||
<?php | ||
|
||
use function fphp\reject; | ||
|
||
describe('reject', function () { | ||
it('should keep every dogs', function () { | ||
$f = function($x) { return $x !== 'dog'; }; | ||
|
||
$res = reject($f, ['cat', 'cat', 'dog', 'cat', 'dog']); | ||
|
||
expect($res)->toBe(['dog', 'dog']); | ||
}); | ||
|
||
it('should be curried', function () { | ||
$f = function($x) { return $x !== 'dog'; }; | ||
|
||
$res = reject($f); | ||
$res = $res(['cat', 'cat', 'dog', 'cat', 'dog']); | ||
|
||
expect($res)->toBe(['dog', 'dog']); | ||
}); | ||
}); |