mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
EHANCHMENT: update the regular expression to disregard any underscores directly before a capital letter, as well as using the PCRE functions and adding a small test. PATCH: #4081
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@77612 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
0923b5825e
commit
cf35533a78
@ -392,10 +392,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
* @return string User friendly singular name of this DataObject
|
* @return string User friendly singular name of this DataObject
|
||||||
*/
|
*/
|
||||||
function singular_name() {
|
function singular_name() {
|
||||||
$name = $this->stat('singular_name');
|
if(!$name = $this->stat('singular_name')) {
|
||||||
if(!$name) {
|
$name = ucwords(trim(strtolower(preg_replace('/_?([A-Z])/', ' $1', $this->class))));
|
||||||
$name = ucwords(trim(strtolower(ereg_replace('([A-Z])',' \\1',$this->class))));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $name;
|
return $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -642,6 +642,25 @@ class DataObjectTest extends SapphireTest {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that singular_name() generates sensible defaults.
|
||||||
|
*/
|
||||||
|
public function testSingularName() {
|
||||||
|
$assertions = array (
|
||||||
|
'DataObjectTest_Player' => 'Data Object Test Player',
|
||||||
|
'DataObjectTest_Team' => 'Data Object Test Team',
|
||||||
|
'DataObjectTest_WithDefaults' => 'Data Object Test With Defaults'
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($assertions as $class => $expectedSingularName) {
|
||||||
|
$this->assertEquals (
|
||||||
|
$expectedSingularName,
|
||||||
|
singleton($class)->singular_name(),
|
||||||
|
"Assert that the singular_name for '$class' is correct."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class DataObjectTest_Player extends Member implements TestOnly {
|
class DataObjectTest_Player extends Member implements TestOnly {
|
||||||
|
Loading…
Reference in New Issue
Block a user