Completed
Push — task/configuration-unit-tests ( 9e3c21...118c43 )
by Romain
02:40
created

Field::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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\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\Field\Activation\ActivationInterface;
20
use Romm\Formz\Configuration\Form\Field\Activation\ActivationUsageInterface;
21
use Romm\Formz\Configuration\Form\Field\Activation\EmptyActivation;
22
use Romm\Formz\Configuration\Form\Field\Behaviour\Behaviour;
23
use Romm\Formz\Configuration\Form\Field\Settings\FieldSettings;
24
use Romm\Formz\Configuration\Form\Field\Validation\Validation;
25
use Romm\Formz\Configuration\Form\Form;
26
use Romm\Formz\Exceptions\EntryNotFoundException;
27
28
class Field extends AbstractFormzConfiguration implements ActivationUsageInterface
29
{
30
    use StoreArrayIndexTrait;
31
    use ParentsTrait;
32
33
    /**
34
     * @var \Romm\Formz\Configuration\Form\Field\Validation\Validation[]
35
     */
36
    protected $validation = [];
37
38
    /**
39
     * @var \Romm\Formz\Configuration\Form\Field\Behaviour\Behaviour[]
40
     */
41
    protected $behaviours = [];
42
43
    /**
44
     * @var ActivationInterface
45
     * @mixedTypesResolver \Romm\Formz\Configuration\Form\Field\Activation\ActivationResolver
46
     * @validate Romm.Formz:Internal\ConditionIsValid
47
     */
48
    protected $activation;
49
50
    /**
51
     * @var \Romm\Formz\Configuration\Form\Field\Settings\FieldSettings
52
     */
53
    protected $settings;
54
55
    /**
56
     * Name of the field. By default, it is the key of this field in the array
57
     * containing all the fields for the parent form.
58
     *
59
     * @var string
60
     */
61
    private $name;
62
63
    /**
64
     * Constructor.
65
     */
66
    public function __construct()
67
    {
68
        $this->settings = new FieldSettings();
69
        $this->settings->setParents([$this]);
70
71
        $this->activation = EmptyActivation::get();
72
    }
73
74
    /**
75
     * @return Form
76
     */
77
    public function getForm()
78
    {
79
        return $this->getFirstParent(Form::class);
80
    }
81
82
    /**
83
     * @return Validation[]
84
     */
85
    public function getValidation()
86
    {
87
        return $this->validation;
88
    }
89
90
    /**
91
     * @param string $validationName
92
     * @return bool
93
     */
94
    public function hasValidation($validationName)
95
    {
96
        return true === isset($this->validation[$validationName]);
97
    }
98
99
    /**
100
     * @param Validation $validation
101
     */
102
    public function addValidation(Validation $validation)
103
    {
104
        $this->validation[$validation->getValidationName()] = $validation;
105
        $validation->setParents([$this]);
106
    }
107
108
    /**
109
     * @param string $validationName
110
     * @return Validation
111
     * @throws EntryNotFoundException
112
     */
113
    public function getValidationByName($validationName)
114
    {
115
        if (false === $this->hasValidation($validationName)) {
116
            throw EntryNotFoundException::validationNotFound($validationName);
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
     * @param ActivationInterface $activation
148
     */
149
    public function setActivation(ActivationInterface $activation)
150
    {
151
        $activation->setRootObject($this);
152
153
        $this->activation = $activation;
154
    }
155
156
    /**
157
     * @return FieldSettings
158
     */
159
    public function getSettings()
160
    {
161
        return $this->settings;
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public function getName()
168
    {
169
        if (null === $this->name) {
170
            $this->name = $this->getArrayIndex();
171
        }
172
173
        return $this->name;
174
    }
175
176
    /**
177
     * @param string $name
178
     */
179
    public function setName($name)
180
    {
181
        $this->name = $name;
182
    }
183
}
184