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

FormResult::markFieldAsValidated()   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\Configuration\Form\Field\Field;
17
use Romm\Formz\Configuration\Form\Field\Validation\Validation;
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
     * @var Validation[]
36
     */
37
    protected $deactivatedFieldsValidation = [];
38
39
    /**
40
     * @var array
41
     */
42
    protected $validatedFields = [];
43
44
    /**
45
     * Flags the given field as deactivated.
46
     *
47
     * @param Field $field
48
     */
49
    public function deactivateField(Field $field)
50
    {
51
        $this->deactivatedFields[$field->getName()] = $field;
52
    }
53
54
    /**
55
     * Returns true if the given field is flagged as deactivated.
56
     *
57
     * @param Field $field
58
     * @return bool
59
     */
60
    public function fieldIsDeactivated(Field $field)
61
    {
62
        return array_key_exists($field->getName(), $this->deactivatedFields);
63
    }
64
65
    /**
66
     * @return Field[]
67
     */
68
    public function getDeactivatedFields()
69
    {
70
        return $this->deactivatedFields;
71
    }
72
73
    /**
74
     * @param Validation $validation
75
     */
76
    public function deactivateValidation(Validation $validation)
77
    {
78
        $fieldName = $validation->getParentField()->getName();
79
80
        if (false === isset($this->deactivatedFieldsValidation[$fieldName])) {
81
            $this->deactivatedFieldsValidation[$fieldName] = [];
82
        }
83
84
        $this->deactivatedFieldsValidation[$fieldName][$validation->getName()] = $validation;
85
    }
86
87
    /**
88
     * @param Validation $validation
89
     * @return bool
90
     */
91
    public function validationIsDeactivated(Validation $validation)
92
    {
93
        $fieldName = $validation->getParentField()->getName();
94
95
        return array_key_exists($fieldName, $this->deactivatedFieldsValidation)
96
            && array_key_exists($validation->getName(), $this->deactivatedFieldsValidation[$fieldName]);
97
    }
98
99
    /**
100
     * @return Validation[]
101
     */
102
    public function getDeactivatedValidations()
103
    {
104
        return $this->deactivatedFieldsValidation;
105
    }
106
107
    /**
108
     * @param Field $field
109
     */
110
    public function markFieldAsValidated(Field $field)
111
    {
112
        $this->validatedFields[$field->getName()] = true;
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    public function getValidatedFields()
119
    {
120
        return array_keys($this->validatedFields);
121
    }
122
}
123