3 ms·
Not too long ago I made something very similar but subtly different: function pipes(callable ...$pipes): Closure { return function($value) use
by smarkov 5y ago
Not too long ago I made something very similar but subtly different:
function pipes(callable ...$pipes): Closure
{
return function($value) use ($pipes) {
foreach ($pipes as $pipe) {
$value = call_user_func($pipe, $value);
}
return $value;
};
}
This makes the pipes reusable and makes it so that functions that take only a single argument can be passed as a plain string:
$sanitize = pipes('trim', 'htmlspecialchars');
$title = $sanitize($title);
$body = $sanitize($body);
While also preserving the ability to pass a closure when you need multiple arguments.