Created
July 31, 2013 13:28
-
-
Save ackintosh/6121960 to your computer and use it in GitHub Desktop.
Compose functions in PHP.
This file contains hidden or 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 | |
class Compose | |
{ | |
public static function _() | |
{ | |
$callables = func_get_args(); | |
return array_reduce($callables, function ($a, $b) { | |
if ($a === null) { | |
return function () use ($b) { | |
return call_user_func_array($b, func_get_args()); | |
}; | |
} else { | |
return function ($arg) use ($a, $b) { | |
return call_user_func($b, $a($arg)); | |
}; | |
} | |
}); | |
} | |
} | |
$mapUcfirst = function ($arg) { | |
return array_map('ucfirst', $arg); | |
}; | |
$splitWithUnderscore = function ($arg) { | |
return explode('_', $arg); | |
}; | |
$join = function ($arg) { | |
return implode('', $arg); | |
}; | |
$camelize = Compose::_($splitWithUnderscore, $mapUcfirst, $join); | |
var_dump($camelize('man_with_a_mission')); | |
// ManWithAMission |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment