Completed
Push — middleware-wip ( 5cfd03...f2f782 )
by Romain
05:53
created

Validator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 197
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A getClassName() 0 4 1
A getPriority() 0 4 1
A setPriority() 0 6 1
A getOptions() 0 4 1
A setOptions() 0 6 1
A getMessages() 0 4 1
A setMessages() 0 6 1
A getActivation() 0 8 2
A hasActivation() 0 4 1
A addActivation() 0 11 2
A doesUseAjax() 0 4 1
A activateAjaxUsage() 0 6 1
A deactivateAjaxUsage() 0 6 1
A getParentField() 0 7 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\Form\Definition\Field\Validation;
15
16
use Romm\ConfigurationObject\Traits\ConfigurationObject\ArrayConversionTrait;
17
use Romm\Formz\Exceptions\SilentException;
18
use Romm\Formz\Form\Definition\AbstractFormDefinition;
19
use Romm\Formz\Form\Definition\Field\Activation\Activation;
20
use Romm\Formz\Form\Definition\Field\Activation\ActivationInterface;
21
use Romm\Formz\Form\Definition\Field\Activation\ActivationUsageInterface;
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\Activation
59
     * @validate Romm.Formz:Internal\ConditionIsValid
60
     */
61
    protected $activation;
62
63
    /**
64
     * @var bool
65
     */
66
    protected $useAjax = false;
67
68
    /**
69
     * @param string $name
70
     * @param string $className
71
     */
72
    public function __construct($name, $className)
73
    {
74
        $this->name = $name;
75
        $this->className = $className;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getName()
82
    {
83
        return $this->name;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getClassName()
90
    {
91
        return $this->className;
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    public function getPriority()
98
    {
99
        return $this->priority;
100
    }
101
102
    /**
103
     * @param int $priority
104
     */
105
    public function setPriority($priority)
106
    {
107
        $this->checkDefinitionFreezeState();
108
109
        $this->priority = $priority;
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function getOptions()
116
    {
117
        return $this->options;
118
    }
119
120
    /**
121
     * @param array $options
122
     */
123
    public function setOptions(array $options)
124
    {
125
        $this->checkDefinitionFreezeState();
126
127
        $this->options = $options;
128
    }
129
130
    /**
131
     * @return Message[]
132
     */
133
    public function getMessages()
134
    {
135
        return $this->messages;
136
    }
137
138
    /**
139
     * @param Message[] $messages
140
     */
141
    public function setMessages(array $messages)
142
    {
143
        $this->checkDefinitionFreezeState();
144
145
        $this->messages = $messages;
146
    }
147
148
    /**
149
     * @return ActivationInterface
150
     */
151
    public function getActivation()
152
    {
153
        if (false === $this->hasActivation()) {
154
            throw new SilentException('todo'); // @todo
155
        }
156
157
        return $this->activation;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function hasActivation()
164
    {
165
        return $this->activation instanceof ActivationInterface;
166
    }
167
168
    /**
169
     * @return ActivationInterface
170
     */
171
    public function addActivation()
172
    {
173
        $this->checkDefinitionFreezeState();
174
175
        if (null === $this->activation) {
176
            $this->activation = GeneralUtility::makeInstance(Activation::class);
177
            $this->activation->attachParent($this);
178
        }
179
180
        return $this->activation;
181
    }
182
183
    /**
184
     * @return bool
185
     */
186
    public function doesUseAjax()
187
    {
188
        return (bool)$this->useAjax;
189
    }
190
191
    /**
192
     * Turns the Ajax usage ON.
193
     */
194
    public function activateAjaxUsage()
195
    {
196
        $this->checkDefinitionFreezeState();
197
198
        $this->useAjax = true;
199
    }
200
201
    /**
202
     * Turns the Ajax usage OFF.
203
     */
204
    public function deactivateAjaxUsage()
205
    {
206
        $this->checkDefinitionFreezeState();
207
208
        $this->useAjax = false;
209
    }
210
211
    /**
212
     * @return Field
213
     */
214
    public function getParentField()
215
    {
216
        /** @var Field $field */
217
        $field = $this->getFirstParent(Field::class);
218
219
        return $field;
220
    }
221
}
222