Completed
Push — wip/steps ( 8555f4...0e4101 )
by Romain
03:24
created

FormResult::markFieldOutOfScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\Error;
15
16
use Romm\Formz\Form\Definition\Field\Field;
17
use Romm\Formz\Form\Definition\Field\Validation\Validator;
18
use Romm\Formz\Service\Traits\StoreDataTrait;
19
use TYPO3\CMS\Extbase\Error\Result;
20
21
/**
22
 * Result used when validating a form instance; it provides more features than
23
 * the basic Extbase `Result` instance.
24
 */
25
class FormResult extends Result
26
{
27
    use StoreDataTrait;
28
29
    /**
30
     * @var Field[]
31
     */
32
    protected $deactivatedFields = [];
33
34
    /**
35
     * Contains fields that are not currently shown, they can be in a different
36
     * step or substep.
37
     *
38
     * @var Field[]
39
     */
40
    protected $fieldsOutOfScope = [];
41
42
    /**
43
     * @var Validator[]
44
     */
45
    protected $deactivatedFieldsValidators = [];
46
47
    /**
48
     * @var array
49
     */
50
    protected $validatedFields = [];
51
52
    /**
53
     * Flags the given field as deactivated: its activation conditions did not
54
     * match.
55
     *
56
     * @param Field $field
57
     */
58
    public function deactivateField(Field $field)
59
    {
60
        $this->deactivatedFields[$field->getName()] = $field;
61
    }
62
63
    /**
64
     * Returns true if the given field is deactivated.
65
     *
66
     * @param Field $field
67
     * @return bool
68
     */
69
    public function fieldIsDeactivated(Field $field)
70
    {
71
        return array_key_exists($field->getName(), $this->deactivatedFields);
72
    }
73
74
    /**
75
     * @param Field $field
76
     */
77
    public function markFieldOutOfScope(Field $field)
78
    {
79
        $this->fieldsOutOfScope[$field->getName()] = $field;
80
    }
81
82
    /**
83
     * @param Field $field
84
     * @return bool
85
     */
86
    public function fieldIsOutOfScope(Field $field)
87
    {
88
        return array_key_exists($field->getName(), $this->fieldsOutOfScope);
89
    }
90
91
    /**
92
     * @return Field[]
93
     */
94
    public function getDeactivatedFields()
95
    {
96
        return $this->deactivatedFields;
97
    }
98
99
    /**
100
     * @param Validator $validator
101
     */
102
    public function deactivateValidator(Validator $validator)
103
    {
104
        $fieldName = $validator->getParentField()->getName();
105
106
        if (false === isset($this->deactivatedFieldsValidators[$fieldName])) {
107
            $this->deactivatedFieldsValidators[$fieldName] = [];
108
        }
109
110
        $this->deactivatedFieldsValidators[$fieldName][$validator->getName()] = $validator;
111
    }
112
113
    /**
114
     * @param Validator $validator
115
     * @return bool
116
     */
117
    public function validatorIsDeactivated(Validator $validator)
118
    {
119
        $fieldName = $validator->getParentField()->getName();
120
121
        return array_key_exists($fieldName, $this->deactivatedFieldsValidators)
122
            && array_key_exists($validator->getName(), $this->deactivatedFieldsValidators[$fieldName]);
123
    }
124
125
    /**
126
     * @return Validator[]
127
     */
128
    public function getDeactivatedValidators()
129
    {
130
        return $this->deactivatedFieldsValidators;
131
    }
132
133
    /**
134
     * @param Field $field
135
     */
136
    public function markFieldAsValidated(Field $field)
137
    {
138
        $this->validatedFields[$field->getName()] = true;
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    public function getValidatedFields()
145
    {
146
        return array_keys($this->validatedFields);
147
    }
148
}
149