Skip to content

Instantly share code, notes, and snippets.

@yuya-takeyama
Last active October 8, 2015 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuya-takeyama/3316674 to your computer and use it in GitHub Desktop.
Save yuya-takeyama/3316674 to your computer and use it in GitHub Desktop.
<?php
class ComposableFunction
{
/**
* @var callable
*/
private $fn;
/**
* Constructor
*
* @param callable $fn
*/
public function __construct(callable $fn)
{
$this->fn = $fn;
}
/**
* Invokes function
*/
public function __invoke()
{
return call_user_func_array($this->fn, func_get_args());
}
/**
* Composes new function
*/
public function compose(callable $fn)
{
return new static(function () use ($fn) {
return $fn(call_user_func_array($this, func_get_args()));
});
}
}
$splitWithUnderscore = new ComposableFunction(function ($str) {
return explode('_', $str);
});
$mapUcfirst = function ($words) {
return array_map('ucfirst', $words);
};
$camelize = $splitWithUnderscore->compose($mapUcfirst)->compose('join');
var_dump($camelize('foo_bar_baz'));
// => FooBarBaz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment