Add ability to create Thread object from DB info

This commit is contained in:
Dmitriy Simushev 2012-10-01 09:59:32 +00:00
parent 8f87a017a1
commit 69477cea8d
2 changed files with 73 additions and 0 deletions

View File

@ -186,6 +186,54 @@ class ThreadTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(0, $this->_helper_getThreadCount($threadid)); $this->assertEquals(0, $this->_helper_getThreadCount($threadid));
} }
public function testCreateFromDbInfo() {
// Check incomplete fields list
$fields_list = array(
'threadid' => 1
);
$thread = Thread::createFromDbInfo($fields_list);
$this->assertFalse($thread);
// Check complete fields list
$fields_list = array(
'threadid' => 10,
'lrevision' => 189,
'istate' => Thread::STATE_QUEUE,
'ltoken' => 19908,
'nextagent' => 0,
'groupid' => 0,
'shownmessageid' => 0,
'messageCount' => 0,
'dtmcreated' => time() - 100,
'dtmmodified' => time() - 90,
'dtmchatstarted' => 0,
'agentId' => 0,
'agentName' => '',
'agentTyping' => 0,
'lastpingagent' => 0,
'locale' => 'en',
'userid' => 1112,
'userName' => 'Guest',
'userTyping' => 0,
'lastpinguser' => time() - 10,
'remote' => '127.0.0.1',
'referer' => 'http://google.com',
'userAgent' => 'Mozilla FireFox'
);
$thread = Thread::createFromDbInfo($fields_list);
$this->assertInstanceOf('Thread', $thread);
unset($thread);
}
public function test__isset() { public function test__isset() {
// Create new thread // Create new thread
$thread = Thread::create(); $thread = Thread::create();

View File

@ -217,6 +217,31 @@ Class Thread {
return $thread; return $thread;
} }
/**
* Create thread object from database info.
*
* @param array $thread_info Associative array of Thread info from database. It must contains ALL thread table's
* FIELDS from the database.
* @return boolean|Thread Returns an object of the Thread class or boolean false on failure
*/
public static function createFromDbInfo($thread_info) {
// Create new empty thread
$thread = new self();
// Check thread fields
$obligatory_fields = array_values($thread->propertyMap);
foreach($obligatory_fields as $field) {
if (!array_key_exists($field, $thread_info)) {
// Obligatory field is missing
unset($thread);
return false;
}
// Copy field to Thread object
$thread->threadInfo[$field] = $thread_info[$field];
}
return $thread;
}
/** /**
* Load thread from database * Load thread from database
* *