improve Linker

This commit is contained in:
coderkun 2014-08-09 00:37:25 +02:00
commit fa8b0cadcd

View file

@ -43,72 +43,61 @@
/** /**
* Process a title and optional a date to create link parameters. * Mask parameters to be used in an URL.
* *
* @param string $title Titel * @param string $param1 First parameter
* @param string $date Date * @return string Masked parameters as string
* @return string Created link parameters
*/ */
public static function getLinkParams($title, $date=null) public static function createLinkParam($param1)
{ {
// Parameters return implode(
$param = ''; \nre\configs\CoreConfig::$classes['linker']['url']['delimiter'],
call_user_func_array(
// Mask special sign seperately '\nre\core\Linker::createLinkParams',
$specials = array('/', '?', '&'); func_get_args()
foreach($specials as &$special) { )
$title = str_replace($special, rawurlencode(rawurlencode($special)), $title); );
} }
// Process title
$param .= str_replace( /**
* Mask parameters to be used in an URL.
*
* @param string $param1 First parameter
* @return string Masked parameters as array
*/
public static function createLinkParams($param1)
{
// Parameters
$linkParams = array();
$params = func_get_args();
foreach($params as $param)
{
// Delete critical signs
$specials = array('/', '?', '&', '#');
foreach($specials as &$special) {
$param = str_replace($special, '', $param);
}
// Process parameter
$param = str_replace(
' ', ' ',
'-', \nre\configs\CoreConfig::$classes['linker']['url']['delimiter'],
substr( substr(
$title, $param,
0, 0,
\nre\configs\CoreConfig::$classes['linker']['url']['length'] \nre\configs\CoreConfig::$classes['linker']['url']['length']
) )
); );
// Process date // Encode parameter
if(!empty($date)) { $linkParams[] = $param;
$param = substr($date, 0, 10).\nre\configs\CoreConfig::$classes['linker']['url']['delimiter'].$param;
} }
// Mask and return parameters // Return link parameters
return array(rawurlencode($param)); return $linkParams;
}
/**
* Extract date and title from a parameter string.
*
* @param string $dateTitle Parameter string with date and title
* @return array Extracted date and title as associative array
*/
public static function extractDateTitle($dateTitle)
{
// Get delimiter
$delimiter = \nre\configs\CoreConfig::$classes[strtolower(get_class())]['url']['delimiter'];
// Split
$dateTitle = explode($delimiter, $dateTitle);
if(count($dateTitle) < 4) {
throw new IdNotFoundException(implode($delimiter, $dateTitle));
}
// Get parts
$date = urldecode(implode($delimiter, array_slice($dateTitle, 0, 3)));
$title = urldecode(implode($delimiter, array_slice($dateTitle, 3)));
// Return date and title
return array(
'date' => $date,
'title' => $title
);
} }
@ -148,7 +137,6 @@
if(count($reqParams) < $offset && $offset > 1) { if(count($reqParams) < $offset && $offset > 1) {
$reqParams[] = $this->request->getParam(2, 'action'); $reqParams[] = $this->request->getParam(2, 'action');
} }
$params = array_map('rawurlencode', $params);
$params = array_merge($reqParams, $params); $params = array_merge($reqParams, $params);
// Use Layout // Use Layout
@ -170,6 +158,9 @@
); );
} }
// Encode parameters
$params = array_map('rawurlencode', $params);
// Set parameters // Set parameters
call_user_func_array( call_user_func_array(
array( array(