Completed
Push — unit-tests-validation ( 4a3d84...420a0c )
by Romain
02:02
created

Field::addValidation()   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\Configuration\Form\Field;
15
16
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
17
use Romm\ConfigurationObject\Traits\ConfigurationObject\StoreArrayIndexTrait;
18
use Romm\Formz\Configuration\AbstractFormzConfiguration;
19
use Romm\Formz\Configuration\Form\Condition\Activation\ActivationInterface;
20
use Romm\Formz\Configuration\Form\Condition\Activation\EmptyActivation;
21
use Romm\Formz\Configuration\Form\Field\Behaviour\Behaviour;
22
use Romm\Formz\Configuration\Form\Field\Settings\FieldSettings;
23
use Romm\Formz\Configuration\Form\Field\Validation\Validation;
24
use Romm\Formz\Configuration\Form\Form;
25
use Romm\Formz\Exceptions\EntryNotFoundException;
26
27
class Field extends AbstractFormzConfiguration
28
{
29
    use StoreArrayIndexTrait;
30
    use ParentsTrait;
31
32
    /**
33
     * @var \ArrayObject<Romm\Formz\Configuration\Form\Field\Validation\Validation>
34
     */
35
    protected $validation = [];
36
37
    /**
38
     * @var \ArrayObject<Romm\Formz\Configuration\Form\Field\Behaviour\Behaviour>
39
     */
40
    protected $behaviours = [];
41
42
    /**
43
     * @var \Romm\Formz\Configuration\Form\Condition\Activation\ActivationResolver
44
     * @validate Romm.Formz:Internal\ConditionIsValid
45
     */
46
    protected $activation;
47
48
    /**
49
     * @var \Romm\Formz\Configuration\Form\Field\Settings\FieldSettings
50
     */
51
    protected $settings;
52
53
    /**
54
     * Name of the field. By default, it is the key of this field in the array
55
     * containing all the fields for the parent form.
56
     *
57
     * @var string
58
     */
59
    private $fieldName;
60
61
    /**
62
     * Constructor.
63
     */
64
    public function __construct()
65
    {
66
        $this->settings = new FieldSettings();
67
        $this->settings->setParents([$this]);
68
69
        $this->activation = EmptyActivation::get();
70
    }
71
72
    /**
73
     * @return Form
74
     */
75
    public function getForm()
76
    {
77
        return $this->getFirstParent(Form::class);
78
    }
79
80
    /**
81
     * @param string $validationName
82
     * @return bool
83
     */
84
    public function hasValidation($validationName)
85
    {
86
        return true === isset($this->validation[$validationName]);
87
    }
88
89
    /**
90
     * @return Validation[]
91
     */
92
    public function getValidation()
93
    {
94
        return $this->validation;
95
    }
96
97
    /**
98
     * @param Validation $validation
99
     */
100
    public function addValidation(Validation $validation)
101
    {
102
        $this->validation[$validation->getValidationName()] = $validation;
103
    }
104
105
    /**
106
     * @param $validationName
107
     * @return Validation
108
     * @throws EntryNotFoundException
109
     */
110
    public function getValidationByName($validationName)
111
    {
112
        if (false === $this->hasValidation($validationName)) {
113
            throw new EntryNotFoundException(
114
                'The validation "' . $validationName . '" was not found. Please use the function "hasValidation()" befo',
115
                1487672276
116
            );
117
        }
118
119
        return $this->validation[$validationName];
120
    }
121
122
    /**
123
     * @return Behaviour[]
124
     */
125
    public function getBehaviours()
126
    {
127
        return $this->behaviours;
128
    }
129
130
    /**
131
     * @return ActivationInterface
132
     */
133
    public function getActivation()
134
    {
135
        return $this->activation;
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function hasActivation()
142
    {
143
        return !($this->activation instanceof EmptyActivation);
144
    }
145
146
    /**
147
     * @return FieldSettings
148
     */
149
    public function getSettings()
150
    {
151
        return $this->settings;
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    public function getFieldName()
158
    {
159
        if (null === $this->fieldName) {
160
            $this->fieldName = $this->getArrayIndex();
161
        }
162
163
        return $this->fieldName;
164
    }
165
166
    /**
167
     * @param string $fieldName
168
     */
169
    public function setFieldName($fieldName)
170
    {
171
        $this->fieldName = $fieldName;
172
    }
173
}
174