Completed
Push — tmp-wip ( 51e3a7 )
by Romain
02:45
created

Field::addValidator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
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;
15
16
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
17
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
18
use Romm\Formz\Exceptions\EntryNotFoundException;
19
use Romm\Formz\Form\Definition\AbstractFormDefinition;
20
use Romm\Formz\Form\Definition\Field\Activation\Activation;
21
use Romm\Formz\Form\Definition\Field\Activation\ActivationInterface;
22
use Romm\Formz\Form\Definition\Field\Activation\ActivationUsageInterface;
23
use Romm\Formz\Form\Definition\Field\Activation\EmptyActivation;
24
use Romm\Formz\Form\Definition\Field\Behaviour\Behaviour;
25
use Romm\Formz\Form\Definition\Field\Settings\FieldSettings;
26
use Romm\Formz\Form\Definition\Field\Validation\Validator;
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29
class Field extends AbstractFormDefinition implements ActivationUsageInterface, DataPreProcessorInterface
0 ignored issues
show
Bug introduced by
There is one abstract method setActivation in this class; you could implement it, or declare this class as abstract.
Loading history...
30
{
31
    /**
32
     * @var string
33
     */
34
    private $name;
35
36
    /**
37
     * @var \Romm\Formz\Form\Definition\Field\Validation\Validator[]
38
     */
39
    protected $validation = [];
40
41
    /**
42
     * @var \Romm\Formz\Form\Definition\Field\Behaviour\Behaviour[]
43
     */
44
    protected $behaviours = [];
45
46
    /**
47
     * @var \Romm\Formz\Form\Definition\Field\Activation\ActivationInterface
48
     * @mixedTypesResolver \Romm\Formz\Form\Definition\Field\Activation\ActivationResolver
49
     * @validate Romm.Formz:Internal\ConditionIsValid
50
     */
51
    protected $activation;
52
53
    /**
54
     * @var \Romm\Formz\Form\Definition\Field\Settings\FieldSettings
55
     */
56
    protected $settings;
57
58
    /**
59
     * @param string $name
60
     */
61
    public function __construct($name)
62
    {
63
        $this->name = $name;
64
65
        $this->settings = new FieldSettings;
66
        $this->settings->setParents([$this]);
67
68
        $this->activation = EmptyActivation::get();
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getName()
75
    {
76
        return $this->name;
77
    }
78
79
    /**
80
     * @return Validator[]
81
     */
82
    public function getValidators()
83
    {
84
        return $this->validation;
85
    }
86
87
    /**
88
     * @param string $name
89
     * @return bool
90
     */
91
    public function hasValidator($name)
92
    {
93
        return true === isset($this->validation[$name]);
94
    }
95
96
    /**
97
     * @param string $name
98
     * @return Validator
99
     * @throws EntryNotFoundException
100
     */
101
    public function getValidator($name)
102
    {
103
        if (false === $this->hasValidator($name)) {
104
            throw EntryNotFoundException::validatorNotFound($name);
0 ignored issues
show
Bug introduced by
The method validatorNotFound() does not exist on Romm\Formz\Exceptions\EntryNotFoundException. Did you maybe mean validationNotFound()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
105
        }
106
107
        return $this->validation[$name];
108
    }
109
110
    /**
111
     * @param string $name
112
     * @param string $className
113
     * @return Validator
114
     */
115
    public function addValidator($name, $className)
116
    {
117
        $this->checkDefinitionFreezeState();
118
119
        if ($this->hasValidator($name)) {
120
            throw new \Exception('todo'); // @todo
121
        }
122
123
        /** @var Validator $validator */
124
        $validator = GeneralUtility::makeInstance(Validator::class, $name, $className);
125
126
        $validator->setParents([$this]);
127
        $this->validation[$name] = $validator;
128
129
        return $validator;
130
    }
131
132
    /**
133
     * @return Behaviour[]
134
     */
135
    public function getBehaviours()
136
    {
137
        return $this->behaviours;
138
    }
139
140
    /**
141
     * @param string    $name
142
     * @param Behaviour $behaviour
143
     */
144
    public function addBehaviour($name, Behaviour $behaviour)
145
    {
146
        $this->behaviours[$name] = $behaviour;
147
    }
148
149
    /**
150
     * @return ActivationInterface
151
     */
152
    public function getActivation()
153
    {
154
        return $this->activation;
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function hasActivation()
161
    {
162
        return !($this->activation instanceof EmptyActivation);
163
    }
164
165
    /**
166
     * @return ActivationInterface
167
     */
168
    public function addActivation()
169
    {
170
        $this->checkDefinitionFreezeState();
171
172
        if ($this->activation instanceof EmptyActivation) {
173
            $this->activation = GeneralUtility::makeInstance(Activation::class);
174
            $this->activation->setParents([$this]);
175
            
176
            \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->activation->getRootObject(), 'ROOT FIELD');
177
        }
178
179
        return $this->activation;
180
    }
181
182
    /**
183
     * @return FieldSettings
184
     */
185
    public function getSettings()
186
    {
187
        return $this->settings;
188
    }
189
190
    /**
191
     * @param DataPreProcessor $processor
192
     */
193
    public static function dataPreProcessor(DataPreProcessor $processor)
194
    {
195
        $data = $processor->getData();
196
197
        /*
198
         * Forcing the names of the validations: they are the keys of the array
199
         * entries.
200
         */
201
        foreach ($data['validation'] as $key => $field) {
202
            $data['validation'][$key]['name'] = $key;
203
        }
204
205
        $processor->setData($data);
206
    }
207
}
208