Completed
Pull Request — master (#446)
by Claus
02:04
created

CaseViewHelper::allowsChildNodeType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 7
loc 7
rs 10
c 0
b 0
f 0
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
 * Case view helper that is only usable within the SwitchViewHelper.
19
 * @see \TYPO3Fluid\Fluid\ViewHelpers\SwitchViewHelper
20
 *
21
 * @api
22
 */
23
class CaseViewHelper extends AbstractViewHelper implements NodeFilterInterface
24
{
25
26
    /**
27
     * @var boolean
28
     */
29
    protected $escapeOutput = false;
30
31
    /**
32
     * @return void
33
     */
34
    public function initializeArguments()
35
    {
36
        parent::initializeArguments();
37
        $this->registerArgument('value', 'mixed', 'Value to match in this case', true);
38
    }
39
40
    /**
41
     * @return string the contents of this view helper if $value equals the expression of the surrounding switch view helper, otherwise an empty string
42
     * @throws ViewHelper\Exception
43
     * @api
44
     */
45
    public function render()
46
    {
47
        $value = $this->arguments['value'];
48
        $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
49
        if (!$viewHelperVariableContainer->exists(SwitchViewHelper::class, 'switchExpression')) {
50
            throw new ViewHelper\Exception('The "case" View helper can only be used within a switch View helper', 1368112037);
51
        }
52
        $switchExpression = $viewHelperVariableContainer->get(SwitchViewHelper::class, 'switchExpression');
53
54
        // non-type-safe comparison by intention
55
        if ($switchExpression == $value) {
56
            $viewHelperVariableContainer->addOrUpdate(SwitchViewHelper::class, 'break', true);
57
            return $this->renderChildren();
58
        }
59
        return '';
60
    }
61
62
    /**
63
     * Case ViewHelper disallows any whitespace-only text nodes as children. This means that
64
     * when rendered, the output of the node excludes any whitespace before and after the
65
     * "real" output.
66
     *
67
     * @param NodeInterface $node
68
     * @return bool
69
     */
70 View Code Duplication
    public function allowsChildNodeType(NodeInterface $node): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
71
    {
72
        if ($node instanceof TextNode && trim($node->getText()) === '') {
73
            return false;
74
        }
75
        return true;
76
    }
77
78
    /**
79
     * @param string $argumentsName
80
     * @param string $closureName
81
     * @param string $initializationPhpCode
82
     * @param ViewHelperNode $node
83
     * @param TemplateCompiler $compiler
84
     * @return string
85
     */
86
    public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
87
    {
88
        return '\'\'';
89
    }
90
}
91