Dynamic Getters

Sometimes it can be useful to get a value depending on specific method parameters.
By now and as far as i know extbase and fluid does not support this accessing strategy.

So i created my own ViewHelper and ObjectAccess Class:

The ViewHelper

/* * *************************************************************
 *  Copyright notice
 *
 *  (c) 2010 Sebastian Schreiber
 *  Contact: me@schreibersebastian.de
 *  All rights reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ************************************************************* */

/**
 *
 * @package TYPO3
 * @subpackage ssch_fluid_extbase_helper
 *
 * @author Sebastian Schreiber <me@schreibersebastian.de>
 *
 * $Id: GetterWithParametersViewHelper.php 56 2011-11-01 19:08:36Z schreiberten $
 */
class Tx_MyExt_ViewHelpers_GetterWithParametersViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {

    /**
     *
     * @param Tx_Extbase_DomainObject_AbstractEntity $object
     * @param string $propertyName
     * @param array $parameters
     * @return mixed
     */
    public function render(Tx_Extbase_DomainObject_AbstractEntity $object, $propertyName, array $parameters = array()) {
        return Tx_MyExt_Reflection_ObjectAccess::getPropertyWithArguments($object, $propertyName, $parameters);
    }

}

The extended ObjectAccess Class:


/* * *************************************************************
 *  Copyright notice
 *
 *  (c) 2010 Sebastian Schreiber
 *  Contact: me@schreibersebastian.de
 *  All rights reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ************************************************************* */

/**
 *
 * @package TYPO3
 * @subpackage ssch_fluid_extbase_helper
 *
 * @author Sebastian Schreiber <me@schreibersebastian.de>
 *
 * $Id: ObjectAccess.php 56 2011-11-01 19:08:36Z schreiberten $
 */
class Tx_MyExt_Reflection_ObjectAccess extends Tx_Extbase_Reflection_ObjectAccess {

    /**
     * Get a property of a given object.
     * Tries to get the property the following ways:
     * - if the target is an array, and has this property, we call it.
     * - if public getter method exists, call it.
     * - if the target object is an instance of ArrayAccess, it gets the property
     *   on it if it exists.
     * - if public property exists, return the value of it.
     * - else, throw exception
     *
     * @param mixed $subject Object or array to get the property from
     * @param string $propertyName name of the property to retrieve
     * @param array $parameters Optional parameters passed to the call_user_func method
     * @return object Value of the property.
     * @throws InvalidArgumentException in case $subject was not an object or $propertyName was not a string
     * @throws RuntimeException if the property was not accessible
     */
    static public function getPropertyWithArguments($subject, $propertyName, array $parameters = array()) {
        if (!is_object($subject) && !is_array($subject)) {
            throw new InvalidArgumentException('$subject must be an object or array, ' . gettype($subject) . ' given.', 1237301367);
        }
        if (!is_string($propertyName)) {
            throw new InvalidArgumentException('Given property name is not of type string.', 1231178303);
        }

        if (is_array($subject)) {
            if (array_key_exists($propertyName, $subject)) {
                return $subject[$propertyName];
            }
        } else {
            if (is_callable(array($subject, 'get' . ucfirst($propertyName)))) {
                return call_user_func_array(array($subject, 'get' . ucfirst($propertyName)), $parameters);
            } elseif (is_callable(array($subject, 'is' . ucfirst($propertyName)))) {
                return call_user_func_array(array($subject, 'is' . ucfirst($propertyName)), $parameters);
            } elseif ($subject instanceof ArrayAccess && isset($subject[$propertyName])) {
                return $subject[$propertyName];
            } elseif (array_key_exists($propertyName, get_object_vars($subject))) {
                return $subject->$propertyName;
            }
        }

        throw new Tx_Extbase_Reflection_Exception_PropertyNotAccessibleException('The property "' . $propertyName . '" on the subject was not accessible.', 1263391473);
    }

}

Veröffentlicht von

avatar

Sebastian Schreiber

Ich bin 1980 in Bergisch Gladbach geboren. Nach dem Abitur und Zivildienst habe ich 2000 an der Fachhochschule Lippe & Höxter das Studium der Medienproduktin begonnen und nach längeren Aufenthalten in Valencia und Berlin 2003 das Studium mit dem Abschluss Bachelor of Science in Medienproduktion abgeschlossen. Nach einer Festanstellung kurz nach dem Studium in einer kleinen Webagentur in Köln mit Schwerpunkt TYPO3 bin ich nun seit 2008 freiberuflicher Webentwickler.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.