<?php declare(strict_types=1);
class testAPI
{
private array $employee;
private array $template;
/**
* testAPI constructor.
* @param array $employee – массив с данными о работнике
* @param array $template — массив с шаблонами
*/
public function __construct(array $employee, array $template)
{
$this->employee = $employee;
$this->template = $template;
}
public function getApiPath(): array
{
return preg_replace_callback(
'#%(.*)%#isU',
function (array $matches) {
return $this->employee[$matches[1]];
},
$this->template
);
}
}
$apiTemplatesSet1 = [
'/api/items/%id%/%name%',
'/api/items/%id%/%role%',
'/api/items/%id%/%salary%'
];
$user = [
'id' => 20,
'name' => 'John Dow',
'role' => 'QA',
'salary' => 100
];
$test = new testAPI($user, $apiTemplatesSet1);
echo '<pre>' . print_r($test->getApiPath(), true) . '</pre>';
preferences:
51.91 ms | 402 KiB | 5 Q