vsprintf(). Bind the template once, feed different value arrays.
Allows you to create a function which allows for creating a Closure which is populated with a sprintf template. Which accepts the array of args to be used to populate the template. This can either be used as part of a Higher Order Function such as array_map() or as part of a compiled/pipe function.
/**
* @param string $template The sprintf template to use.
* @return Closure(array):string
*/
Strings\vSprintf(string $template): ClosureWhen Strings\vSprintf() is called, it returns the following Closure which can be used like a regular function.
/**
* @param mixed[] $values The values to be used to populate the sprintf template.
* @return string The formatted string
* @psalm-pure
*/
$function(array $values): stringThis can be used to create a simple closure which can be used as a regular function.
// Creates the Closure.
$nameAndAge = Strings\vSprintf('Hello %s you are %d years old.');
// Called as a function.
echo $nameAndAge(['Dave', 12]); // Hello Dave you are 12 years old.
// Used in a higher order function.
$array = array_map( $nameAndAge, [['Dave', 12], ['Jane', 11]]);
print_r($array); // [Hello Dave you are 12 years old., Hello Jane you are 11 years old.]This can be called inline using currying.
echo Strings\vSprintf('%s-H')(['Bar']); // Bar-HIf you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.
$array = array_map(
Strings\vSprintf('Hello %s you are %d years old.'),
[['Dave', 12], ['Jane', 11]]
);
print_r($array); // [Hello Dave you are 12 years old., Hello Jane you are 11 years old.]