Completed
Push — tmp-wip ( afad54...8ffff6 )
by Romain
02:25
created

Validator::deactivateAjaxUsage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
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\Definition\Field\Validation;
15
16
use Romm\ConfigurationObject\Traits\ConfigurationObject\ArrayConversionTrait;
17
use Romm\Formz\Form\Definition\AbstractFormDefinition;
18
use Romm\Formz\Form\Definition\Field\Activation\Activation;
19
use Romm\Formz\Form\Definition\Field\Activation\ActivationInterface;
20
use Romm\Formz\Form\Definition\Field\Activation\ActivationUsageInterface;
21
use Romm\Formz\Form\Definition\Field\Activation\EmptyActivation;
22
use Romm\Formz\Form\Definition\Field\Field;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
class Validator extends AbstractFormDefinition implements ActivationUsageInterface
26
{
27
    use ArrayConversionTrait;
28
29
    /**
30
     * @var string
31
     */
32
    private $name;
33
34
    /**
35
     * @var string
36
     * @validate NotEmpty
37
     * @validate Romm.ConfigurationObject:ClassImplements(interface=TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface)
38
     */
39
    private $className;
40
41
    /**
42
     * @var int
43
     * @validate Number
44
     */
45
    protected $priority;
46
47
    /**
48
     * @var array
49
     */
50
    protected $options = [];
51
52
    /**
53
     * @var \Romm\Formz\Form\Definition\Field\Validation\Message[]
54
     */
55
    protected $messages = [];
56
57
    /**
58
     * @var \Romm\Formz\Form\Definition\Field\Activation\ActivationInterface
59
     * @mixedTypesResolver \Romm\Formz\Form\Definition\Field\Activation\ActivationResolver
60
     * @validate Romm.Formz:Internal\ConditionIsValid
61
     */
62
    protected $activation;
63
64
    /**
65
     * @var bool
66
     */
67
    protected $useAjax = false;
68
69
    /**
70
     * @param string $name
71
     * @param string $className
72
     */
73
    public function __construct($name, $className)
74
    {
75
        $this->name = $name;
76
        $this->className = $className;
77
        $this->activation = EmptyActivation::get();
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getName()
84
    {
85
        return $this->name;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getClassName()
92
    {
93
        return $this->className;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function getPriority()
100
    {
101
        return $this->priority;
102
    }
103
104
    /**
105
     * @param int $priority
106
     */
107
    public function setPriority($priority)
108
    {
109
        $this->checkDefinitionFreezeState();
110
111
        $this->priority = $priority;
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    public function getOptions()
118
    {
119
        return $this->options;
120
    }
121
122
    /**
123
     * @param array $options
124
     */
125
    public function setOptions(array $options)
126
    {
127
        $this->checkDefinitionFreezeState();
128
129
        $this->options = $options;
130
    }
131
132
    /**
133
     * @return Message[]
134
     */
135
    public function getMessages()
136
    {
137
        return $this->messages;
138
    }
139
140
    /**
141
     * @param Message[] $messages
142
     */
143
    public function setMessages(array $messages)
144
    {
145
        $this->checkDefinitionFreezeState();
146
147
        $this->messages = $messages;
148
    }
149
150
    /**
151
     * @return ActivationInterface
152
     */
153
    public function getActivation()
154
    {
155
        if (false === $this->hasActivation()) {
156
            throw new \Exception('todo'); // @todo
157
        }
158
159
        return $this->activation;
160
    }
161
162
    /**
163
     * @return bool
164
     */
165
    public function hasActivation()
166
    {
167
        return !($this->activation instanceof EmptyActivation);
168
    }
169
170
    /**
171
     * @return ActivationInterface
172
     */
173
    public function addActivation()
174
    {
175
        $this->checkDefinitionFreezeState();
176
177
        if ($this->activation instanceof EmptyActivation) {
178
            $this->activation = GeneralUtility::makeInstance(Activation::class);
179
            $this->activation->attachParent($this);
180
        }
181
182
        return $this->activation;
183
    }
184
185
    /**
186
     * @return bool
187
     */
188
    public function doesUseAjax()
189
    {
190
        return (bool)$this->useAjax;
191
    }
192
193
    /**
194
     * Turns the Ajax usage ON.
195
     */
196
    public function activateAjaxUsage()
197
    {
198
        $this->checkDefinitionFreezeState();
199
200
        $this->useAjax = true;
201
    }
202
203
    /**
204
     * Turns the Ajax usage OFF.
205
     */
206
    public function deactivateAjaxUsage()
207
    {
208
        $this->checkDefinitionFreezeState();
209
210
        $this->useAjax = false;
211
    }
212
213
    /**
214
     * @return Field
215
     */
216
    public function getParentField()
217
    {
218
        /** @var Field $field */
219
        $field = $this->getFirstParent(Field::class);
220
221
        return $field;
222
    }
223
}
224