Completed
Push — wip/steps ( b236f9...8555f4 )
by Romain
03:09
created

Step::getAuthorizedActions()   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\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
     * @validate NotEmpty
61
     */
62
    protected $supportedFields;
63
64
    /**
65
     * @var \Romm\Formz\Form\Definition\Step\Step\Substep\Substeps
66
     */
67
    protected $substeps;
68
69
    /**
70
     * @return string
71
     */
72
    public function getIdentifier()
73
    {
74
        return $this->getArrayIndex();
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    public function getPageUid()
81
    {
82
        return $this->pageUid;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getExtension()
89
    {
90
        return $this->extension;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getController()
97
    {
98
        return $this->controller;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getAction()
105
    {
106
        return $this->action;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getAuthorizedActions()
113
    {
114
        return array_merge([$this->action], $this->authorizedActions);
115
    }
116
117
    /**
118
     * @return SupportedField[]
119
     */
120
    public function getSupportedFields()
121
    {
122
        return $this->supportedFields;
123
    }
124
125
    /**
126
     * @param Field $field
127
     * @return bool
128
     */
129
    public function supportsField(Field $field)
130
    {
131
        foreach ($this->supportedFields as $supportedField) {
132
            if ($supportedField->getField() === $field) {
133
                return true;
134
            }
135
        }
136
137
        return false;
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function hasSubsteps()
144
    {
145
        return $this->substeps instanceof Substeps;
146
    }
147
148
    /**
149
     * @return Substeps
150
     */
151
    public function getSubsteps()
152
    {
153
        if (false === $this->hasSubsteps()) {
154
            throw new SilentException('todo'); // @todo
155
        }
156
157
        return $this->substeps;
158
    }
159
160
    /**
161
     * This function will parse the configuration for `supportedFields`: instead
162
     * of being forced to fill the `fieldName` option for each entry, the field
163
     * key of the entry can be the actual name of the field, and the value of
164
     * the entry can be anything but an array.
165
     *
166
     * @param DataPreProcessor $processor
167
     */
168
    public static function dataPreProcessor(DataPreProcessor $processor)
169
    {
170
        $data = $processor->getData();
171
172
        $supportedFields = (isset($data['supportedFields']))
173
            ? $data['supportedFields']
174
            : [];
175
176
        foreach ($supportedFields as $key => $supportedField) {
177
            $supportedField = is_array($supportedField)
178
                ? $supportedField
179
                : [];
180
            $supportedField['fieldName'] = $key;
181
182
            $supportedFields[$key] = $supportedField;
183
        }
184
185
        $data['supportedFields'] = $supportedFields;
186
187
        $processor->setData($data);
188
    }
189
}
190