$user = [
'id' => 1,
'name' => 'Ivan Ivanov',
'role' => 'developer',
'salary' => 100
];
$apiTemplatesSet1 = [
'/api/items/%id%/%name%',
'/api/items/%id%/%role%',
'/api/items/%id%/%salary%'
];
class Invoker
{
private $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function __invoke(array $key): string
{
return $this->data[$key[1]];
}
}
$function = new Invoker($user);
$result = array_map(function ($key) use ($function)
{
return preg_replace_callback('#%(.*)%#isU', $function, $key);
}
, array_values($apiTemplatesSet1));
echo '<pre>' . print_r($result, true);
https://3v4l.org/DbIAi