Hi All,
I've just finished the first draft of a method that validates the passed URI using the $_GET array.
Hopefully the intro in the comment will explain what it is I'm trying to achieve.
I'm sure this can be tightened up - ALL constructive critique, advice, ideas, notes etc.. welcome.
Some thoughts after a quick review from me:
Have I missed any logic? (probably!)
Too many returns? How do I reduce this, if it's possible?
Is there a way to centralise this $arrCurrentContent for easy updates?
/**
*
*
* @author nawrik
*
* Uses $_GET array to process the current URI.
*
* $_GET format:
* /?$key=category-name/.
* $key must be an integer.
* An unlimited amount of '$key=categories' can be set.
* Each URI must end with 'content=article-name'.
*
* An Example URI:
* http://www.yoursite.com/?1=company-name&2=personnel&3=bio&content=joe-bloggs
*
* 'content' must exist as a 'content-slug' within the content table.
* ALL categories must exist as comma separated string in the 'parents' field of the content table.
* The 'parents' string for the above example would look like this: 'company-name,personnel,bio'
*
* returns array $arrCurrentContent
*
*
*/
public function getURI($arrURI)
{
// Load Home Page
if(is_array($arrURI) && empty($arrURI))
{
$arrCurrentContent = array(
'http_response' => '200',
'content_id' => '',
'content_slug' => '',
'full_path' => '',
'category_path' => '',
'home' => 'N',
'message' => ''
);
return $arrCurrentContent;
}
// If there are any empty strings passed - return 404
foreach($arrURI as $strKey => $strValue)
{
if(empty($strValue))
{
unset($arrURI[$strKey]);
$arrCurrentContent = array(
'http_response' => '404',
'content_id' => '',
'content_slug' => '',
'full_path' => '',
'category_path' => '',
'home' => 'N',
'message' => '$_GET value '.$strKey.' is empty'
);
return $arrCurrentContent;
}
}
// If the content parameter doesn't exist - return 404
if(!array_key_exists('content', $arrURI))
{
$arrCurrentContent = array(
'http_response' => '404',
'content_id' => '',
'content_slug' => '',
'full_path' => '',
'category_path' => '',
'home' => 'N',
'message' => 'The content slug is not set.'
);
return $arrCurrentContent;
}
// Check to see if the content parameter passed exists as a content-slug in the database.
// If there is a match return the key of the content-slug then create an array of that content 'parents' - if not return 404
$arrAllContentSlugs = $this->getData('getAllContentSlugs','core');
$iContentId = array_search($arrURI['content'], $arrAllContentSlugs);
if($iContentId == FALSE && !is_int($iContentId))
{
$arrCurrentContent = array(
'http_response' => '404',
'content_id' => '',
'content_slug' => '',
'full_path' => '',
'category_path' => '',
'home' => 'N',
'message' => 'The content slug does not exist in the DB - '
);
return $arrCurrentContent;
}
else
{
$strContentParents = $this->getData('getContentParents','core',$iContentId);
$arrContentParents = explode(',',$strContentParents);
}
// The content parameter has now been validated, set for good and clean up
$strContentName = $arrURI['content'];
unset($arrURI['content']);
// Reset the arrURI keys allowing for an accurate array_diff_assoc
$arrURIPath = array_values($arrURI);
// Compare the URI 'path' to the content parents retrieved from the database
// If $arrCurrentPath is populated there is a difference - return 404
// If not the current URI has been validated and return 200
$arrCurrentPath = array_diff_assoc($arrURIPath, $arrContentParents);
if(!empty($arrCurrentPath))
{
$arrCurrentContent = array(
'http_response' => '404',
'content_id' => $iContentId,
'content_slug' => $strContentName,
'full_path' => '',
'category_path' => '',
'home' => 'N',
'message' => 'This category path does not exist.'
);
return $arrCurrentContent;
}
else
{
$strCurrentPath = implode('/',$arrContentParents).'/';
$arrCurrentContent = array(
'http_response' => '200',
'content_id' => $iContentId,
'content_slug' => $strContentName,
'full_path' => $strCurrentPath.$strContentName.'/',
'category_path' => $strCurrentPath,
'home' => 'N',
'message' => ''
);
return $arrCurrentContent;
}
} // getURI