Completed
Push — unit-tests-conditions ( 8c637d...408609 )
by Romain
02:38
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 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\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
26
class Field extends AbstractFormzConfiguration
27
{
28
    use StoreArrayIndexTrait;
29
    use ParentsTrait;
30
31
    /**
32
     * @var \ArrayObject<Romm\Formz\Configuration\Form\Field\Validation\Validation>
33
     */
34
    protected $validation = [];
35
36
    /**
37
     * @var \ArrayObject<Romm\Formz\Configuration\Form\Field\Behaviour\Behaviour>
38
     */
39
    protected $behaviours = [];
40
41
    /**
42
     * @var \Romm\Formz\Configuration\Form\Condition\Activation\ActivationResolver
43
     * @validate Romm.Formz:Internal\ConditionIsValid
44
     */
45
    protected $activation;
46
47
    /**
48
     * @var \Romm\Formz\Configuration\Form\Field\Settings\FieldSettings
49
     */
50
    protected $settings;
51
52
    /**
53
     * Name of the field. By default, it is the key of this field in the array
54
     * containing all the fields for the parent form.
55
     *
56
     * @var string
57
     */
58
    private $fieldName;
59
60
    /**
61
     * Constructor.
62
     */
63
    public function __construct()
64
    {
65
        $this->settings = new FieldSettings();
66
        $this->settings->setParents([$this]);
67
68
        $this->activation = EmptyActivation::get();
69
    }
70
71
    /**
72
     * @return Form
73
     */
74
    public function getForm()
75
    {
76
        return $this->getFirstParent(Form::class);
77
    }
78
79
    /**
80
     * @param string $validationName
81
     * @return bool
82
     */
83
    public function hasValidation($validationName)
84
    {
85
        return true === isset($this->validation[$validationName]);
86
    }
87
88
    /**
89
     * @param string|null $validationName If given, will try to fetch the validation with the given name.
90
     * @return Validation|Validation[]
91
     */
92
    public function getValidation($validationName = null)
93
    {
94
        $result = $this->validation;
95
96
        if (null !== $validationName) {
97
            $result = (true === isset($this->validation[$validationName]))
98
                ? $this->validation[$validationName]
99
                : null;
100
        }
101
102
        return $result;
103
    }
104
105
    /**
106
     * @param string     $validationName
107
     * @param Validation $validation
108
     */
109
    public function addValidation($validationName, Validation $validation)
110
    {
111
        $this->validation[$validationName] = $validation;
112
    }
113
114
    /**
115
     * @return Behaviour[]
116
     */
117
    public function getBehaviours()
118
    {
119
        return $this->behaviours;
120
    }
121
122
    /**
123
     * @return ActivationInterface
124
     */
125
    public function getActivation()
126
    {
127
        return $this->activation;
128
    }
129
130
    /**
131
     * @return bool
132
     */
133
    public function hasActivation()
134
    {
135
        return !($this->activation instanceof EmptyActivation);
136
    }
137
138
    /**
139
     * @return FieldSettings
140
     */
141
    public function getSettings()
142
    {
143
        return $this->settings;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getFieldName()
150
    {
151
        if (null === $this->fieldName) {
152
            $this->fieldName = $this->getArrayIndex();
153
        }
154
155
        return $this->fieldName;
156
    }
157
158
    /**
159
     * @param string $fieldName
160
     */
161
    public function setFieldName($fieldName)
162
    {
163
        $this->fieldName = $fieldName;
164
    }
165
}
166