Completed
Push — feature/improve-form-definitio... ( a3c595...bf8e8a )
by Romain
02:54
created

View::dataPreProcessor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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\Configuration\View;
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\Configuration\View\Classes\Classes;
20
use Romm\Formz\Configuration\View\Layouts\LayoutGroup;
21
use Romm\Formz\Exceptions\EntryNotFoundException;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
class View extends AbstractConfiguration implements DataPreProcessorInterface
25
{
26
27
    /**
28
     * @var \Romm\Formz\Configuration\View\Classes\Classes
29
     */
30
    protected $classes;
31
32
    /**
33
     * @var \Romm\Formz\Configuration\View\Layouts\LayoutGroup[]
34
     */
35
    protected $layouts = [];
36
37
    /**
38
     * @var array
39
     * @validate NotEmpty
40
     */
41
    protected $layoutRootPaths = [];
42
43
    /**
44
     * @var array
45
     * @validate NotEmpty
46
     */
47
    protected $partialRootPaths = [];
48
49
    /**
50
     * Constructor.
51
     */
52
    public function __construct()
53
    {
54
        $this->classes = GeneralUtility::makeInstance(Classes::class);
55
    }
56
57
    /**
58
     * @return Classes
59
     */
60
    public function getClasses()
61
    {
62
        return $this->classes;
63
    }
64
65
    /**
66
     * @return LayoutGroup[]
67
     */
68
    public function getLayouts()
69
    {
70
        return $this->layouts;
71
    }
72
73
    /**
74
     * @param string $name
75
     * @return bool
76
     */
77
    public function hasLayout($name)
78
    {
79
        return true === isset($this->layouts[$name]);
80
    }
81
82
    /**
83
     * @param string $name
84
     * @return LayoutGroup
85
     * @throws EntryNotFoundException
86
     */
87
    public function getLayout($name)
88
    {
89
        if (false === $this->hasLayout($name)) {
90
            throw EntryNotFoundException::viewLayoutNotFound($name);
91
        }
92
93
        return $this->layouts[$name];
94
    }
95
96
    /**
97
     * @param string $name
98
     * @return LayoutGroup
99
     */
100
    public function addLayout($name)
101
    {
102
        if ($this->hasParent($name)) {
103
            throw new \Exception('todo'); // @todo
104
        }
105
106
        /** @var LayoutGroup $layout */
107
        $layout = GeneralUtility::makeInstance(LayoutGroup::class, $name);
108
        $layout->attachParent($this);
0 ignored issues
show
Documentation Bug introduced by
The method attachParent does not exist on object<Romm\Formz\Config...ew\Layouts\LayoutGroup>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
109
110
        $this->layouts[$name] = $layout;
111
112
        return $layout;
113
    }
114
115
    /**
116
     * @param string $key
117
     * @param string $path
118
     */
119
    public function setLayoutRootPath($key, $path)
120
    {
121
        $this->layoutRootPaths[$key] = $path;
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    public function getLayoutRootPaths()
128
    {
129
        return $this->layoutRootPaths;
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function getAbsoluteLayoutRootPaths()
136
    {
137
        $paths = $this->layoutRootPaths;
138
139
        foreach ($paths as $key => $path) {
140
            $paths[$key] = $this->getAbsolutePath($path);
141
        }
142
143
        return $paths;
144
    }
145
146
    /**
147
     * @param string $key
148
     * @param string $path
149
     */
150
    public function setPartialRootPath($key, $path)
151
    {
152
        $this->partialRootPaths[$key] = $path;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    public function getPartialRootPaths()
159
    {
160
        return $this->partialRootPaths;
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    public function getAbsolutePartialRootPaths()
167
    {
168
        $paths = $this->partialRootPaths;
169
170
        foreach ($paths as $key => $path) {
171
            $paths[$key] = $this->getAbsolutePath($path);
172
        }
173
174
        return $paths;
175
    }
176
177
    /**
178
     * @param DataPreProcessor $processor
179
     */
180
    public static function dataPreProcessor(DataPreProcessor $processor)
181
    {
182
        $data = $processor->getData();
183
184
        /*
185
         * Forcing the names of the layouts: they are the keys of the array
186
         * entries.
187
         */
188
        foreach ($data['layouts'] as $key => $field) {
189
            $data['layouts'][$key]['name'] = $key;
190
        }
191
192
        $processor->setData($data);
193
    }
194
}
195