Completed
Branch unit-test-view-helpers (3b8d1a)
by Romain
03:13
created

AbstractViewHelper::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * 2016 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;
15
16
use Romm\Formz\ViewHelpers\Service\FormzViewHelperService;
17
18
abstract class AbstractViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
19
{
20
21
    /**
22
     * @var FormzViewHelperService
23
     */
24
    protected $service;
25
26
    /**
27
     * Initializes any ViewHelper correctly.
28
     */
29
    public function initialize()
30
    {
31
        $this->service = FormzViewHelperService::get();
32
    }
33
34
    /**
35
     * Checks that the current `FormViewHelper` exists. If not, an exception is
36
     * thrown.
37
     *
38
     * @throws \Exception
39
     */
40
    protected function checkIsInsideFormViewHelper()
41
    {
42
        $flag = $this->service->formContextExists();
43
44
        if (false === $flag) {
45
            throw new \Exception(
46
                'The view helper "' . get_called_class() . '" must be used inside the view helper "' . FormViewHelper::class . '".',
47
                1465243085
48
            );
49
        }
50
    }
51
52
    /**
53
     * Checks that the `FieldViewHelper` has been called. If not, an exception
54
     * is thrown.
55
     *
56
     * @throws \Exception
57
     */
58
    protected function checkIsInsideFieldViewHelper()
59
    {
60
        $flag = $this->service->fieldContextExists($this->renderingContext);
61
62
        if (false === $flag) {
63
            throw new \Exception(
64
                'The view helper "' . get_called_class() . '" must be used inside the view helper "' . FieldViewHelper::class . '".',
65
                1465243085
66
            );
67
        }
68
    }
69
}
70