Completed
Push — wip/steps ( 47ee47 )
by Romain
03:13
created

Step::getSubsteps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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