Completed
Pull Request — master (#214)
by Claus
03:52
created

CaseViewHelper::compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 5
dl 0
loc 4
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\SyntaxTree\ViewHelperNode;
11
use TYPO3Fluid\Fluid\Core\ViewHelper;
12
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
13
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileEmpty;
14
15
/**
16
 * Case view helper that is only usable within the SwitchViewHelper.
17
 * @see \TYPO3Fluid\Fluid\ViewHelpers\SwitchViewHelper
18
 *
19
 * @api
20
 */
21
class CaseViewHelper extends AbstractViewHelper
22
{
23
    use CompileEmpty;
24
25
    /**
26
     * @var boolean
27
     */
28
    protected $escapeOutput = false;
29
30
    /**
31
     * @return void
32
     */
33
    public function initializeArguments()
34
    {
35
        parent::initializeArguments();
36
        $this->registerArgument('value', 'mixed', 'Value to match in this case', true);
37
    }
38
39
    /**
40
     * @return string the contents of this view helper if $value equals the expression of the surrounding switch view helper, otherwise an empty string
41
     * @throws ViewHelper\Exception
42
     * @api
43
     */
44
    public function render()
45
    {
46
        $value = $this->arguments['value'];
47
        $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
48
        if (!$viewHelperVariableContainer->exists(SwitchViewHelper::class, 'switchExpression')) {
49
            throw new ViewHelper\Exception('The "case" View helper can only be used within a switch View helper', 1368112037);
50
        }
51
        $switchExpression = $viewHelperVariableContainer->get(SwitchViewHelper::class, 'switchExpression');
52
53
        // non-type-safe comparison by intention
54
        if ($switchExpression == $value) {
55
            $viewHelperVariableContainer->addOrUpdate(SwitchViewHelper::class, 'break', true);
56
            return $this->renderChildren();
57
        }
58
        return '';
59
    }
60
}
61