add object transforms

This commit is contained in:
Kacper Donat 2018-09-07 21:52:21 +02:00
parent 822b939ec8
commit c11a615169
3 changed files with 51 additions and 1 deletions

10
src/Transforms/index.php Normal file
View File

@ -0,0 +1,10 @@
<?php
/**
* Copyright 2018 Kacper Donat
*
* @author Kacper "Kadet" Donat <kacper@kadet.net>
*
* Full license available in separate LICENSE file
*/

View File

@ -0,0 +1,40 @@
<?php
/**
* Copyright 2018 Kacper Donat
*
* @author Kacper "Kadet" Donat <kacper@kadet.net>
*
* Full license available in separate LICENSE file
*/
namespace Kadet\Functional\Transforms;
function getter($property)
{
return method('get'.ucfirst($property));
}
function property($property)
{
return function ($object) use ($property) {
if (method_exists($object, $getter = 'get'.ucfirst($property))) {
return $object->{$getter}();
}
return $object->{$property};
};
}
function field($field)
{
return function ($object) use ($field) {
return $object->{$field};
};
}
function method($method, ...$args)
{
return function ($object) use ($method, $args) {
return $object->{$method}(...$args);
};
}

View File

@ -2,7 +2,6 @@
namespace Kadet\Functional;
use function foo\func;
use Kadet\Functional\Predicate\AllOfPredicate;
use Kadet\Functional\Predicate\AnyOfPredicate;
use Kadet\Functional\Predicate\ClosurePredicate;
@ -14,6 +13,7 @@ use Kadet\Functional\Utils\FixedFunction;
use Kadet\Functional\Utils\PartiallyAppliedFunction;
require_once __DIR__.'/Utils/index.php';
require_once __DIR__.'/Transforms/index.php';
/**
* @param $predicate