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:
Will Rossiter 2009-05-22 03:49:15 +00:00
parent 0923b5825e
commit cf35533a78
2 changed files with 23 additions and 4 deletions

View File

@ -392,10 +392,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* @return string User friendly singular name of this DataObject
*/
function singular_name() {
$name = $this->stat('singular_name');
if(!$name) {
$name = ucwords(trim(strtolower(ereg_replace('([A-Z])',' \\1',$this->class))));
if(!$name = $this->stat('singular_name')) {
$name = ucwords(trim(strtolower(preg_replace('/_?([A-Z])/', ' $1', $this->class))));
}
return $name;
}

View File

@ -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 {