Completed
Push — middleware-wip ( a29b16...4a8d7b )
by Romain
11:57
created

FormStepPersistence::resetValidationData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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