Skip to content

Commit

Permalink
Added a default descriptor namer which names the descriptor to this r…
Browse files Browse the repository at this point in the history
…egex capture of the descriptor class name (if matching): /^([A-Za-z0-9]+)Descriptor$/i
  • Loading branch information
m4rw3r committed Dec 21, 2009
1 parent d425922 commit b0330ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/Db/Descriptor.php
Expand Up @@ -143,7 +143,16 @@ public function getClass()
{
if(empty($this->class))
{
throw new Db_Descriptor_MissingValueException('class name', '');
$c = get_class($this);

if($c != 'Db_Descriptor' && preg_match('/^([A-Za-z0-9]+)Descriptor$/i', $c, $r))
{
$this->setClass($r[1]);
}
else
{
throw new Db_Descriptor_MissingValueException('class name', '');
}
}

return $this->class;
Expand Down
18 changes: 18 additions & 0 deletions tests/Db/DescriptorTest.php
Expand Up @@ -193,6 +193,24 @@ public function testSetClass()
$this->assertEquals($desc->getFactory(), 'new Test');
}

// ------------------------------------------------------------------------

public function testAutoSetClass()
{
if( ! class_exists('DummyTestAutoSetClassDescriptor'))
{
eval('class DummyTestAutoSetClassDescriptor extends Db_Descriptor {}');
}

$desc = new DummyTestAutoSetClassDescriptor();

$this->assertEquals('DummyTestAutoSetClass', $desc->getClass());
$this->assertEquals('dummytestautosetclass', $desc->getSingular());

$desc->setClass('Test');
$this->assertEquals('test', $desc->getSingular());
}

// ------------------------------------------------------------------------

/**
Expand Down

0 comments on commit b0330ed

Please sign in to comment.