[FEATURE] Allow calling toArray() on ObjectStorage using name getArray() 61/46961/3
authorClaus Due <claus@namelesscoder.net>
Mon, 29 Feb 2016 14:17:41 +0000 (15:17 +0100)
committerBenni Mack <benni@typo3.org>
Sat, 5 Mar 2016 19:48:10 +0000 (20:48 +0100)
This change adds an alias method which allows ObjectStorage's
values to be accessed using a "get"-prefixed method name which
in turn allows the values to be addressed using numerical
indices or converted to arrays on-the-fly by adding an ".array"
segment to the property path.

Change-Id: Ibe94910d6043a52c7ef330e18e0330021ecbb325
Resolves: #73752
Releases: master
Reviewed-on: https://review.typo3.org/46961
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Feature-73752-AllowAccessingObjectStorageAsArrayInFluidAndOtherPlaces.rst [new file with mode: 0644]
typo3/sysext/extbase/Classes/Persistence/ObjectStorage.php
typo3/sysext/extbase/Tests/Unit/Persistence/ObjectStorageTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-73752-AllowAccessingObjectStorageAsArrayInFluidAndOtherPlaces.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-73752-AllowAccessingObjectStorageAsArrayInFluidAndOtherPlaces.rst
new file mode 100644 (file)
index 0000000..6c205f1
--- /dev/null
@@ -0,0 +1,23 @@
+==================================================================================
+Feature: #73752 - Allow accessing ObjectStorage as array in Fluid and other places
+==================================================================================
+
+Description
+===========
+
+Creates an alias of "toArray()" allowing the method to be called as "getArray()" which in turn allows the method to be
+called transparently from ObjectAccess::getPropertyPath, enabling access in Fluid and other places.
+
+
+Impact
+======
+
+By creating an extremely simple aliasing of "toArray()" on ObjectStorage allowing it to be called as "getArray()" enables:
+
+.. code-block:: php
+
+       ObjectAccess::getPropertyPath($subject, 'objectstorageproperty.array.4') to get the 4th element
+
+.. code-block:: text
+
+       {myObject.objectstorageproperty.array.4} in Fluid (including {myObject.objectstorageproperty.array.{dynamicIndex}} in v8)
index 948ba3e..f927b27 100644 (file)
@@ -299,6 +299,18 @@ class ObjectStorage implements \Countable, \Iterator, \ArrayAccess, ObjectMonito
     }
 
     /**
+     * Alias of toArray which allows that method to be used from contexts which support
+     * for example dotted paths, e.g. ObjectAccess::getPropertyPath($object, 'children.array.123')
+     * to get exactly the 123rd item in the "children" property which is an ObjectStorage.
+     *
+     * @return array
+     */
+    public function getArray()
+    {
+        return $this->toArray();
+    }
+
+    /**
      * Dummy method to avoid serialization.
      *
      * @throws \RuntimeException
index 45a926b..6234c79 100644 (file)
@@ -209,7 +209,8 @@ class ObjectStorageTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
         $object2 = new \StdClass();
         $objectStorage->attach($object1, 'foo');
         $objectStorage->attach($object2, 'bar');
-        $this->assertEquals($objectStorage->toArray(), array($object1, $object2));
+        $this->assertEquals(array($object1, $object2), $objectStorage->toArray());
+        $this->assertEquals(array($object1, $object2), $objectStorage->getArray());
     }
 
     /**