Completed
Push — development ( 2f23f8...bbd82c )
by Romain
02:13
created

OptionViewHelper::injectFieldService()   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 1
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;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Exceptions\ContextNotFoundException;
18
use Romm\Formz\Service\ViewHelper\Field\FieldViewHelperService;
19
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
20
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
21
22
/**
23
 * Use this view helper at the first level inside the Field view helper. It will
24
 * be added to the arguments list, which can then be used inside the field
25
 * template.
26
 *
27
 * It does not add any feature, but allows the Fluid template to be far more
28
 * readable, as you can see below:
29
 *
30
 * Without the Option view helper:
31
 *
32
 * ```
33
 *  <fz:field layout="..."
34
 *               arguments="{label: '{f:translate(key: \'my_lll_key\')}', foo: 'bar'}">
35
 *
36
 *      ...
37
 *  </fz:field>
38
 * ```
39
 *
40
 * With it:
41
 *
42
 * ```
43
 *  <fz:field layout="...">
44
 *      <fz:option name="label" value="{f:translate(key: 'my_lll_key')}" />
45
 *      <fz:option name="foo" value="bar" />
46
 *
47
 *      ...
48
 *  </fz:field>
49
 * ```
50
 */
51
class OptionViewHelper extends AbstractViewHelper implements CompilableInterface
52
{
53
    /**
54
     * @var array
55
     */
56
    protected static $options = [];
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function initializeArguments()
62
    {
63
        $this->registerArgument('name', 'string', 'Name of the option.', true);
64
        $this->registerArgument('value', 'string', 'Value of the option.', true);
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function render()
71
    {
72
        return self::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
0 ignored issues
show
Compatibility introduced by
$this->renderingContext of type object<TYPO3Fluid\Fluid\...deringContextInterface> is not a sub-type of object<TYPO3\CMS\Fluid\C...deringContextInterface>. It seems like you assume a child interface of the interface TYPO3Fluid\Fluid\Core\Re...nderingContextInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
79
    {
80
        /** @var FieldViewHelperService $service */
81
        $service = Core::instantiate(FieldViewHelperService::class);
82
83
        if (false === $service->fieldContextExists()) {
84
            throw ContextNotFoundException::optionViewHelperFieldContextNotFound();
85
        }
86
87
        $service->setFieldOption($arguments['name'], $arguments['value']);
88
    }
89
}
90