/**
* Parse the URL
*
* @param array &$segments The URL segments to parse
* @param array &$vars The vars that result from the segments
*
* @return void
*
* @since 3.4
*/
public function parse(&$segments, &$vars)
{
// Get the views and the currently active query vars
$views = $this->router->getViews();
$active = $this->router->menu->getActive();
if ($active) {
$vars = array_merge($active->query, $vars);
}
// We don't have a view or its not a view of this component! We stop here
if (!isset($vars['view']) || !isset($views[$vars['view']])) {
return;
}
// Copy the segments, so that we can iterate over all of them and at the same time modify the original segments
$tempSegments = $segments;
// Iterate over the segments as long as a segment fits
foreach ($tempSegments as $segment) {
// Our current view is nestable. We need to check first if the segment fits to that
if ($views[$vars['view']]->nestable) {
if (\is_callable(array($this->router, 'get' . ucfirst($views[$vars['view']]->name) . 'Id'))) {
$key = \call_user_func_array(array($this->router, 'get' . ucfirst($views[$vars['view']]->name) . 'Id'), array($segment, $vars));
// Did we get a proper key? If not, we need to look in the child-views
if ($key) {
$vars[$views[$vars['view']]->key] = $key;
array_shift($segments);
continue;
}
} else {
// The router is not complete. The get<View>Id() method is missing.
return;
}
}
// Lets find the right view that belongs to this segment
$found = false;
foreach ($views[$vars['view']]->children as $view) {
if (!$view->key) {
if ($view->name === $segment) {
// The segment is a view name
$parent = $views[$vars['view']];
$vars['view'] = $view->name;
$found = true;
if ($view->parent_key && isset($vars[$parent->key])) {
$parent_key = $vars[$parent->key];
$vars[$view->parent_key] = $parent_key;
unset($vars[$parent->key]);
}
break;
}
} elseif (\is_callable(array($this->router, 'get' . ucfirst($view->name) . 'Id'))) {
// Hand the data over to the router specific method and see if there is a content item that fits
$key = \call_user_func_array(array($this->router, 'get' . ucfirst($view->name) . 'Id'), array($segment, $vars));
if ($key) {
// Found the right view and the right item
$parent = $views[$vars['view']];
$vars['view'] = $view->name;
$found = true;
if ($view->parent_key && isset($vars[$parent->key])) {
$parent_key = $vars[$parent->key];
$vars[$view->parent_key] = $parent_key;
unset($vars[$parent->key]);
}
$vars[$view->key] = $key;
break;
}
}
}
if (!$found) {
return;
}
array_shift($segments);
}
}