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\Configuration\Form\Step\Step\Step; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This object is stored in a form metadata, and contains important information |
20
|
|
|
* about the form steps: |
21
|
|
|
* - Which steps were already validated; |
22
|
|
|
* - Form data that were submitted by the user at every step. |
23
|
|
|
* |
24
|
|
|
* Data consistency of this object is assured by the form object hash (which is |
25
|
|
|
* mainly calculated from the form configuration): if the hash changes (if the |
26
|
|
|
* form configuration changes), the steps that were validated are no longer |
27
|
|
|
* considered as valid, and will need to be validated again. |
28
|
|
|
*/ |
29
|
|
|
class FormStepPersistence |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $objectHash; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $validatedSteps = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $stepsFormValues = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $configurationHash |
48
|
|
|
*/ |
49
|
|
|
public function __construct($configurationHash) |
50
|
|
|
{ |
51
|
|
|
$this->objectHash = $configurationHash; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Step $step |
56
|
|
|
*/ |
57
|
|
|
public function markStepAsValidated(Step $step) |
58
|
|
|
{ |
59
|
|
|
$this->validatedSteps[] = $step->getName(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Step $step |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function stepWasValidated(Step $step) |
67
|
|
|
{ |
68
|
|
|
return in_array($step->getName(), $this->validatedSteps); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Step $step |
73
|
|
|
* @param array $formValues |
74
|
|
|
*/ |
75
|
|
|
public function addStepFormValues(Step $step, array $formValues) |
76
|
|
|
{ |
77
|
|
|
$this->stepsFormValues[$step->getName()] = $formValues; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Step $step |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function hasStepFormValues(Step $step) |
85
|
|
|
{ |
86
|
|
|
return true === array_key_exists($step->getName(), $this->stepsFormValues); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Step $step |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function getStepFormValues(Step $step) |
94
|
|
|
{ |
95
|
|
|
return $this->stepsFormValues[$step->getName()]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function hasData() |
102
|
|
|
{ |
103
|
|
|
return false === empty($this->validatedSteps) |
104
|
|
|
&& false === empty($this->stepsFormValues); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getObjectHash() |
111
|
|
|
{ |
112
|
|
|
return $this->objectHash; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $hash |
117
|
|
|
*/ |
118
|
|
|
public function refreshObjectHash($hash) |
119
|
|
|
{ |
120
|
|
|
$this->objectHash = $hash; |
121
|
|
|
$this->validatedSteps = []; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|