Explorar o código

ZF-7021 Advancments in the ArrayCollection class to take advantagge if the SPL ArrayObject and ArrayAccess in the ArrayCollection class.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16081 44c647ce-9c0f-0410-b52a-842ac1e357ba
wadearnold %!s(int64=16) %!d(string=hai) anos
pai
achega
aaccec9851

+ 61 - 51
library/Zend/Amf/Value/Messaging/ArrayCollection.php

@@ -29,17 +29,11 @@
  * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  */
-class Zend_Amf_Value_Messaging_ArrayCollection implements Iterator
+class Zend_Amf_Value_Messaging_ArrayCollection implements  ArrayAccess , IteratorAggregate, Countable
 {
     /**
-     * Current index of source 
-     * @var integer
-     */
-    protected $_sourceIndex = 0;
-    
-    /**
      * Data in the ArrayCollection
-     * @var unknown_type
+     * @var ArrayObject
      */
     protected $_source;
     
@@ -49,83 +43,96 @@ class Zend_Amf_Value_Messaging_ArrayCollection implements Iterator
      */
     public function __construct($data = null)
     {
-        $this->_source = array();
+        $this->_source = new ArrayObject();
         if (!is_null($data)) {
             $this->loadSource($data); 
         }
     }
     
-    /**
-     * Get the number of elements in the collection
-     * @return integer Count
-     */
-    public function count()
-    {
-        return count($this->_source);
-    }
     
     /**
-     * Reset the iterator to the beginning of the data
-     * @return void
+     * Value is append to the last element of the ArrayCollection
+     * @param misc new value to be added to the ArrayCollection
      */
-    public function rewind()
+    public function append($value)
     {
-        $this->_sourceIndex = 0;
+        if(!is_null($value))
+        {
+            $this->_source->append($value); 
+        }
     }
     
     /**
-     * Returns the data at the current index in the collection
-     * 
-     * @return mixed the current row or null if no rows exist. 
+     * Allow name value pairs to be added to the ArrayCollection
+     * @param mixed name pair
+     * @param mixed value pair
      */
-    public function current()
+    public function __set($name, $value)
     {
-        if(isset($this->_source[$this->_sourceIndex])) {
-            return $this->_source[$this->_sourceIndex];
+        if($name == 'externalizedData') {
+            $this->loadSource($value);
         } else {
-            return null;
+            $this->_source[] = array($name => $value); 
         }
     }
     
-    
     /**
-     * Returns the collections current index number
-     * @return mixed the current row number (starts at 0), null if there is no data 
+     * Get the number of elements in the collection
+     * @return integer Count
      */
-    public function key()
+    public function count()
     {
-        return $this->_sourceIndex;
+        return count($this->_source);
+    }
+        
+	/**
+     * Check if the specified offset exists exists for the key supplied. 
+     *
+     * @param mixed $offset
+     * @return bool true if it exists. 
+     */
+    function offsetExists($offset) {
+        return isset($this->_source[$offset]);
     }
 
     /**
-     * @return mixed the next row number collection, or null if not another row.
+     * Value of given offset
+     *
+     * @param mixed $offset
+     * @return mixed
      */
-    public function next()
-    {
-       return ++$this->_sourceIndex;
+    function offsetGet($offset) {
+        return $this->_source[$offset];
     }
 
     /**
-	 * checks if the iterator is valid
-	 * @return boolean is the iterator valid
+     * Update or add a new value based on the on the offset key. Careful as this will overwrite any existing propery by the same offset id
+     *
+     * @param mixed Offset to modify
+     * @param mixed New value for the offset. 
      */
-    public function valid()
-    {
-        return 0 <= $this->_sourceIndex && $this->_sourceIndex < $this->count();
+    function offsetSet($offset,$value) {
+        if (!is_null($offset)) {
+            $this->_source[$offset] = $value;
+        }
+    }
+
+    /**
+     * Offest to delete from the collection
+     *
+     * @param mixed $offset
+     */
+    function offsetUnset($offset) {
+        unset($this->_source[$offset]);
     }
     
     /**
-     * Allow name value pairs to be added to the ArrayCollection
-     * @param mixed name pair
-     * @param mixed value pair
+	 * Return the source of the iterator
+	 * @return ArrayObject
      */
-    public function __set($name, $value)
+    function getIterator()
     {
-        if($name == 'externalizedData') {
-            $this->loadSource($value);
-        } else {
-            $this->_source[] = array($name => $value); 
-        }
+        return $this->_source;
     }
     
     /**
@@ -153,6 +160,9 @@ class Zend_Amf_Value_Messaging_ArrayCollection implements Iterator
                     }
                 }
             }
+        } else {
+            require_once 'Zend/Amf/Server/Exception.php';
+            throw new Zend_Amf_Server_Exception("Could not load source data into an ArrayCollection must be an Array");
         }
     }
 }

+ 3 - 0
tests/Zend/Amf/AllTests.php

@@ -17,6 +17,7 @@ require_once 'Zend/Amf/Value/MessageBodyTest.php';
 require_once 'Zend/Amf/Value/MessageHeaderTest.php';
 require_once 'Zend/Amf/AuthTest.php';
 require_once 'Zend/Amf/ResourceTest.php';
+require_once 'Zend/Amf/Value/ArrayCollectionTest.php'
 
 class Zend_Amf_AllTests
 {
@@ -39,6 +40,8 @@ class Zend_Amf_AllTests
         $suite->addTestSuite('Zend_Amf_Value_MessageHeaderTest');
         $suite->addTestSuite('Zend_Amf_AuthTest');
         $suite->addTestSuite('Zend_Amf_ResourceTest');
+        $suite->addTestSuite('Zend_Amf_ArrayCollectionTest');
+        
 
         return $suite;
     }

+ 195 - 0
tests/Zend/Amf/Value/ArrayCollectionTest.php

@@ -0,0 +1,195 @@
+<?php
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Amf_Value_ArrayCollectionTest::main');
+}
+
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+require_once 'Zend/Amf/Value/Messaging/ArrayCollection.php';
+/**
+ * Test case for Zend_Amf_Value_MessageBody
+ *
+ * @package Zend_Amf
+ * @subpackage UnitTests
+ * @version $Id: MessageBodyTest.php 12004 2008-10-18 14:29:41Z mikaelkael $
+ */
+class Zend_Amf_Value_ArrayCollectionTest extends PHPUnit_Framework_TestCase
+{
+    
+    
+    /** 
+     * Refrence to the array collection
+     * @var Zend_Amf_Value_Message_ArrayCollection
+     */
+    protected $_arrayCollection;
+    
+    /**
+     * Data to be used to populate the ArrayCollection
+     */
+    protected $_data;
+    /**
+     * Runs the test methods of this class.
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite("Zend_Amf_Value_ArrayCollectionTest");
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+
+    public function setUp()
+    {
+        $data = array();
+        $data[] = array('foo' => 'foo1', 'bar' => 'bar1');
+        $data[] = array('foo' => 'foo2', 'bar' => 'bar2');
+        $this->_data = $data;
+        
+    }
+    
+    public function tearDown()
+    {
+        unset($this->_arrayCollection);
+        unset($this->_data);
+    }
+
+    public function testConstructorArrayCollectionTwo()
+    {
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollectionTwo($this->_data);
+        $this->assertEquals('bar2', $this->_arrayCollection[1]['bar']);       
+    }
+    
+    /**
+     * Check that the ArrayCollection can be accessed like a standard array.
+     */
+    public function testConstructorArrayCollection()
+    {
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
+        $this->assertEquals('bar2', $this->_arrayCollection[1]['bar']);
+    }
+    
+    /** 
+     * Check that we can get the count of the ArrayCollection
+     */
+    public function testCountable()
+    {
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
+        $this->assertEquals(2, count($this->_arrayCollection));
+    }
+    
+    /** 
+     * Test that we can foreach through the ArrayCollection
+     */
+    public function testIteratorArray()
+    {
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
+        $total = count($this->_arrayCollection);
+        $count = 0;
+        foreach($this->_arrayCollection as $row) {
+            $count++;
+        }
+        $this->assertEquals(2, $count);
+    }
+    
+    /**
+     * Test that we can alter an item based on it's offset
+     */
+    public function testOffsetExists()
+    {
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
+        $this->assertTrue($this->_arrayCollection->offsetExists(1));       
+    }
+    
+    /**
+     * Check that you can set and get the changes to an offset key.
+     */
+    public function testOffsetSetGet()
+    {
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
+        $data = array('fooSet' => 'fooSet2', 'barSet' => 'barSet2');
+        $this->_arrayCollection->offsetSet(1,$data);
+        $this->assertEquals($data, $this->_arrayCollection->offsetGet(1));
+    }
+    
+    /**
+     * Check that you can delete an item from the arraycollection based on key. 
+     */
+    public function testOffsetUnset()
+    {
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
+        $data = array('foo' => 'foo1', 'bar' => 'bar1');
+        $this->assertEquals($data, $this->_arrayCollection->offsetGet(0));
+        $this->assertEquals(2, count($this->_arrayCollection));
+        $this->_arrayCollection->offsetUnset(0);
+        $this->assertEquals(1, count($this->_arrayCollection));
+    }
+    
+    /**
+     * Check that you can transform an ArrayCollection into a standard array with iterator_to_array
+     */
+    public function testIteratorToArray()
+    {
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
+        $standardArray = iterator_to_array($this->_arrayCollection);
+        $this->assertTrue(is_array($standardArray));
+    }
+    
+    /**
+     * Make sure that you can append more name values to the arraycollection
+     */
+    public function testAppend()
+    {
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
+        $arrayCollectionTwo = new Zend_Amf_Value_Messaging_ArrayCollection();
+        $arrayCollectionTwo->append(array('foo' => 'foo1', 'bar' => 'bar1'));
+        $arrayCollectionTwo->append(array('foo' => 'foo2', 'bar' => 'bar2'));
+        $this->assertEquals($arrayCollectionTwo, $this->_arrayCollection);    
+    }
+    
+    /**
+     * Test to make sure that when the iterator as data it is a valid iterator
+     *
+    public function testValid()
+    {
+        unset($this->_arrayCollection);
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection();
+        $this->assertFalse($this->_arrayCollection->valid());
+        unset($this->_arrayCollection);
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
+        $this->assertTrue($this->_arrayCollection->valid());
+    }
+    */
+    
+    /*
+    public function testArrayIterator()
+    {
+        $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
+        $data0 = array('foo' => 'foo1', 'bar' => 'bar1');
+        $data1 = array('foo' => 'foo2', 'bar' => 'bar2');
+        $data3 = array('kung' => 'foo', 'Bruce' => 'Lee');
+        $this->_arrayCollection->offsetSet(3,$data3);
+        $this->assertEquals($data0,$this->_arrayCollection->current());
+        $this->_arrayCollection->next();
+        $this->assertEquals($data1,$this->_arrayCollection->current());
+        $this->_arrayCollection->next();
+        var_dump($this->_arrayCollection->key());
+        $this->assertEquals($data3,$this->_arrayCollection->current());
+        $this->_arrayCollection->rewind();
+        $this->assertEquals($data0,$this->_arrayCollection->current());
+        
+    }
+    */
+    
+
+}
+
+class Zend_Amf_Value_ArrayCollectionTest_SerializableData
+{
+    public function __toString()
+    {
+        return __CLASS__;
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Amf_Value_ArrayCollectionTest::main') {
+    Zend_Amf_Value_ArrayCollectionTest::main();
+}