Passed
Pull Request — master (#44)
by Romain
04:24
created

HasViewHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderStatic() 0 5 1
A render() 0 5 1
A initializeArguments() 0 7 1
A evaluateCondition() 0 8 1
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\ViewHelpers\Slot;
18
19
use Closure;
20
use CuyZ\Notiz\View\Slot\SlotContainer;
21
use CuyZ\Notiz\View\Slot\SlotView;
22
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
23
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
24
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
25
26
/**
27
 * Acts like a basic condition view-helper; allows checking if the given slot
28
 * has been registered in the view.
29
 *
30
 * Usage:
31
 *
32
 * ```
33
 * <nz:slot.has name="MySlot">
34
 *     <f:then>
35
 *         <nz:slot.render name="MySlot" />
36
 *     </f:then>
37
 *     <f:else>
38
 *         Some default value…
39
 *     </f:else>
40
 * </nz:slot>
41
 * ```
42
 *
43
 * Shorter notation is supported when else statement is not required:
44
 *
45
 * ```
46
 * <nz:slot.has name="MySlot">
47
 *     <nz:slot.render name="MySlot" />
48
 * </nz:slot>
49
 * ```
50
 */
51
class HasViewHelper extends AbstractConditionViewHelper implements CompilableInterface
52
{
53
    /**
54
     * Unfortunately, the rendering context is not passed to the method
55
     * `evaluateCondition`. We need to first save it in the class before the
56
     * method is called.
57
     *
58
     * @var RenderingContextInterface
59
     */
60
    protected static $staticRenderingContext;
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function initializeArguments()
66
    {
67
        parent::initializeArguments();
68
69
        $this->registerArgument('name', 'string', 'Name of the slot.', true);
70
71
        unset($this->argumentDefinitions['condition']);
72
    }
73
74
    /**
75
     * @param array $arguments
76
     * @return bool
77
     */
78
    protected static function evaluateCondition($arguments = null)
79
    {
80
        /** @var SlotContainer $slotContainer */
81
        $slotContainer = self::$staticRenderingContext
82
            ->getViewHelperVariableContainer()
83
            ->get(SlotView::class, SlotView::SLOT_CONTAINER);
84
85
        return $slotContainer->has($arguments['name']);
86
    }
87
88
    /**
89
     * @see HasViewHelper::$staticRenderingContext
90
     *
91
     * @return string
92
     */
93
    public function render()
94
    {
95
        self::$staticRenderingContext = $this->renderingContext;
96
97
        return parent::render();
98
    }
99
100
    /**
101
     * @see HasViewHelper::$staticRenderingContext
102
     *
103
     * @inheritdoc
104
     */
105
    public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
106
    {
107
        self::$staticRenderingContext = $renderingContext;
108
109
        return parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
110
    }
111
}
112