Completed
Push — unit-tests-validation ( de96f6 )
by Romain
02:30
created

Field::getValidationByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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 $validationName
99
     * @return Validation
100
     * @throws EntryNotFoundException
101
     */
102
    public function getValidationByName($validationName)
103
    {
104
        if (false === $this->hasValidation($validationName)) {
105
            throw new EntryNotFoundException(
106
                'The validation "' . $validationName . '" was not found. Please use the function "hasValidation()" befo',
107
                1487672276
108
            );
109
        }
110
111
        return $this->validation[$validationName];
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