This function has been integrated to apantle/fun-php
Last active
September 3, 2019 23:33
-
-
Save tzkmx/ebfae26982636662168963391cedc854 to your computer and use it in GitHub Desktop.
Expansor of single value to hashMap through specified mapper(s)
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
{ | |
"name": "tzkmx/unfold", | |
"description": "Expansor of single value to hashMap through specified mapper(s)", | |
"type": "library", | |
"autoload": { | |
"files": ["unfold.php"] | |
}, | |
"require-dev": { | |
"phpunit/phpunit": "~7.0", | |
"apantle/fun-php": "^0.2.2", | |
"apantle/hashmapper": "^1.2" | |
}, | |
"license": "MIT", | |
"authors": [ | |
{ | |
"name": "Jesus E. Franco Martinez", | |
"email": "jefrancomix@gmail.com" | |
} | |
], | |
"require": { | |
"php": ">7.0" | |
} | |
} |
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
<phpunit colors="true" bootstrap="vendor/autoload.php"> | |
<testsuites> | |
<testsuite name="unit"> | |
<file>Test.php</file> | |
</testsuite> | |
</testsuites> | |
</phpunit> |
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
<?php | |
namespace tzkmx; | |
function unfold($specs, $tameme = null) | |
{ | |
return function ($input, $optional = null) use ($specs, $tameme) { | |
$mecapal = []; | |
foreach ($specs as $target => $mapper) { | |
$mecapal[$target] = isUnaryFn($mapper) | |
? call_user_func($mapper, $input) | |
: call_user_func($mapper, $input, $optional, $tameme) | |
; | |
} | |
return $mecapal; | |
}; | |
} | |
function isUnaryFn(callable $callable) | |
{ | |
$reflector = new \ReflectionFunction($callable); | |
return boolval($reflector->getNumberOfParameters() === 1); | |
} |
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
<?php | |
use \PHPUnit\Framework\TestCase; | |
use function tzkmx\unfold; | |
use function Apantle\FunPHP\identity; | |
use function Apantle\Hashmapper\hashMapper; | |
class UnfoldTest extends TestCase | |
{ | |
public function testBasicUnfold() | |
{ | |
$input = new \DateTimeImmutable(); | |
$expected = [ | |
'targetA' => $input, | |
'targetB' => $input | |
]; | |
$actual = unfold([ | |
'targetA' => 'Apantle\FunPHP\identity', | |
'targetB' => 'Apantle\FunPHP\identity', | |
])($input); | |
$this->assertEquals($expected, $actual); | |
$this->assertEquals($input->format('U'), $actual['targetA']->format('U')); | |
$this->assertEquals($input->format('U'), $actual['targetB']->format('U')); | |
} | |
public function testExpectsTameme() | |
{ | |
$input = [ 1, 2, 3 ]; | |
$expected = [ | |
'targetA' => [ 4 => 1, 5 => 2, 6 => 3 ], | |
'targetB' => [ 5 => 2 ] | |
]; | |
$tameme = new class extends ArrayObject {}; | |
$actual = unfold([ | |
'targetA' => function ($member, $input = null, $tameme) { | |
$build = array_reduce($member, function ($accum, $num) { | |
$accum[$num + 3] = $num; | |
return $accum; | |
}, []); | |
$tameme['prev'] = $build; | |
return $build; | |
}, | |
'targetB' => function ($member, $input, $tameme) { | |
$prev = $tameme['prev']; | |
$build = []; | |
foreach($prev as $key => $val) { | |
if($key % 2 !== 0) { | |
$build[$key] = $val; | |
} | |
} | |
return $build; | |
} | |
], $tameme)($input); | |
$this->assertEquals($expected, $actual); | |
} | |
public function testReceivesOptionalArgument() | |
{ | |
$input = [ | |
'vendor' => 'tzkmx', | |
'utility' => 'unfold' | |
]; | |
$expected = [ | |
'vendorName' => 'tzkmx', | |
'vendorLen' => 5, | |
'serialized' => 'a:2:{s:6:"vendor";s:5:"tzkmx";s:7:"utility";s:6:"unfold";}', | |
'utility' => 'unfold', | |
'utilLen' => 6, | |
'package' => [ 'tzkmx/unfold' => $input ] | |
]; | |
$tameme = new class extends ArrayObject {}; | |
$actual = hashMapper([ | |
'vendor' => [ '...', unfold([ | |
'vendorName' => 'strval', | |
'vendorLen' => 'strlen', | |
'serialized' => function ($member, $hash, $tameme) { | |
$tameme['name'] = $member; | |
return serialize($hash); | |
} | |
], $tameme) | |
], | |
'utility' => [ '...', unfold([ | |
'utility' => 'strval', | |
'utilLen' => 'strlen', | |
'package' => function ($member, $hash, $tameme) { | |
$name = $tameme['name']; | |
return [ "$name/$member" => $hash ]; | |
} | |
], $tameme) | |
] | |
])($input); | |
$this->assertEquals($expected, $actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment