Completed
Push — feature/version-2 ( 61b6a8...2f0885 )
by Romain
03:48 queued 01:55
created

FormDefinition::dataPreProcessor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
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\Form\Definition;
15
16
use Romm\ConfigurationObject\ConfigurationObjectInterface;
17
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait;
18
use Romm\ConfigurationObject\Service\ServiceFactory;
19
use Romm\ConfigurationObject\Traits\ConfigurationObject\ArrayConversionTrait;
20
use Romm\ConfigurationObject\Traits\ConfigurationObject\DefaultConfigurationObjectTrait;
21
use Romm\ConfigurationObject\Traits\ConfigurationObject\StoreArrayIndexTrait;
22
use Romm\Formz\Condition\Items\ConditionItemInterface;
23
use Romm\Formz\Configuration\AbstractFormzConfiguration;
24
use Romm\Formz\Configuration\Configuration;
25
use Romm\Formz\Exceptions\EntryNotFoundException;
26
use Romm\Formz\Form\Definition\Field\Field;
27
use Romm\Formz\Form\Definition\Settings\FormSettings;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
class FormDefinition extends AbstractFormzConfiguration implements ConfigurationObjectInterface
31
{
32
    use DefaultConfigurationObjectTrait;
33
    use StoreArrayIndexTrait;
34
    use ParentsTrait;
35
    use ArrayConversionTrait;
36
37
    /**
38
     * @var \Romm\Formz\Form\Definition\Field\Field[]
39
     * @validate NotEmpty
40
     */
41
    protected $fields = [];
42
43
    /**
44
     * @var \Romm\Formz\Condition\Items\ConditionItemInterface[]
45
     * @mixedTypesResolver \Romm\Formz\Form\Definition\Condition\ConditionItemResolver
46
     */
47
    protected $conditionList = [];
48
49
    /**
50
     * @var \Romm\Formz\Form\Definition\Settings\FormSettings
51
     */
52
    protected $settings;
53
54
    /**
55
     * Constructor.
56
     */
57
    public function __construct()
58
    {
59
        $this->settings = GeneralUtility::makeInstance(FormSettings::class);
60
    }
61
62
    /**
63
     * Will initialize correctly the configuration object settings.
64
     *
65
     * @return ServiceFactory
66
     */
67
    public static function getConfigurationObjectServices()
68
    {
69
        return Configuration::getConfigurationObjectServices();
70
    }
71
72
    /**
73
     * Returns FormZ root configuration object.
74
     *
75
     * @return Configuration
76
     */
77
    public function getRootConfiguration()
78
    {
79
        /** @var Configuration $configuration */
80
        $configuration = $this->getFirstParent(Configuration::class);
81
82
        return $configuration;
83
    }
84
85
    /**
86
     * @return Field[]
87
     */
88
    public function getFields()
89
    {
90
        return $this->fields;
91
    }
92
93
    /**
94
     * @param string $name
95
     * @return bool
96
     */
97
    public function hasField($name)
98
    {
99
        return true === isset($this->fields[$name]);
100
    }
101
102
    /**
103
     * @param string $name
104
     * @return Field
105
     * @throws EntryNotFoundException
106
     */
107
    public function getField($name)
108
    {
109
        if (false === $this->hasField($name)) {
110
            throw EntryNotFoundException::configurationFieldNotFound($name);
111
        }
112
113
        return $this->fields[$name];
114
    }
115
116
    /**
117
     * @param Field $field
118
     */
119
    public function addField(Field $field)
120
    {
121
        $field->setParents([$this]);
0 ignored issues
show
Deprecated Code introduced by
The method Romm\ConfigurationObject...entsTrait::setParents() has been deprecated with message: This function is deprecated and will be removed in v2! Use function `addParents()` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
122
123
        $this->fields[$field->getName()] = $field;
124
    }
125
126
    /**
127
     * @return ConditionItemInterface[]
128
     */
129
    public function getConditionList()
130
    {
131
        return $this->conditionList;
132
    }
133
134
    /**
135
     * @param string                 $name
136
     * @param ConditionItemInterface $condition
137
     */
138
    public function addCondition($name, ConditionItemInterface $condition)
139
    {
140
        $this->conditionList[$name] = $condition;
141
    }
142
143
    /**
144
     * @return FormSettings
145
     */
146
    public function getSettings()
147
    {
148
        return $this->settings;
149
    }
150
}
151