protected function extractAttributes($url = false, $locale = '')
{
if (!empty($url)) {
$attributes = [];
$parse = parse_url($url);
if (isset($parse['path'])) {
$parse = explode("/", $parse['path']);
} else {
$parse = [];
}
$url = [];
foreach ($parse as $segment) {
if (!empty($segment)) {
$url[] = $segment;
}
}
foreach ($this->router->getRoutes() as $route) {
$path = $route->getUri();
if (!preg_match("/{[\\w]+}/", $path)) {
continue;
}
$path = explode("/", $path);
$i = 0;
$match = true;
foreach ($path as $j => $segment) {
if (isset($url[$i])) {
if ($segment === $url[$i]) {
$i++;
continue;
}
if (preg_match("/{[\\w]+}/", $segment)) {
// must-have parameters
$attribute_name = preg_replace(["/}/", "/{/", "/\\?/"], "", $segment);
$attributes[$attribute_name] = $url[$i];
$i++;
continue;
}
if (preg_match("/{[\\w]+\\?}/", $segment)) {
// optional parameters
if (!isset($path[$j + 1]) || $path[$j + 1] !== $url[$i]) {
// optional parameter taken
$attribute_name = preg_replace(["/}/", "/{/", "/\\?/"], "", $segment);
$attributes[$attribute_name] = $url[$i];
$i++;
continue;
}
}
} else {
if (!preg_match("/{[\\w]+\\?}/", $segment)) {
// no optional parameters but no more $url given
// this route does not match the url
$match = false;
break;
}
}
}
if (isset($url[$i + 1])) {
$match = false;
}
if ($match) {
return $attributes;
}
}
} else {
if (!$this->router->current()) {
return [];
}
$attributes = $this->router->current()->parameters();
$response = event('routes.translation', [$locale, $attributes]);
if (!empty($response)) {
$response = array_shift($response);
}
if (is_array($response)) {
$attributes = array_merge($attributes, $response);
}
}
return $attributes;
}