Completed
Push — master ( 3c5c0f...ec5ff6 )
by Laurent
01:40
created

Form::setData()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 6
nop 1
dl 0
loc 27
rs 8.5546
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 */
5
6
namespace flightlog\form;
7
8
use ValidatorInterface;
9
use Webmozart\Assert\Assert;
10
11
/**
12
 * @author Laurent De Coninck <[email protected]>
13
 */
14
abstract class Form implements FormInterface
15
{
16
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * @var string
24
     */
25
    private $method;
26
27
    /**
28
     * @var array
29
     */
30
    private $options;
31
32
    /**
33
     * @var \stdClass|null
34
     */
35
    private $object;
36
37
    /**
38
     * @var null|\ValidatorInterface
39
     */
40
    private $validator;
41
42
    /**
43
     * @var array|FormElementInterface[]
44
     */
45
    private $elements;
46
47
    /**
48
     * Form constructor.
49
     *
50
     * @param string $name
51
     * @param string $method
52
     * @param array  $options
53
     */
54
    public function __construct($name, $method = FormInterface::METHOD_GET, array $options = [])
55
    {
56
        $this->name = $name;
57
        $this->method = $method;
58
        $this->options = $options;
59
        $this->elements = [];
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function getName()
66
    {
67
        return $this->name;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function getMethod()
74
    {
75
        return $this->method;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function getOptions()
82
    {
83
        return $this->options;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function add(FormElementInterface $element)
90
    {
91
        Assert::keyNotExists($this->elements, $element->getName(), 'Element already exists');
92
        $this->elements[$element->getName()] = $element;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function remove($fieldName)
101
    {
102
        unset($this->elements[$fieldName]);
103
        return $this;
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function has($fieldName)
110
    {
111
        return isset($this->elements[$fieldName]);
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function validate()
118
    {
119
        if (!$this->validator) {
120
            return true;
121
        }
122
123
        if (null === $this->object) {
124
            throw new \InvalidArgumentException('Object not bound');
125
        }
126
127
        $validation = $this->validator->isValid($this->object, $_REQUEST);
128
129
        if (!$validation) {
130
            foreach ($this->elements as $fieldName => $field) {
131
                $field->setErrors($this->validator->getError($fieldName));
132
            }
133
        }
134
135
        return $validation;
136
    }
137
138
    /**
139
     * @return array|string[]
140
     */
141
    public function getErrorMessages()
142
    {
143
        return $this->validator->getErrors();
144
    }
145
146
    /**
147
     * @param null|\ValidatorInterface $validator
148
     *
149
     * @return Form
150
     */
151
    protected function setValidator(ValidatorInterface $validator)
152
    {
153
        $this->validator = $validator;
154
        return $this;
155
    }
156
157
158
    /**
159
     * @inheritDoc
160
     */
161
    public function bind($object)
162
    {
163
        foreach ($this->elements as $element) {
164
            $name = $this->camelCase($element->getName());
165
            $methodName = 'get' . $name;
166
            if (!method_exists($object, $methodName)) {
167
                continue;
168
            }
169
170
            $element->setValue($object->{$methodName}());
171
        }
172
173
        $this->object = $object;
174
175
        return $this;
176
    }
177
178
    /**
179
     * @inheritDoc
180
     */
181
    public function setData(array $data)
182
    {
183
        foreach ($data as $fieldName => $currentData) {
184
            if (!key_exists($fieldName, $this->elements) || $this->elements[$fieldName]->isDisabled()) {
185
                continue;
186
            }
187
188
            $this->elements[$fieldName]->setValue($currentData);
189
190
            $methodName = 'set' . $this->camelCase($fieldName);
191
            if (null === $this->object) {
192
                continue;
193
            }
194
195
            if (method_exists($this->object, $methodName)) {
196
                $this->object->{$methodName}($currentData);
197
                continue;
198
            }
199
200
            if (property_exists($this->object, $fieldName)) {
201
                $this->object->{$fieldName} = $currentData;
202
                continue;
203
            }
204
        }
205
206
        return $this;
207
    }
208
209
    /**
210
     * @inheritDoc
211
     */
212
    public function getObject()
213
    {
214
        return $this->object;
215
    }
216
217
    /**
218
     * @inheritDoc
219
     */
220
    public function getElement($elementName)
221
    {
222
        if (!key_exists($elementName, $this->elements)) {
223
            throw new \InvalidArgumentException(sprintf('Element %s not found ', $elementName));
224
        }
225
226
        return $this->elements[$elementName];
227
    }
228
229
    /**
230
     * @param string $name
231
     *
232
     * @return string
233
     */
234
    private function camelCase($name)
235
    {
236
        $str = str_replace('_', '', ucwords($name, '-'));
237
        return lcfirst($str);
238
    }
239
240
}