From 4b160ee19bbc07163c5919b33b8dab56d02f6f05 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Fri, 19 Mar 2010 17:01:56 +0100 Subject: [PATCH] Implement Iterator, ArrayAccess, Countable interfaces. --- framework/History/lib/Horde/History.php | 42 ++++---- .../Horde/{HistoryObject.php => History/Log.php} | 55 +++++++++- framework/History/lib/Horde/History/Mock.php | 33 +++--- framework/History/lib/Horde/History/Sql.php | 33 +++--- .../History/test/Horde/History/InterfaceTest.php | 115 ++++++++++----------- 5 files changed, 157 insertions(+), 121 deletions(-) rename framework/History/lib/Horde/{HistoryObject.php => History/Log.php} (54%) diff --git a/framework/History/lib/Horde/History.php b/framework/History/lib/Horde/History.php index 04fba81b9..52957b074 100644 --- a/framework/History/lib/Horde/History.php +++ b/framework/History/lib/Horde/History.php @@ -152,27 +152,27 @@ abstract class Horde_History * Logs an event to an item's history log. Any other details about the * event are passed in $attributes. * - * @param Horde_HistoryObject $history The history item to add to. - * @param array $attributes The hash of name => value - * entries that describe this - * event. - * @param boolean $replaceAction If $attributes['action'] is - * already present in the item's - * history log, update that entry - * instead of creating a new one. + * @param Horde_History_Log $history The history item to add to. + * @param array $attributes The hash of name => value + * entries that describe this + * event. + * @param boolean $replaceAction If $attributes['action'] is + * already present in the item's + * history log, update that entry + * instead of creating a new one. * * @throws Horde_History_Exception */ - abstract protected function _log(Horde_HistoryObject $history, + abstract protected function _log(Horde_History_Log $history, array $attributes, $replaceAction = false); /** - * Returns a Horde_HistoryObject corresponding to the named history - * entry, with the data retrieved appropriately. + * Returns a Horde_History_Log corresponding to the named history entry, + * with the data retrieved appropriately. * * @param string $guid The name of the history entry to retrieve. * - * @return Horde_HistoryObject A Horde_HistoryObject + * @return Horde_History_Log A Horde_History_Log object. * * @throws Horde_History_Exception * @throws InvalidArgumentException @@ -186,14 +186,12 @@ abstract class Horde_History } /** - * Returns a Horde_HistoryObject corresponding to the named history - * entry, with the data retrieved appropriately. + * Returns a Horde_History_Log corresponding to the named history entry, + * with the data retrieved appropriately. * - * @param string $guid The name of the history entry to retrieve. - * - * @return Horde_HistoryObject A Horde_HistoryObject + * @param string $guid The name of the history entry to retrieve. * - * @throws Horde_History_Exception + * @return Horde_History_Log A Horde_History_Log object. */ abstract public function _getHistory($guid); @@ -287,11 +285,9 @@ abstract class Horde_History $last = 0; - if (is_array($history->data)) { - foreach ($history->data as $entry) { - if (($entry['action'] == $action) && ($entry['ts'] > $last)) { - $last = $entry['ts']; - } + foreach ($history as $entry) { + if (($entry['action'] == $action) && ($entry['ts'] > $last)) { + $last = $entry['ts']; } } diff --git a/framework/History/lib/Horde/HistoryObject.php b/framework/History/lib/Horde/History/Log.php similarity index 54% rename from framework/History/lib/Horde/HistoryObject.php rename to framework/History/lib/Horde/History/Log.php index dd54ce202..ca5d47afb 100644 --- a/framework/History/lib/Horde/HistoryObject.php +++ b/framework/History/lib/Horde/History/Log.php @@ -8,9 +8,10 @@ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. * * @author Chuck Hagenbuch + * @author Jan Schneider * @package History */ -class Horde_HistoryObject +class Horde_History_Log implements Iterator, ArrayAccess, Countable { /** * TODO @@ -20,7 +21,7 @@ class Horde_HistoryObject /** * TODO */ - public $data = array(); + protected $_data = array(); /** * Constructor. @@ -51,13 +52,57 @@ class Horde_HistoryObject $history = array_merge($history, $extra); } } - $this->data[] = $history; + $this->_data[] = $history; } } - public function getData() + public function current() { - return $this->data; + return current($this->_data); } + public function key() + { + return key($this->_data); + } + + public function next() + { + next($this->_data); + } + + public function rewind() + { + reset($this->_data); + } + + public function valid() + { + return current($this->_data) !== false; + } + + public function offsetExists($offset) + { + return isset($this->_data[$offset]); + } + + public function offsetGet($offset) + { + return $this->_data[$offset]; + } + + public function offsetSet($offset, $value) + { + $this->_data[$offset] = $value; + } + + public function offsetUnset($offset) + { + unset($this->_data[$offset]); + } + + public function count() + { + return count($this->_data); + } } diff --git a/framework/History/lib/Horde/History/Mock.php b/framework/History/lib/Horde/History/Mock.php index d4dacb5b1..6a88dac6f 100644 --- a/framework/History/lib/Horde/History/Mock.php +++ b/framework/History/lib/Horde/History/Mock.php @@ -46,19 +46,18 @@ class Horde_History_Mock extends Horde_History * Logs an event to an item's history log. Any other details about the * event are passed in $attributes. * - * @param Horde_HistoryObject $history The history item to add to. - * @param array $attributes The hash of name => value - * entries that describe this - * event. - * @param boolean $replaceAction If $attributes['action'] is - * already present in the item's - * history log, update that entry - * instead of creating a new one. + * @param Horde_History_Log $history The history item to add to. + * @param array $attributes The hash of name => value + * entries that describe this + * event. + * @param boolean $replaceAction If $attributes['action'] is + * already present in the item's + * history log, update that entry + * instead of creating a new one. * * @throws Horde_History_Exception */ - protected function _log(Horde_HistoryObject $history, - array $attributes, + protected function _log(Horde_History_Log $history, array $attributes, $replaceAction = false) { $values = array( @@ -80,10 +79,10 @@ class Horde_History_Mock extends Horde_History * or not to add the entry later. */ $done = false; if ($replaceAction && !empty($values['history_action'])) { - for ($i = 0, $count = count($history->data); $i < $count; ++$i) { - if (!empty($history->data[$i]['action']) && - $history->data[$i]['action'] == $values['history_action']) { - $this->_data[$history->data[$i]['id']] = $values; + foreach ($history as $entry) { + if (!empty($entry['action']) && + $entry['action'] == $values['history_action']) { + $this->_data[$entry['id']] = $values; $done = true; break; } @@ -100,12 +99,12 @@ class Horde_History_Mock extends Horde_History } /** - * Returns a Horde_HistoryObject corresponding to the named history entry, + * Returns a Horde_History_Log corresponding to the named history entry, * with the data retrieved appropriately. * * @param string $guid The name of the history entry to retrieve. * - * @return Horde_HistoryObject A Horde_HistoryObject + * @return Horde_History_Log A Horde_History_Log object. */ public function _getHistory($guid) { @@ -116,7 +115,7 @@ class Horde_History_Mock extends Horde_History $result[] = $element; } } - return new Horde_HistoryObject($guid, $result); + return new Horde_History_Log($guid, $result); } /** diff --git a/framework/History/lib/Horde/History/Sql.php b/framework/History/lib/Horde/History/Sql.php index 4537973a0..3e102fab1 100644 --- a/framework/History/lib/Horde/History/Sql.php +++ b/framework/History/lib/Horde/History/Sql.php @@ -76,19 +76,18 @@ class Horde_History_Sql extends Horde_History * Logs an event to an item's history log. Any other details about the * event are passed in $attributes. * - * @param Horde_HistoryObject $history The history item to add to. - * @param array $attributes The hash of name => value - * entries that describe this - * event. - * @param boolean $replaceAction If $attributes['action'] is - * already present in the item's - * history log, update that entry - * instead of creating a new one. + * @param Horde_History_Log $history The history item to add to. + * @param array $attributes The hash of name => value + * entries that describe this + * event. + * @param boolean $replaceAction If $attributes['action'] is + * already present in the item's + * history log, update that entry + * instead of creating a new one. * * @throws Horde_History_Exception */ - protected function _log(Horde_HistoryObject $history, - array $attributes, + protected function _log(Horde_History_Log $history, array $attributes, $replaceAction = false) { /* If we want to replace an entry with the same action, try and find @@ -96,9 +95,9 @@ class Horde_History_Sql extends Horde_History * or not to add the entry later. */ $done = false; if ($replaceAction && !empty($attributes['action'])) { - for ($i = 0, $count = count($history->data); $i < $count; ++$i) { - if (!empty($history->data[$i]['action']) && - $history->data[$i]['action'] == $attributes['action']) { + foreach ($history as $entry) { + if (!empty($entry['action']) && + $entry['action'] == $attributes['action']) { $values = array( $attributes['ts'], $attributes['who'], @@ -110,7 +109,7 @@ class Horde_History_Sql extends Horde_History $values[] = $attributes ? serialize($attributes) : null; - $values[] = $history->data[$i]['id']; + $values[] = $entry['id']; $r = $this->_write_db->query( 'UPDATE horde_histories SET history_ts = ?,' . @@ -156,12 +155,12 @@ class Horde_History_Sql extends Horde_History } /** - * Returns a Horde_HistoryObject corresponding to the named history entry, + * Returns a Horde_History_Log corresponding to the named history entry, * with the data retrieved appropriately. * * @param string $guid The name of the history entry to retrieve. * - * @return Horde_HistoryObject A Horde_HistoryObject + * @return Horde_History_Log A Horde_History_Log object. * * @throws Horde_History_Exception */ @@ -169,7 +168,7 @@ class Horde_History_Sql extends Horde_History { $rows = $this->_db->getAll('SELECT * FROM horde_histories WHERE object_uid = ?', array($guid), DB_FETCHMODE_ASSOC); $this->handleError($rows); - return new Horde_HistoryObject($guid, $rows); + return new Horde_History_Log($guid, $rows); } /** diff --git a/framework/History/test/Horde/History/InterfaceTest.php b/framework/History/test/Horde/History/InterfaceTest.php index 7804d79a9..2f356fe41 100644 --- a/framework/History/test/Horde/History/InterfaceTest.php +++ b/framework/History/test/Horde/History/InterfaceTest.php @@ -195,7 +195,7 @@ EOL; $history = $this->getHistory($environment); $history->log('test', array('action' => 'test')); $this->assertTrue($history->getActionTimestamp('test', 'test') > 0); - $data = $history->getHistory('test')->getData(); + $data = $history->getHistory('test'); $this->assertTrue(isset($data[0]['who'])); } } @@ -229,31 +229,31 @@ EOL; $history->log('test', array('who' => 'me', 'ts' => 1000, 'action' => 'test'), false); $history->log('test', array('who' => 'you', 'ts' => 2000, 'action' => 'yours')); $history->log('test', array('who' => 'you', 'ts' => 2000, 'action' => 'yours'), true); - $data = $history->getHistory('test')->getData(); + $data = $history->getHistory('test'); $expect = array( - array( - 'action' => 'test', - 'desc' => '', - 'who' => 'me', - 'id' => 1, - 'ts' => 1000, - ), - array( - 'action' => 'test', - 'desc' => '', - 'who' => 'me', - 'id' => 2, - 'ts' => 1000, - ), - array( - 'action' => 'yours', - 'desc' => '', - 'who' => 'you', - 'id' => 3, - 'ts' => 2000, - ), + 'action' => 'test', + 'desc' => '', + 'who' => 'me', + 'id' => 1, + 'ts' => 1000, ); - $this->assertEquals($expect, $data); + $this->assertEquals($expect, $data[0]); + $expect = array( + 'action' => 'test', + 'desc' => '', + 'who' => 'me', + 'id' => 2, + 'ts' => 1000, + ); + $this->assertEquals($expect, $data[1]); + $expect = array( + 'action' => 'yours', + 'desc' => '', + 'who' => 'you', + 'id' => 3, + 'ts' => 2000, + ); + $this->assertEquals($expect, $data[2]); } } @@ -269,31 +269,30 @@ EOL; } } - public function testMethodGethistoryHasResultHordehistoryobjectRepresentingTheHistoryLogMatchingTheGivenGuid() + public function testMethodGethistoryHasResultHordehistorylogRepresentingTheHistoryLogMatchingTheGivenGuid() { foreach ($this->getEnvironments() as $environment) { $history = $this->getHistory($environment); $history->log('test', array('who' => 'me', 'ts' => 1000, 'action' => 'test')); $history->log('test', array('who' => 'you', 'ts' => 2000, 'action' => 'yours', 'extra' => array('a' => 'a'))); - $data = $history->getHistory('test')->getData(); + $data = $history->getHistory('test'); + $expect = array( + 'action' => 'test', + 'desc' => '', + 'who' => 'me', + 'id' => 1, + 'ts' => 1000, + ); + $this->assertEquals($expect, $data[0]); $expect = array( - array( - 'action' => 'test', - 'desc' => '', - 'who' => 'me', - 'id' => 1, - 'ts' => 1000, - ), - array( - 'action' => 'yours', - 'desc' => '', - 'who' => 'you', - 'id' => 2, - 'ts' => 2000, - 'extra' => array('a' => 'a'), - ), + 'action' => 'yours', + 'desc' => '', + 'who' => 'you', + 'id' => 2, + 'ts' => 2000, + 'extra' => array('a' => 'a'), ); - $this->assertEquals($expect, $data); + $this->assertEquals($expect, $data[1]); } } @@ -431,22 +430,20 @@ EOL; $history->log('yours', array('who' => 'you', 'ts' => 2000, 'action' => 'yours')); $history->log('yours', array('who' => 'you', 'ts' => 2000, 'action' => 'yours'), true); $history->removeByNames(array('test')); - $data = $history->getHistory('test')->getData(); - $this->assertEquals(array(), $data); - $data = $history->getHistory('yours')->getData(); + $data = $history->getHistory('test'); + $this->assertEquals(0, count($data)); + $data = $history->getHistory('yours'); $expect = array( - array( - 'action' => 'yours', - 'desc' => '', - 'who' => 'you', - 'id' => 3, - 'ts' => 2000, - ), + 'action' => 'yours', + 'desc' => '', + 'who' => 'you', + 'id' => 3, + 'ts' => 2000, ); - $this->assertEquals($expect, $data); + $this->assertEquals($expect, $data[0]); $history->removeByNames(array('yours')); - $data = $history->getHistory('yours')->getData(); - $this->assertEquals(array(), $data); + $data = $history->getHistory('yours'); + $this->assertEquals(0, count($data)); } } @@ -459,10 +456,10 @@ EOL; $history->log('yours', array('who' => 'you', 'ts' => 2000, 'action' => 'yours')); $history->log('yours', array('who' => 'you', 'ts' => 2000, 'action' => 'yours'), true); $history->removeByNames(array('test', 'yours')); - $data = $history->getHistory('test')->getData(); - $this->assertEquals(array(), $data); - $data = $history->getHistory('yours')->getData(); - $this->assertEquals(array(), $data); + $data = $history->getHistory('test'); + $this->assertEquals(0, count($data)); + $data = $history->getHistory('yours'); + $this->assertEquals(0, count($data)); } } -- 2.11.0