|
1
|
|
|
<?php |
|
2
|
|
|
namespace TYPO3Fluid\Fluid\ViewHelpers; |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file belongs to the package "TYPO3 Fluid". |
|
6
|
|
|
* See LICENSE.txt that was shipped with this package. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode; |
|
10
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
|
11
|
|
|
use TYPO3Fluid\Fluid\Core\Variables\VariableProviderInterface; |
|
12
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
|
13
|
|
|
|
|
14
|
|
|
class CViewHelper extends AbstractViewHelper |
|
15
|
|
|
{ |
|
16
|
|
|
protected $escapeChildren = false; |
|
17
|
|
|
|
|
18
|
|
|
protected $escapeOutput = false; |
|
19
|
|
|
|
|
20
|
|
|
public function initializeArguments() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->registerArgument('s', 'string', 'A string argument'); |
|
23
|
|
|
$this->registerArgument('i', 'int', 'An integer argument'); |
|
24
|
|
|
$this->registerArgument('f', 'float', 'A float argument'); |
|
25
|
|
|
$this->registerArgument('b', 'bool', 'A boolean argument'); |
|
26
|
|
|
$this->registerArgument('a', 'array', 'An array argument'); |
|
27
|
|
|
$this->registerArgument('dt', \DateTime::class, 'A DateTime object argument'); |
|
28
|
|
|
$this->registerArgument('return', 'bool', 'Return the arguments array, do not encode'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) |
|
32
|
|
|
{ |
|
33
|
|
|
$data = [ |
|
34
|
|
|
'string' => $arguments['s'], |
|
35
|
|
|
'int' => $arguments['i'], |
|
36
|
|
|
'float' => $arguments['f'], |
|
37
|
|
|
'bool' => $arguments['b'], |
|
38
|
|
|
'array' => $arguments['a'], |
|
39
|
|
|
'datetime' => ($arguments['dt'] instanceof \DateTime ? $arguments['dt']->format('U') : null), |
|
40
|
|
|
'child' => $renderChildrenClosure(), |
|
41
|
|
|
]; |
|
42
|
|
|
if ($arguments['return']) { |
|
43
|
|
|
return $data; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
return json_encode($data, JSON_PRETTY_PRINT); |
|
46
|
|
|
} |
|
47
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.