Completed
Push — feature/update_flight ( 01d55d...a5f5fd )
by Laurent
01:55
created

Form::getErrorMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
        return $this->options;
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88
    public function add(FormElementInterface $element)
89
    {
90
        Assert::keyNotExists($this->elements, $element->getName(), 'Element already exists');
91
        $this->elements[$element->getName()] = $element;
92
93
        return $this;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function validate()
100
    {
101
        if(!$this->validator){
102
            return true;
103
        }
104
105
        if(null === $this->object){
106
            throw new \InvalidArgumentException('Object not bound');
107
        }
108
109
        $validation = $this->validator->isValid($this->object, $_REQUEST);
110
        
111
        if(!$validation){
112
            foreach($this->elements as $fieldName => $field){
113
                $field->setErrors($this->validator->getError($fieldName));
114
            }
115
        }
116
117
        return $validation;
118
    }
119
120
    /**
121
     * @return array|string[]
122
     */
123
    public function getErrorMessages(){
124
        return $this->validator->getErrors();
125
    }
126
127
    /**
128
     * @param null|\ValidatorInterface $validator
129
     *
130
     * @return Form
131
     */
132
    protected function setValidator(ValidatorInterface $validator)
133
    {
134
        $this->validator = $validator;
135
        return $this;
136
    }
137
138
139
    /**
140
     * @inheritDoc
141
     */
142
    public function bind($object)
143
    {
144
        foreach($this->elements as $element){
145
            $name = $this->camelCase($element->getName());
146
            $methodName = 'get'.$name;
147
            if(!method_exists($object, $methodName)){
148
                continue;
149
            }
150
151
            $element->setValue($object->{$methodName}());
152
        }
153
154
        $this->object = $object;
155
156
        return $this;
157
    }
158
159
    /**
160
     * @inheritDoc
161
     */
162
    public function setData(array $data)
163
    {
164
        foreach($data as $fieldName => $currentData){
165
            if(!key_exists($fieldName, $this->elements) || $this->elements[$fieldName]->isDisabled()){
166
                continue;
167
            }
168
169
            $this->elements[$fieldName]->setValue($currentData);
170
171
            $methodName = 'set'.$this->camelCase($fieldName);
172
            if(null === $this->object){
173
                continue;
174
            }
175
176
            if(method_exists($this->object, $methodName)){
177
                $this->object->{$methodName}($currentData);
178
                continue;
179
            }
180
181
            if(property_exists($this->object, $fieldName)){
182
                $this->object->{$fieldName} = $currentData;
183
                continue;
184
            }
185
        }
186
187
        return $this;
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193
    public function getObject()
194
    {
195
        return $this->object;
196
    }
197
198
    /**
199
     * @inheritDoc
200
     */
201
    public function getElement($elementName)
202
    {
203
        if(!key_exists($elementName, $this->elements)){
204
            throw new \InvalidArgumentException(sprintf('Element %s not found ', $elementName));
205
        }
206
207
        return $this->elements[$elementName];
208
    }
209
210
    /**
211
     * @param string $name
212
     *
213
     * @return string
214
     */
215
    private function camelCase($name)
216
    {
217
        $str = str_replace('_', '', ucwords($name, '-'));
218
        return  lcfirst($str);
219
    }
220
221
}