Completed
Push — feature/api ( 725c17...de88d6 )
by Laurent
01:40
created

Form   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 229
rs 10
c 0
b 0
f 0
wmc 29
lcom 1
cbo 2

15 Methods

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