Completed
Push — middleware-wip ( f499dc...68caca )
by Romain
03:25
created

ViewClass::addItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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\Configuration\View\Classes;
15
16
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
17
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
18
use Romm\Formz\Configuration\AbstractConfiguration;
19
use Romm\Formz\Exceptions\EntryNotFoundException;
20
21
class ViewClass extends AbstractConfiguration implements DataPreProcessorInterface
22
{
23
24
    /**
25
     * @var array
26
     */
27
    protected $items = [];
28
29
    /**
30
     * @return array
31
     */
32
    public function getItems()
33
    {
34
        return $this->items;
35
    }
36
37
    /**
38
     * @param string $name
39
     * @return bool
40
     */
41
    public function hasItem($name)
42
    {
43
        return true === isset($this->items[$name]);
44
    }
45
46
    /**
47
     * @param string $name
48
     * @return array
49
     * @throws EntryNotFoundException
50
     */
51
    public function getItem($name)
52
    {
53
        if (false === $this->hasItem($name)) {
54
            throw EntryNotFoundException::viewClassNotFound($name);
55
        }
56
57
        return $this->items[$name];
58
    }
59
60
    /**
61
     * @param string $name
62
     * @param string $value
63
     */
64
    public function setItem($name, $value)
65
    {
66
        $this->checkConfigurationFreezeState();
67
68
        $this->items[$name] = $value;
69
    }
70
71
    /**
72
     * This function is called before the TypoScript data is injected in this
73
     * class. We use it to put all the array data in the key `items`, so it can
74
     * be properly injected in the `$items` property.
75
     *
76
     * @param DataPreProcessor $processor
77
     */
78
    public static function dataPreProcessor(DataPreProcessor $processor)
79
    {
80
        $finalData = [];
81
        $data = $processor->getData();
82
83
        foreach ($data as $key => $value) {
84
            $finalData[$key] = (string)$value;
85
        }
86
87
        $processor->setData(['items' => $finalData]);
88
    }
89
}
90