Completed
Push — wip/steps ( 116a8d...b2c91c )
by Romain
02:25
created

FormStepPersistence::getStepFormValues()   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 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\FormObject\Service\Step;
15
16
use Romm\Formz\Form\Definition\Step\Step\Step;
17
use Romm\Formz\Form\Definition\Step\Step\StepDefinition;
18
use TYPO3\CMS\Core\Utility\ArrayUtility;
19
20
/**
21
 * This object is stored in a form metadata, and contains important information
22
 * about the form steps:
23
 * - Which steps were already validated;
24
 * - Form data that were submitted by the user at every step. @todo tmp-delete?
25
 *
26
 * Data consistency of this object is assured by the form object hash (which is
27
 * mainly calculated from the form configuration): if the hash changes (if the
28
 * form configuration changes), the steps that were validated are no longer
29
 * considered as valid, and will need to be validated again.
30
 */
31
class FormStepPersistence
32
{
33
    /**
34
     * @var string
35
     */
36
    protected $objectHash;
37
38
    /**
39
     * @var array
40
     */
41
    protected $validatedSteps = [];
42
43
    /**
44
     * @var array
45
     */
46
    protected $stepLevels = [];
47
48
    // @todo tmp-delete?
49
//    /**
50
//     * @var array
51
//     */
52
//    protected $stepsFormValues = [];
53
54
    /**
55
     * @var array
56
     */
57
    protected $validatedFields = [];
58
59
    /**
60
     * @param string $configurationHash
61
     */
62
    public function __construct($configurationHash)
63
    {
64
        $this->objectHash = $configurationHash;
65
    }
66
67
    /**
68
     * @param StepDefinition $stepDefinition
69
     */
70
    public function markStepAsValidated(StepDefinition $stepDefinition)
71
    {
72
        $identifier = $stepDefinition->getStep()->getIdentifier();
73
74
        $this->validatedSteps[$identifier] = $identifier;
75
        $this->stepLevels[$stepDefinition->getStepLevel()] = $identifier;
76
    }
77
78
    /**
79
     * @param StepDefinition $stepDefinition
80
     */
81
    public function removeStep(StepDefinition $stepDefinition)
82
    {
83
        $identifier = $stepDefinition->getStep()->getIdentifier();
84
85
        unset($this->validatedSteps[$identifier]);
86
        unset($this->stepLevels[$stepDefinition->getStepLevel()]);
87
        // @todo tmp-delete?
88
//        unset($this->stepsFormValues[$identifier]);
89
    }
90
91
    /**
92
     * @param Step $step
93
     * @return bool
94
     */
95
    public function stepWasValidated(Step $step)
96
    {
97
        return in_array($step->getIdentifier(), $this->validatedSteps);
98
    }
99
100
    /**
101
     * @param StepDefinition $stepDefinition
102
     */
103
    public function setStepLevel(StepDefinition $stepDefinition)
104
    {
105
        $this->stepLevels[$stepDefinition->getStepLevel()] = $stepDefinition->getStep()->getIdentifier();
106
    }
107
108
    /**
109
     * @param int $level
110
     * @return bool
111
     */
112
    public function hasStepIdentifierAtLevel($level)
113
    {
114
        return isset($this->stepLevels[$level]);
115
    }
116
117
    /**
118
     * @param int $level
119
     * @return string
120
     */
121
    public function getStepIdentifierAtLevel($level)
122
    {
123
        if (false === $this->hasStepIdentifierAtLevel($level)) {
124
            throw new \Exception('todo'); // @todo
125
        }
126
127
        return $this->stepLevels[$level];
128
    }
129
130
    // @todo tmp-delete?
131
//    /**
132
//     * @param StepDefinition $stepDefinition
133
//     * @param array          $formValues
134
//     */
135
//    public function addStepFormValues(StepDefinition $stepDefinition, array $formValues)
136
//    {
137
//        $identifier = $stepDefinition->getStep()->getIdentifier();
138
//
139
//        if (false === isset($this->stepsFormValues[$identifier])) {
140
//            $this->stepsFormValues[$identifier] = $formValues;
141
//        } else {
142
//            ArrayUtility::mergeRecursiveWithOverrule($this->stepsFormValues[$identifier], $formValues);
143
//        }
144
//    }
145
//
146
//    /**
147
//     * @param StepDefinition $stepDefinition
148
//     * @return bool
149
//     */
150
//    public function hasStepFormValues(StepDefinition $stepDefinition)
151
//    {
152
//        return true === array_key_exists($stepDefinition->getStep()->getIdentifier(), $this->stepsFormValues);
153
//    }
154
//
155
//    /**
156
//     * @param StepDefinition $stepDefinition
157
//     * @return array
158
//     */
159
//    public function getStepFormValues(StepDefinition $stepDefinition)
160
//    {
161
//        if (false === $this->hasStepFormValues($stepDefinition)) {
162
//            throw new \Exception('todo'); // @todo
163
//        }
164
//
165
//        return $this->stepsFormValues[$stepDefinition->getStep()->getIdentifier()];
166
//    }
167
//
168
//    /**
169
//     * @return array
170
//     */
171
//    public function getMergedFormValues()
172
//    {
173
//        $formValues = [];
174
//
175
//        foreach ($this->stepsFormValues as $stepFormValues) {
176
//            unset($stepFormValues['__identity']);
177
//            $formValues = array_merge($formValues, $stepFormValues);
178
//        }
179
//
180
//        return $formValues;
181
//    }
182
183
    /**
184
     * @param array $validatedFields
185
     */
186
    public function addValidatedFields(array $validatedFields)
187
    {
188
        $this->validatedFields = array_merge($this->validatedFields, $validatedFields);
189
    }
190
191
    /**
192
     * @return bool
193
     */
194
    public function hasData()
195
    {
196
        return false === empty($this->validatedSteps);
197
        // @todo tmp-delete?
198
//            && false === empty($this->stepsFormValues);
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    public function getObjectHash()
205
    {
206
        return $this->objectHash;
207
    }
208
209
    /**
210
     * @param string $hash
211
     */
212
    public function refreshObjectHash($hash)
213
    {
214
        $this->objectHash = $hash;
215
        $this->resetValidationData();
216
    }
217
218
    /**
219
     * @todo
220
     */
221
    public function resetValidationData()
222
    {
223
        $this->validatedSteps = [];
224
        $this->stepLevels = [];
225
    }
226
}
227