ENHANCEMENT: abstract generateURLSegments functionality out to Convert::raw2url() to allow non site tree objects to safely make use of the logic. BUGFIX: #5586 Group::setCode() now calls Convert::raw2url() rather than requiring a SiteTree instance

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@115205 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Will Rossiter 2010-12-20 03:18:51 +00:00
parent 71c52f01df
commit bda08c6988
4 changed files with 49 additions and 12 deletions

View File

@ -195,7 +195,12 @@ class Convert {
}
/**
* @uses recursiveXMLToArray()
* Converts an XML string to a PHP array
*
* @uses {@link recursiveXMLToArray()}
* @param string
*
* @return array
*/
static function xml2array($val) {
$xml = new SimpleXMLElement($val);
@ -203,8 +208,12 @@ class Convert {
}
/**
* Function recursively run from {@link Convert::xml2array()}
* @uses SimpleXMLElement
* Convert a XML string to a PHP array recursively. Do not
* call this function directly, Please use {@link Convert::xml2array()}
*
* @param SimpleXMLElement
*
* @return mixed
*/
protected static function recursiveXMLToArray($xml) {
if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') {
@ -223,11 +232,13 @@ class Convert {
if(isset($a)) $r['@'] = $a; // Attributes
return $r;
}
return (string) $xml;
}
/**
* Create a link if the string is a valid URL
*
* @param string The string to linkify
* @return A link to the URL if string is a URL
*/
@ -335,5 +346,24 @@ class Convert {
$data
);
}
/**
* Convert a string (normally a title) to a string suitable for using in
* urls and other html attributes
*
* @param string
*
* @return string
*/
public static function raw2url($title) {
$t = (function_exists('mb_strtolower')) ? mb_strtolower($title) : strtolower($title);
$t = Object::create('Transliterator')->toASCII($t);
$t = str_replace('&','-and-',$t);
$t = str_replace('&','-and-',$t);
$t = ereg_replace('[^A-Za-z0-9]+','-',$t);
$t = ereg_replace('-+','-',$t);
$t = trim($t, '-');
return $t;
}
}

View File

@ -1521,16 +1521,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* @return string Generated url segment
*/
function generateURLSegment($title){
$t = (function_exists('mb_strtolower')) ? mb_strtolower($title) : strtolower($title);
$t = Object::create('Transliterator')->toASCII($t);
$t = str_replace('&','-and-',$t);
$t = str_replace('&','-and-',$t);
$t = ereg_replace('[^A-Za-z0-9]+','-',$t);
$t = ereg_replace('-+','-',$t);
$t = Convert::raw2url($title);
if(!$t || $t == '-' || $t == '-1') {
$t = "page-$this->ID";
}
$t = trim($t, '-');
// Hook for decorators
$this->extend('updateURLSegment', $t, $title);

View File

@ -313,9 +313,11 @@ class Group extends DataObject {
/**
* Overloaded to ensure the code is always descent.
*
* @param string
*/
public function setCode($val){
$this->setField("Code",SiteTree::generateURLSegment($val));
$this->setField("Code", Convert::raw2url($val));
}
function onBeforeWrite() {

View File

@ -96,5 +96,15 @@ class ConvertTest extends SapphireTest {
$this->assertEquals('Jones', $obj->Tom);
$this->assertEquals('Structure', $obj->My->Complicated);
}
/**
* @todo test toASCII()
*/
function testRaw2URL() {
$this->assertEquals('foo', Convert::raw2url('foo'));
$this->assertEquals('foo-and-bar', Convert::raw2url('foo & bar'));
$this->assertEquals('foo-and-bar', Convert::raw2url('foo & bar!'));
$this->assertEquals('foo-s-bar-2', Convert::raw2url('foo\'s [bar] (2)'));
}
}