[FEATURE] Support 'has*' for properties in ObjectAccess 36/28036/4
authorSimon Schaufelberger <simonschaufi@googlemail.com>
Tue, 4 Mar 2014 13:28:45 +0000 (14:28 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Mon, 19 Jan 2015 06:43:22 +0000 (07:43 +0100)
Support calling object methods starting with 'has'.

Change-Id: Iec4913203b07fee70e6f5bb3ea9d980d2febc5c9
Resolves: #56529
Releases: master
Reviewed-on: http://review.typo3.org/28036
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Documentation/Changelog/master/Feature-56529-SupportHasInArrayObject.rst [new file with mode: 0644]
typo3/sysext/extbase/Classes/Reflection/ObjectAccess.php
typo3/sysext/extbase/Tests/Unit/Reflection/Fixture/DummyClassWithGettersAndSetters.php
typo3/sysext/extbase/Tests/Unit/Reflection/ObjectAccessTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-56529-SupportHasInArrayObject.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-56529-SupportHasInArrayObject.rst
new file mode 100644 (file)
index 0000000..2d4e871
--- /dev/null
@@ -0,0 +1,10 @@
+============================================================
+Feature: #56529 - Support \"has*\" Functions in ObjectAccess
+============================================================
+
+Description
+===========
+
+ObjectAccess supports \"is*\" and \"get*\" functions to return
+property values. Another convenience method is \"has*\", which
+can be used in the same way.
\ No newline at end of file
index 915f36b..99c50c0 100644 (file)
@@ -127,6 +127,10 @@ class ObjectAccess {
                if (is_callable(array($subject, $getterMethodName))) {
                        return $subject->{$getterMethodName}();
                }
+               $getterMethodName = 'has' . ucfirst($propertyName);
+               if (is_callable(array($subject, $getterMethodName))) {
+                       return $subject->{$getterMethodName}();
+               }
                if (is_object($subject) && array_key_exists($propertyName, get_object_vars($subject))) {
                        return $subject->{$propertyName};
                }
@@ -238,6 +242,9 @@ class ObjectAccess {
                                if (substr($methodName, 0, 3) === 'get') {
                                        $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
                                }
+                               if (substr($methodName, 0, 3) === 'has') {
+                                       $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
+                               }
                        }
                }
                $propertyNames = array_unique($declaredPropertyNames);
@@ -320,6 +327,9 @@ class ObjectAccess {
                if (is_callable(array($object, 'get' . ucfirst($propertyName)))) {
                        return TRUE;
                }
+               if (is_callable(array($object, 'has' . ucfirst($propertyName)))) {
+                       return TRUE;
+               }
                if (is_callable(array($object, 'is' . ucfirst($propertyName)))) {
                        return TRUE;
                }
index 6385323..2e539c0 100644 (file)
@@ -37,6 +37,8 @@ class DummyClassWithGettersAndSetters {
 
        public $publicProperty2 = 42;
 
+       protected $anotherBooleanProperty = TRUE;
+
        /**
         * @param mixed $property
         */
@@ -113,4 +115,22 @@ class DummyClassWithGettersAndSetters {
        public function setWriteOnlyMagicProperty($value) {
        }
 
+       /**
+        * sets the AnotherBooleanProperty
+        *
+        * @param boolean $anotherBooleanProperty
+        *
+        * @return void
+        */
+       public function setAnotherBooleanProperty($anotherBooleanProperty) {
+               $this->anotherBooleanProperty = $anotherBooleanProperty;
+       }
+
+       /**
+        * @return bool
+        */
+       public function hasAnotherBooleanProperty() {
+               return $this->anotherBooleanProperty;
+       }
+
 }
index 8c8579b..711e699 100644 (file)
@@ -256,7 +256,7 @@ class ObjectAccessTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        public function getGettablePropertyNamesReturnsAllPropertiesWhichAreAvailable() {
                $gettablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettablePropertyNames($this->dummyObject);
-               $expectedPropertyNames = array('anotherProperty', 'booleanProperty', 'property', 'property2', 'publicProperty', 'publicProperty2');
+               $expectedPropertyNames = array('anotherBooleanProperty', 'anotherProperty', 'booleanProperty', 'property', 'property2', 'publicProperty', 'publicProperty2');
                $this->assertEquals($gettablePropertyNames, $expectedPropertyNames, 'getGettablePropertyNames returns not all gettable properties.');
        }
 
@@ -265,7 +265,7 @@ class ObjectAccessTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        public function getSettablePropertyNamesReturnsAllPropertiesWhichAreAvailable() {
                $settablePropertyNames = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getSettablePropertyNames($this->dummyObject);
-               $expectedPropertyNames = array('anotherProperty', 'property', 'property2', 'publicProperty', 'publicProperty2', 'writeOnlyMagicProperty');
+               $expectedPropertyNames = array('anotherBooleanProperty', 'anotherProperty', 'property', 'property2', 'publicProperty', 'publicProperty2', 'writeOnlyMagicProperty');
                $this->assertEquals($settablePropertyNames, $expectedPropertyNames, 'getSettablePropertyNames returns not all settable properties.');
        }
 
@@ -287,6 +287,7 @@ class ObjectAccessTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        public function getGettablePropertiesReturnsTheCorrectValuesForAllProperties() {
                $allProperties = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getGettableProperties($this->dummyObject);
                $expectedProperties = array(
+                       'anotherBooleanProperty' => TRUE,
                        'anotherProperty' => 42,
                        'booleanProperty' => TRUE,
                        'property' => 'string1',
@@ -345,6 +346,7 @@ class ObjectAccessTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'privateProperty'));
                $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'writeOnlyMagicProperty'));
                $this->assertFalse(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'shouldNotBePickedUp'));
+               $this->assertTrue(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($this->dummyObject, 'anotherBooleanProperty'));
        }
 
        /**