Completed
Push — wip/steps ( d74e9e...854d85 )
by Romain
02:42
created

Step::addSupportedField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
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\Form\Definition\Step\Step;
15
16
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
17
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
18
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
19
use Romm\ConfigurationObject\Traits\ConfigurationObject\StoreArrayIndexTrait;
20
use Romm\Formz\Exceptions\SilentException;
21
use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent;
22
use Romm\Formz\Form\Definition\Field\Field;
23
use Romm\Formz\Form\Definition\Step\Step\Substep\Substeps;
24
25
class Step extends AbstractFormDefinitionComponent implements DataPreProcessorInterface
26
{
27
    use ParentsTrait;
28
    use StoreArrayIndexTrait;
29
30
    /**
31
     * @var int
32
     * @validate IntegerValidator
33
     * @validate Romm.Formz:PageExists
34
     */
35
    protected $pageUid;
36
37
    /**
38
     * @var string
39
     */
40
    protected $extension;
41
42
    /**
43
     * @var string
44
     */
45
    protected $controller;
46
47
    /**
48
     * @var string
49
     * @validate NotEmpty
50
     */
51
    protected $action;
52
53
    /**
54
     * @var string[]
55
     */
56
    protected $authorizedActions = [];
57
58
    /**
59
     * @var \Romm\Formz\Form\Definition\Step\Step\SupportedField[]
60
     */
61
    protected $supportedFields;
62
63
    /**
64
     * @var \Romm\Formz\Form\Definition\Step\Step\Substep\Substeps
65
     */
66
    protected $substeps;
67
68
    /**
69
     * @return string
70
     */
71
    public function getIdentifier()
72
    {
73
        return $this->getArrayIndex();
74
    }
75
76
    /**
77
     * @return int
78
     */
79
    public function getPageUid()
80
    {
81
        return $this->pageUid;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getExtension()
88
    {
89
        return $this->extension;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getController()
96
    {
97
        return $this->controller;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getAction()
104
    {
105
        return $this->action;
106
    }
107
108
    /**
109
     * @return array
110
     */
111
    public function getAuthorizedActions()
112
    {
113
        return array_merge([$this->action], $this->authorizedActions);
114
    }
115
116
    /**
117
     * @return SupportedField[]
118
     */
119
    public function getSupportedFields()
120
    {
121
        return $this->supportedFields;
122
    }
123
124
    /**
125
     * @param string $fieldName
126
     */
127
    public function addSupportedField($fieldName)
128
    {
129
        $this->supportedFields[$fieldName] = new SupportedField($fieldName);
130
        $this->supportedFields[$fieldName]->attachParent($this);
131
    }
132
133
    /**
134
     * @param Field $field
135
     * @return bool
136
     */
137
    public function supportsField(Field $field)
138
    {
139
        foreach ($this->supportedFields as $supportedField) {
140
            if ($supportedField->getField() === $field) {
141
                return true;
142
            }
143
        }
144
145
        return false;
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    public function hasSubsteps()
152
    {
153
        return $this->substeps instanceof Substeps;
154
    }
155
156
    /**
157
     * Alias for Fluid usage.
158
     *
159
     * @return bool
160
     */
161
    public function getHasSubsteps()
162
    {
163
        return $this->hasSubsteps();
164
    }
165
166
    /**
167
     * @return Substeps
168
     */
169
    public function getSubsteps()
170
    {
171
        if (false === $this->hasSubsteps()) {
172
            throw new SilentException('todo'); // @todo
173
        }
174
175
        return $this->substeps;
176
    }
177
178
    /**
179
     * This function will parse the configuration for `supportedFields`: instead
180
     * of being forced to fill the `fieldName` option for each entry, the field
181
     * key of the entry can be the actual name of the field, and the value of
182
     * the entry can be anything but an array.
183
     *
184
     * @param DataPreProcessor $processor
185
     */
186
    public static function dataPreProcessor(DataPreProcessor $processor)
187
    {
188
        $data = $processor->getData();
189
190
        $supportedFields = (isset($data['supportedFields']))
191
            ? $data['supportedFields']
192
            : [];
193
194
        foreach ($supportedFields as $key => $supportedField) {
195
            $supportedField = is_array($supportedField)
196
                ? $supportedField
197
                : [];
198
            $supportedField['fieldName'] = $key;
199
200
            $supportedFields[$key] = $supportedField;
201
        }
202
203
        $data['supportedFields'] = $supportedFields;
204
205
        $processor->setData($data);
206
    }
207
}
208