Completed
Push — wip/steps-v9 ( 28f03f...7f392d )
by Romain
02:41
created

HasViewHelper::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\ViewHelpers\Slot;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Exceptions\ContextNotFoundException;
18
use Romm\Formz\Service\ViewHelper\Field\FieldViewHelperService;
19
use Romm\Formz\Service\ViewHelper\Slot\SlotViewHelperService;
20
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
21
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
22
23
/**
24
 * Will check if a given slot has been defined.
25
 *
26
 * @see \Romm\Formz\ViewHelpers\SlotViewHelper
27
 */
28
class HasViewHelper extends AbstractConditionViewHelper implements CompilableInterface
29
{
30
    /**
31
     * @inheritdoc
32
     */
33
    public function initializeArguments()
34
    {
35
        parent::initializeArguments();
36
37
        $this->registerArgument('slot', 'string', 'Name of the slot.', true);
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function render()
44
    {
45
        if (static::evaluateCondition($this->arguments)) {
46
            return $this->renderThenChild();
47
        }
48
49
        return $this->renderElseChild();
50
    }
51
52
    /**
53
     * @param array $arguments
54
     * @return bool
55
     * @throws ContextNotFoundException
56
     */
57
    protected static function evaluateCondition($arguments = null)
58
    {
59
        /** @var FieldViewHelperService $fieldService */
60
        $fieldService = Core::instantiate(FieldViewHelperService::class);
61
62
        if (false === $fieldService->fieldContextExists()) {
63
            throw ContextNotFoundException::slotHasViewHelperFieldContextNotFound();
64
        }
65
66
        /** @var SlotViewHelperService $slotService */
67
        $slotService = Core::instantiate(SlotViewHelperService::class);
68
        $slotName = $arguments['slot'];
69
70
        return $slotService->hasSlot($slotName);
71
    }
72
}
73