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\Compiler\TemplateCompiler; |
10
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\NodeFilterInterface; |
11
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\NodeInterface; |
12
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\TextNode; |
13
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode; |
14
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper; |
15
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A view helper which specifies the "default" case when used within the SwitchViewHelper. |
19
|
|
|
* @see \TYPO3Fluid\Fluid\ViewHelpers\SwitchViewHelper |
20
|
|
|
* |
21
|
|
|
* @api |
22
|
|
|
*/ |
23
|
|
|
class DefaultCaseViewHelper extends AbstractViewHelper implements NodeFilterInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var boolean |
28
|
|
|
*/ |
29
|
|
|
protected $escapeOutput = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string the contents of this view helper if no other "Case" view helper of the surrounding switch view helper matches |
33
|
|
|
* @throws ViewHelper\Exception |
34
|
|
|
* @api |
35
|
|
|
*/ |
36
|
|
|
public function render() |
37
|
|
|
{ |
38
|
|
|
$viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer(); |
39
|
|
|
if (!$viewHelperVariableContainer->exists(SwitchViewHelper::class, 'switchExpression')) { |
40
|
|
|
throw new ViewHelper\Exception('The "default case" View helper can only be used within a switch View helper', 1368112037); |
41
|
|
|
} |
42
|
|
|
return $this->renderChildren(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* DefaultCase ViewHelper disallows any whitespace-only text nodes as children. This means that |
47
|
|
|
* when rendered, the output of the node excludes any whitespace before and after the |
48
|
|
|
* "real" output. |
49
|
|
|
* |
50
|
|
|
* @param NodeInterface $node |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function allowsChildNodeType(NodeInterface $node): bool |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if ($node instanceof TextNode && trim($node->getText()) === '') { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $argumentsName |
63
|
|
|
* @param string $closureName |
64
|
|
|
* @param string $initializationPhpCode |
65
|
|
|
* @param ViewHelperNode $node |
66
|
|
|
* @param TemplateCompiler $compiler |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler) |
70
|
|
|
{ |
71
|
|
|
return '\'\''; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.