<?php
namespace Test\XF\Mvc;
use XF\Mvc\RouteBuiltLink;
class Router extends XFCP_Router
{
public function generateMatchRegexInner($format, $wrapper = '#')
{
$matchRegex = parent::generateMatchRegexInner($format, $wrapper);
$matchRegex = preg_replace_callback(
'#:(\+)?test_str_extended(?:_p)?<([a-zA-Z0-9_.]+)>/?#',
function ($match) {
$mainMatch = '(?:(?P<' . $match[2] . '>[a-zA-Z0-9_-]+)/)';
return $match[1] ? $mainMatch : "{$mainMatch}?";
},
$matchRegex
);
return $matchRegex;
}
protected function buildRouteUrl($prefix, array $route, $action, $data = null, array &$parameters = [])
{
$url = parent::buildRouteUrl($prefix, $route, $action, $data, $parameters);
if (!empty($route['build_callback']) && is_string($url) || $url instanceof RouteBuiltLink) {
return $url;
}
$url = preg_replace_callback(
'#:(?:\+)?test_str_extended(_p)?<([a-zA-Z0-9_.]+)>(/?)#',
function($match) use ($data, &$parameters) {
$inParams = !empty($match[1]);
$stringKey = $match[2];
$trailingSlash = $match[3];
$search = $inParams ? $parameters : $data;
if ($search && isset($search[$stringKey])) {
$key = strval($search[$stringKey]);
if ($inParams) {
unset($parameters[$stringKey]);
}
if (strlen($key)) {
return $key . $trailingSlash;
}
}
return '';
},
$url
);
return $url;
}
}