mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
API CHANGE: Refactored ClassInfo::baseDataClass() to use inbuilt PHP methods, and throw an exception if the passed class is not a subclass of DataObject.
This commit is contained in:
parent
d187718a3f
commit
0d03348926
@ -90,21 +90,26 @@ class ClassInfo {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the root data class for that class.
|
||||
* This root table has a lot of special use in the DataObject system.
|
||||
* Returns the root class (the first to extend from DataObject) for the
|
||||
* passed class.
|
||||
*
|
||||
* @param mixed $class string of the classname or instance of the class
|
||||
* @return array
|
||||
* @param string|object $class
|
||||
* @return string
|
||||
*/
|
||||
static function baseDataClass($class) {
|
||||
global $_ALL_CLASSES;
|
||||
public static function baseDataClass($class) {
|
||||
if (is_object($class)) $class = get_class($class);
|
||||
reset($_ALL_CLASSES['parents'][$class]);
|
||||
while($val = next($_ALL_CLASSES['parents'][$class])) {
|
||||
if($val == 'DataObject') break;
|
||||
|
||||
if (!self::is_subclass_of($class, 'DataObject')) {
|
||||
throw new Exception("$class is not a subclass of DataObject");
|
||||
}
|
||||
|
||||
while ($next = get_parent_class($class)) {
|
||||
if ($next == 'DataObject') {
|
||||
return $class;
|
||||
}
|
||||
|
||||
$class = $next;
|
||||
}
|
||||
$baseDataClass = next($_ALL_CLASSES['parents'][$class]);
|
||||
return $baseDataClass ? $baseDataClass : $class;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,9 +34,21 @@ class ClassInfoTest extends SapphireTest {
|
||||
// );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ClassInfo::baseDataClass()
|
||||
*/
|
||||
public function testBaseDataClass() {
|
||||
$this->assertEquals('ClassInfoTest_BaseClass', ClassInfo::baseDataClass('ClassInfoTest_BaseClass'));
|
||||
$this->assertEquals('ClassInfoTest_BaseClass', ClassInfo::baseDataClass('ClassInfoTest_ChildClass'));
|
||||
$this->assertEquals('ClassInfoTest_BaseClass', ClassInfo::baseDataClass('ClassInfoTest_GrandChildClass'));
|
||||
|
||||
$this->setExpectedException('Exception');
|
||||
ClassInfo::baseDataClass('DataObject');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ClassInfoTest_BaseClass {
|
||||
class ClassInfoTest_BaseClass extends DataObject {
|
||||
|
||||
}
|
||||
|
||||
@ -47,4 +59,3 @@ class ClassInfoTest_ChildClass extends ClassInfoTest_BaseClass {
|
||||
class ClassInfoTest_GrandChildClass extends ClassInfoTest_ChildClass {
|
||||
|
||||
}
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user