Completed
Push — task/configuration-unit-tests ( 6e620b...41a50f )
by Romain
02:14
created

Field::addBehaviour()   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 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\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
        /** @var Form $form */
80
        $form = $this->getFirstParent(Form::class);
81
82
        return $form;
83
    }
84
85
    /**
86
     * @return Validation[]
87
     */
88
    public function getValidation()
89
    {
90
        return $this->validation;
91
    }
92
93
    /**
94
     * @param string $validationName
95
     * @return bool
96
     */
97
    public function hasValidation($validationName)
98
    {
99
        return true === isset($this->validation[$validationName]);
100
    }
101
102
    /**
103
     * @param Validation $validation
104
     */
105
    public function addValidation(Validation $validation)
106
    {
107
        $this->validation[$validation->getName()] = $validation;
108
        $validation->setParents([$this]);
109
    }
110
111
    /**
112
     * @param string $validationName
113
     * @return Validation
114
     * @throws EntryNotFoundException
115
     */
116
    public function getValidationByName($validationName)
117
    {
118
        if (false === $this->hasValidation($validationName)) {
119
            throw EntryNotFoundException::validationNotFound($validationName);
120
        }
121
122
        return $this->validation[$validationName];
123
    }
124
125
    /**
126
     * @return Behaviour[]
127
     */
128
    public function getBehaviours()
129
    {
130
        return $this->behaviours;
131
    }
132
133
    /**
134
     * @param string    $name
135
     * @param Behaviour $behaviour
136
     */
137
    public function addBehaviour($name, Behaviour $behaviour)
138
    {
139
        $this->behaviours[$name] = $behaviour;
140
    }
141
142
    /**
143
     * @return ActivationInterface
144
     */
145
    public function getActivation()
146
    {
147
        return $this->activation;
148
    }
149
150
    /**
151
     * @return bool
152
     */
153
    public function hasActivation()
154
    {
155
        return !($this->activation instanceof EmptyActivation);
156
    }
157
158
    /**
159
     * @param ActivationInterface $activation
160
     */
161
    public function setActivation(ActivationInterface $activation)
162
    {
163
        $activation->setRootObject($this);
164
165
        $this->activation = $activation;
166
    }
167
168
    /**
169
     * @return FieldSettings
170
     */
171
    public function getSettings()
172
    {
173
        return $this->settings;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getName()
180
    {
181
        if (null === $this->name) {
182
            $this->name = $this->getArrayIndex();
183
        }
184
185
        return $this->name;
186
    }
187
188
    /**
189
     * @param string $name
190
     */
191
    public function setName($name)
192
    {
193
        $this->name = $name;
194
    }
195
}
196