Files
oberstufe-alt/typo3conf/ext/vhs/Classes/ViewHelpers/CallViewHelper.php
2018-04-02 08:07:38 +02:00

55 lines
1.7 KiB
PHP

<?php
namespace FluidTYPO3\Vhs\ViewHelpers;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* ### Call ViewHelper
*
* Calls a method on an existing object. Usable as inline or tag.
*
* ### Examples
*
* <!-- inline, useful as argument, for example in f:for -->
* {object -> v:call(method: 'toArray')}
* <!-- tag, useful to quickly output simple values -->
* <v:call object="{object}" method="unconventionalGetter" />
* <v:call method="unconventionalGetter">{object}</v:call>
* <!-- arguments for the method -->
* <v:call object="{object}" method="doSomethingWithArguments" arguments="{0: 'foo', 1: 'bar'}" />
*
* @author Claus Due <claus@namelesscoder.net>
* @package Vhs
* @subpackage ViewHelpers
*/
class CallViewHelper extends AbstractViewHelper {
/**
* @param string $method
* @param object $object
* @param array $arguments
* @throws \RuntimeException
* @return mixed
*/
public function render($method, $object = NULL, array $arguments = array()) {
if (NULL === $object) {
$object = $this->renderChildren();
if (FALSE === is_object($object)) {
throw new \RuntimeException('Using v:call requires an object either as "object" attribute, tag content or inline argument', 1356849652);
}
}
if (FALSE === method_exists($object, $method)) {
throw new \RuntimeException('Method "' . $method . '" does not exist on object of type ' . get_class($object), 1356834755);
}
return call_user_func_array(array($object, $method), $arguments);
}
}