Completed
Push — development ( 9be619...e22509 )
by Romain
02:55
created

FieldViewHelperService::getFieldOptions()   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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\Service\ViewHelper\Field;
15
16
use Romm\Formz\Configuration\Form\Field\Field;
17
use Romm\Formz\Configuration\View\Layouts\Layout;
18
use Romm\Formz\Core\Core;
19
use TYPO3\CMS\Core\SingletonInterface;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
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 FieldViewHelperService implements SingletonInterface
28
{
29
    /**
30
     * Contains all current fields being rendered by FormZ: if a field is
31
     * rendered beneath another field, several entries will be added to this
32
     * property.
33
     *
34
     * @var FieldContextEntry[]
35
     */
36
    protected $contextEntries;
37
38
    /**
39
     * @var StandaloneView[]
40
     */
41
    protected $view;
42
43
    /**
44
     * Adds a new context entry to the entries array. The other field-related
45
     * methods will be processed on this entry until the field rendering has
46
     * ended.
47
     *
48
     * @param Field $field
49
     */
50
    public function setCurrentField(Field $field)
51
    {
52
        $this->contextEntries[] = GeneralUtility::makeInstance(FieldContextEntry::class, $field);
53
    }
54
55
    /**
56
     * Removes the current field context entry.
57
     */
58
    public function removeCurrentField()
59
    {
60
        array_pop($this->contextEntries);
61
    }
62
63
    /**
64
     * Checks that a field context is found.
65
     *
66
     * @return bool
67
     */
68
    public function fieldContextExists()
69
    {
70
        return false === empty($this->contextEntries);
71
    }
72
73
    /**
74
     * @return Field
75
     */
76
    public function getCurrentField()
77
    {
78
        return $this->getCurrentContext()->getField();
79
    }
80
81
    /**
82
     * @param string $name
83
     * @param mixed  $value
84
     */
85
    public function setFieldOption($name, $value)
86
    {
87
        $this->getCurrentContext()->setOption($name, $value);
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function getFieldOptions()
94
    {
95
        return $this->getCurrentContext()->getOptions();
96
    }
97
98
    /**
99
     * Returns a view instance, based on the template file of the layout. The
100
     * view is stored in local cache, to improve performance: the template file
101
     * content will be fetched only once.
102
     *
103
     * @param Layout $layout
104
     * @return StandaloneView
105
     */
106
    public function getView(Layout $layout)
107
    {
108
        $identifier = $layout->getTemplateFile();
109
110
        if (null === $this->view[$identifier]) {
111
            /** @var StandaloneView $view */
112
            $view = Core::instantiate(StandaloneView::class);
113
            $view->setTemplatePathAndFilename($layout->getTemplateFile());
114
115
            $this->view[$identifier] = $view;
116
        }
117
118
        return $this->view[$identifier];
119
    }
120
121
    /**
122
     * @return FieldContextEntry
123
     */
124
    protected function getCurrentContext()
125
    {
126
        return end($this->contextEntries);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression end($this->contextEntries); of type Romm\Formz\Service\ViewH...FieldContextEntry|false adds false to the return on line 126 which is incompatible with the return type documented by Romm\Formz\Service\ViewH...vice::getCurrentContext of type Romm\Formz\Service\ViewH...Field\FieldContextEntry. It seems like you forgot to handle an error condition.
Loading history...
127
    }
128
}
129