__get()
, что бы получить доступ к приватному свойству, т.е. к датам),а в классе Interval скорректировал метод по подсчету разницу в днях:public function toDays(): string
{
$startDate = strtotime($this->startDate->date);
$endDate = strtotime($this->endDate->date);
$diff = ($endDate - $startDate)/(60*60*24);
return 'Разница (в днях) между датами = ' . abs($diff) . '.';
}
array (size=3)
0 =>
array (size=3)
0 => string '/api/items/20/John Dow' (length=22)
1 => string '/api/items/20/QA' (length=16)
2 => string '/api/items/20/100' (length=17)
1 =>
array (size=3)
0 => string '/api/items/20/John Dow' (length=22)
1 => string '/api/items/20/QA' (length=16)
2 => string '/api/items/20/100' (length=17)
2 =>
array (size=3)
0 => string '/api/items/20/John Dow' (length=22)
1 => string '/api/items/20/QA' (length=16)
2 => string '/api/items/20/100' (length=17)
public function getApiPath(): array
{
return array_map(function () {
return preg_replace_callback(
'#%(.*)%#isU',
function ($matches) {
return $this->employee[$matches[1]];
},
$this->template);
},
array_values($this->template));
}
<?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 array_map(function () {
$regex = '#%(.*)%#isU';
return preg_replace_callback($regex, $this->prepareTemplate(), $this->template);
},
array_values($this->template));
}
private function prepareTemplate(): array
{
$result = [];
foreach ($this->employee as $key => $value) {
$tmp = str_replace("%$key%", $value, $this->template);
$result[] = $tmp;
}
var_dump($result);
exit;
}
}
$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);
var_dump($test->getApiPath());
Результат:
array (size=4)
0 =>
array (size=3)
0 => string '/api/items/20/%name%' (length=20)
1 => string '/api/items/20/%role%' (length=20)
2 => string '/api/items/20/%salary%' (length=22)
1 =>
array (size=3)
0 => string '/api/items/%id%/John Dow' (length=24)
1 => string '/api/items/%id%/%role%' (length=22)
2 => string '/api/items/%id%/%salary%' (length=24)
2 =>
array (size=3)
0 => string '/api/items/%id%/%name%' (length=22)
1 => string '/api/items/%id%/QA' (length=18)
2 => string '/api/items/%id%/%salary%' (length=24)
3 =>
array (size=3)
0 => string '/api/items/%id%/%name%' (length=22)
1 => string '/api/items/%id%/%role%' (length=22)
2 => string '/api/items/%id%/100' (length=19)
<?php declare(strict_types=1);
class testAPI
{
private array $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function getApiPath(array $template): array
{
return array_map(function (array $template) {
$regex = '#%(.*)%#isU';
return preg_replace_callback($regex, $this->prepareTemplate(), $template);
},
array_values($template));
}
private function prepareTemplate()
{
// callback который будет производить замену
}
}
$apiTemplatesSet1 = [
'/api/items/%id%/%name%',
'/api/items/%id%/%role%',
'/api/items/%id%/%salary%'
];
$test = new testAPI([
'id' => 20,
'name' => 'John Dow',
'role' => 'QA',
'salary' => 100
]);
Ваша регулярка получше, тут даже str_replace срезать с конца ничего не надо (в отличие от первого варианта). Спасибо!