Completed
Push — tmp-wip ( 51e3a7...57b785 )
by Romain
03:04
created

Validator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 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 4 1
A hasActivation() 0 4 1
A addActivation() 0 12 2
A doesUseAjax() 0 4 1
A activateAjaxUsage() 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\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
        return $this->activation;
156
    }
157
158
    /**
159
     * @return bool
160
     */
161
    public function hasActivation()
162
    {
163
        return !($this->activation instanceof EmptyActivation);
164
    }
165
166
    /**
167
     * @return ActivationInterface
168
     */
169
    public function addActivation()
170
    {
171
        $this->checkDefinitionFreezeState();
172
173
        if ($this->activation instanceof EmptyActivation) {
174
            $this->activation = GeneralUtility::makeInstance(Activation::class);
175
            $this->activation->setParents([$this]);
176
            \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->activation->getRootObject(), 'ROOT VALIDATION');
177
        }
178
179
        return $this->activation;
180
    }
181
182
    /**
183
     * @return bool
184
     */
185
    public function doesUseAjax()
186
    {
187
        return (bool)$this->useAjax;
188
    }
189
190
    /**
191
     * @param bool $flag
192
     */
193
    public function activateAjaxUsage($flag = true)
194
    {
195
        $this->checkDefinitionFreezeState();
196
197
        $this->useAjax = (bool)$flag;
198
    }
199
200
    /**
201
     * @return Field
202
     */
203
    public function getParentField()
204
    {
205
        /** @var Field $field */
206
        $field = $this->getFirstParent(Field::class);
207
208
        return $field;
209
    }
210
}
211