Completed
Push — unit-test-view-helpers ( d4d7d8...d9d29f )
by Romain
06:46
created

FieldService::getView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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\Service;
15
16
use Romm\Formz\Configuration\Form\Field\Field;
17
use Romm\Formz\Core\Core;
18
use Romm\Formz\Exceptions\ContextNotFoundException;
19
use Romm\Formz\ViewHelpers\FieldViewHelper;
20
use TYPO3\CMS\Core\SingletonInterface;
21
use TYPO3\CMS\Fluid\View\StandaloneView;
22
23
/**
24
 * Contains methods to help view helpers to manipulate data concerning the
25
 * current field.
26
 */
27
class FieldService implements SingletonInterface
28
{
29
    /**
30
     * @var Field
31
     */
32
    protected $currentField;
33
34
    /**
35
     * @var array
36
     */
37
    protected $fieldOptions = [];
38
39
    /**
40
     * Unique instance of view, stored to save some performance.
41
     *
42
     * @var StandaloneView
43
     */
44
    protected $view;
45
46
    /**
47
     * Reset every state that can be used by this service.
48
     */
49
    public function resetState()
50
    {
51
        $this->currentField = null;
52
    }
53
54
    /**
55
     * Checks that the `FieldViewHelper` has been called. If not, an exception
56
     * is thrown.
57
     *
58
     * @return bool
59
     */
60
    public function fieldContextExists()
61
    {
62
        return $this->currentField instanceof Field;
63
    }
64
65
    /**
66
     * Returns the current field which was defined by the `FieldViewHelper`.
67
     *
68
     * Returns null if no current field was found.
69
     *
70
     * @return Field|null
71
     */
72
    public function getCurrentField()
73
    {
74
        return $this->currentField;
75
    }
76
77
    /**
78
     * @param Field $field
79
     */
80
    public function setCurrentField(Field $field)
81
    {
82
        $this->currentField = $field;
83
    }
84
85
    /**
86
     * @param string $name
87
     * @param mixed  $value
88
     */
89
    public function setFieldOption($name, $value)
90
    {
91
        $this->fieldOptions[$name] = $value;
92
    }
93
94
    /**
95
     * @return array
96
     */
97
    public function getFieldOptions()
98
    {
99
        return $this->fieldOptions;
100
    }
101
102
    /**
103
     * @return $this
104
     */
105
    public function resetFieldOptions()
106
    {
107
        $this->fieldOptions = [];
108
109
        return $this;
110
    }
111
112
    /**
113
     * Unset the current field.
114
     *
115
     * @return $this
116
     */
117
    public function removeCurrentField()
118
    {
119
        $this->currentField = null;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Checks that the `FieldViewHelper` has been called. If not, an exception
126
     * is thrown.
127
     *
128
     * @throws \Exception
129
     */
130
    public function checkIsInsideFieldViewHelper()
131
    {
132
        if (false === $this->fieldContextExists()) {
133
            throw new ContextNotFoundException(
134
                'The view helper "' . get_called_class() . '" must be used inside the view helper "' . FieldViewHelper::class . '".',
135
                1465243085
136
            );
137
        }
138
    }
139
140
    /**
141
     * @return StandaloneView
142
     */
143
    public function getView()
144
    {
145
        if (null === $this->view) {
146
            $this->view = Core::instantiate(StandaloneView::class);
147
        }
148
149
        return $this->view;
150
    }
151
}
152