Implement Iterator, ArrayAccess, Countable interfaces.
authorJan Schneider <jan@horde.org>
Fri, 19 Mar 2010 16:01:56 +0000 (17:01 +0100)
committerJan Schneider <jan@horde.org>
Fri, 19 Mar 2010 17:06:32 +0000 (18:06 +0100)
framework/History/lib/Horde/History.php
framework/History/lib/Horde/History/Log.php [new file with mode: 0644]
framework/History/lib/Horde/History/Mock.php
framework/History/lib/Horde/History/Sql.php
framework/History/lib/Horde/HistoryObject.php [deleted file]
framework/History/test/Horde/History/InterfaceTest.php

index 04fba81..52957b0 100644 (file)
@@ -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/History/Log.php b/framework/History/lib/Horde/History/Log.php
new file mode 100644 (file)
index 0000000..ca5d47a
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+/**
+ * Class for presenting Horde_History information.
+ *
+ * Copyright 2003-2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author  Chuck Hagenbuch <chuck@horde.org>
+ * @author  Jan Schneider <jan@horde.org>
+ * @package History
+ */
+class Horde_History_Log implements Iterator, ArrayAccess, Countable
+{
+    /**
+     * TODO
+     */
+    public $uid;
+
+    /**
+     * TODO
+     */
+    protected $_data = array();
+
+    /**
+     * Constructor.
+     *
+     * TODO
+     */
+    public function __construct($uid, $data = array())
+    {
+        $this->uid = $uid;
+
+        if (!$data) {
+            return;
+        }
+
+        reset($data);
+        while (list(,$row) = each($data)) {
+            $history = array(
+                'action' => $row['history_action'],
+                'desc' => $row['history_desc'],
+                'who' => $row['history_who'],
+                'id' => $row['history_id'],
+                'ts' => $row['history_ts']
+            );
+
+            if ($row['history_extra']) {
+                $extra = @unserialize($row['history_extra']);
+                if ($extra) {
+                    $history = array_merge($history, $extra);
+                }
+            }
+            $this->_data[] = $history;
+        }
+    }
+
+    public function current()
+    {
+        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);
+    }
+}
index d4dacb5..6a88dac 100644 (file)
@@ -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);
     }
 
     /**
index 4537973..3e102fa 100644 (file)
@@ -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/lib/Horde/HistoryObject.php b/framework/History/lib/Horde/HistoryObject.php
deleted file mode 100644 (file)
index dd54ce2..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-<?php
-/**
- * Class for presenting Horde_History information.
- *
- * Copyright 2003-2010 The Horde Project (http://www.horde.org/)
- *
- * See the enclosed file COPYING for license information (LGPL). If you
- * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
- *
- * @author  Chuck Hagenbuch <chuck@horde.org>
- * @package History
- */
-class Horde_HistoryObject
-{
-    /**
-     * TODO
-     */
-    public $uid;
-
-    /**
-     * TODO
-     */
-    public $data = array();
-
-    /**
-     * Constructor.
-     *
-     * TODO
-     */
-    public function __construct($uid, $data = array())
-    {
-        $this->uid = $uid;
-
-        if (!$data) {
-            return;
-        }
-
-        reset($data);
-        while (list(,$row) = each($data)) {
-            $history = array(
-                'action' => $row['history_action'],
-                'desc' => $row['history_desc'],
-                'who' => $row['history_who'],
-                'id' => $row['history_id'],
-                'ts' => $row['history_ts']
-            );
-
-            if ($row['history_extra']) {
-                $extra = @unserialize($row['history_extra']);
-                if ($extra) {
-                    $history = array_merge($history, $extra);
-                }
-            }
-            $this->data[] = $history;
-        }
-    }
-
-    public function getData()
-    {
-        return $this->data;
-    }
-
-}
index 7804d79..2f356fe 100644 (file)
@@ -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));
         }
     }