Completed
Push — middleware-wip ( a8f94d...dac6b8 )
by Romain
16:11
created

FormStepPersistence::hasData()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 3
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\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
     * @var array
48
     */
49
    protected $validatedFields = [];
50
51
    /**
52
     * @param string $configurationHash
53
     */
54
    public function __construct($configurationHash)
55
    {
56
        $this->objectHash = $configurationHash;
57
    }
58
59
    /**
60
     * @param Step $step
61
     */
62
    public function markStepAsValidated(Step $step)
63
    {
64
        $this->validatedSteps[] = $step->getName();
65
    }
66
67
    /**
68
     * @param Step $step
69
     * @return bool
70
     */
71
    public function stepWasValidated(Step $step)
72
    {
73
        return in_array($step->getName(), $this->validatedSteps);
74
    }
75
76
    /**
77
     * @param Step  $step
78
     * @param array $formValues
79
     */
80
    public function addStepFormValues(Step $step, array $formValues)
81
    {
82
        $this->stepsFormValues[$step->getName()] = $formValues;
83
    }
84
85
    /**
86
     * @param Step $step
87
     * @return bool
88
     */
89
    public function hasStepFormValues(Step $step)
90
    {
91
        return true === array_key_exists($step->getName(), $this->stepsFormValues);
92
    }
93
94
    /**
95
     * @param Step $step
96
     * @return array
97
     */
98
    public function getStepFormValues(Step $step)
99
    {
100
        return $this->stepsFormValues[$step->getName()];
101
    }
102
103
    /**
104
     * @param array $validatedFields
105
     */
106
    public function addValidatedFields(array $validatedFields)
107
    {
108
        $this->validatedFields = array_merge($this->validatedFields, $validatedFields);
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    public function hasData()
115
    {
116
        return false === empty($this->validatedSteps)
117
            && false === empty($this->stepsFormValues);
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getObjectHash()
124
    {
125
        return $this->objectHash;
126
    }
127
128
    /**
129
     * @param string $hash
130
     */
131
    public function refreshObjectHash($hash)
132
    {
133
        $this->objectHash = $hash;
134
        $this->validatedSteps = [];
135
    }
136
}
137