mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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:
parent
71c52f01df
commit
bda08c6988
@ -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) {
|
static function xml2array($val) {
|
||||||
$xml = new SimpleXMLElement($val);
|
$xml = new SimpleXMLElement($val);
|
||||||
@ -203,8 +208,12 @@ class Convert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function recursively run from {@link Convert::xml2array()}
|
* Convert a XML string to a PHP array recursively. Do not
|
||||||
* @uses SimpleXMLElement
|
* call this function directly, Please use {@link Convert::xml2array()}
|
||||||
|
*
|
||||||
|
* @param SimpleXMLElement
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected static function recursiveXMLToArray($xml) {
|
protected static function recursiveXMLToArray($xml) {
|
||||||
if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') {
|
if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') {
|
||||||
@ -223,11 +232,13 @@ class Convert {
|
|||||||
if(isset($a)) $r['@'] = $a; // Attributes
|
if(isset($a)) $r['@'] = $a; // Attributes
|
||||||
return $r;
|
return $r;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (string) $xml;
|
return (string) $xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a link if the string is a valid URL
|
* Create a link if the string is a valid URL
|
||||||
|
*
|
||||||
* @param string The string to linkify
|
* @param string The string to linkify
|
||||||
* @return A link to the URL if string is a URL
|
* @return A link to the URL if string is a URL
|
||||||
*/
|
*/
|
||||||
@ -336,4 +347,23 @@ class Convert {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1521,16 +1521,11 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
|||||||
* @return string Generated url segment
|
* @return string Generated url segment
|
||||||
*/
|
*/
|
||||||
function generateURLSegment($title){
|
function generateURLSegment($title){
|
||||||
$t = (function_exists('mb_strtolower')) ? mb_strtolower($title) : strtolower($title);
|
$t = Convert::raw2url($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);
|
|
||||||
if(!$t || $t == '-' || $t == '-1') {
|
if(!$t || $t == '-' || $t == '-1') {
|
||||||
$t = "page-$this->ID";
|
$t = "page-$this->ID";
|
||||||
}
|
}
|
||||||
$t = trim($t, '-');
|
|
||||||
|
|
||||||
// Hook for decorators
|
// Hook for decorators
|
||||||
$this->extend('updateURLSegment', $t, $title);
|
$this->extend('updateURLSegment', $t, $title);
|
||||||
|
@ -313,9 +313,11 @@ class Group extends DataObject {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Overloaded to ensure the code is always descent.
|
* Overloaded to ensure the code is always descent.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
*/
|
*/
|
||||||
public function setCode($val){
|
public function setCode($val){
|
||||||
$this->setField("Code",SiteTree::generateURLSegment($val));
|
$this->setField("Code", Convert::raw2url($val));
|
||||||
}
|
}
|
||||||
|
|
||||||
function onBeforeWrite() {
|
function onBeforeWrite() {
|
||||||
|
@ -97,4 +97,14 @@ class ConvertTest extends SapphireTest {
|
|||||||
$this->assertEquals('Structure', $obj->My->Complicated);
|
$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)'));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user