Completed
Push — master ( de703e...a2c256 )
by Shcherbak
06:30 queued 10s
created

Form::setMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
4
  namespace Fiv\Form;
5
6
  use Fiv\Form\Element;
7
  use Fiv\Form\Element\Submit;
8
9
  /**
10
   * @method Form setAction($action);
11
   * @method string|null getAction();
12
   *
13
   * @author Ivan Shcherbak <[email protected]>
14
   */
15
  class Form extends Element\Html {
16
17
    /**
18
     * @var null|string
19
     */
20
    protected $uid = null;
21
22
    /**
23
     * @var boolean|null
24
     */
25
    protected $validationResult = null;
26
27
    /**
28
     * @var array|null
29
     */
30
    protected $data = null;
31
32
    /**
33
     * @var Element\Base[]
34
     */
35
    protected $elements = [];
36
37
    /**
38
     * Default form attributes
39
     *
40
     * @var array
41
     */
42
    protected $attributes = [
43
      'method' => 'post',
44
    ];
45
46
47
    /**
48
     * @param $name
49
     * @return $this
50
     */
51 4
    public function setName($name) {
52 4
      $this->uid = $name;
53 4
      $this->attributes['name'] = $name;
54 4
      return $this;
55
    }
56
57
58
    /**
59
     * @return array
60
     * @throws \Exception
61
     */
62 9
    public function getData() {
63 9
      if (!isset($this->data)) {
64 1
        throw new \Exception("Data does not exist!");
65
      }
66
67 8
      return $this->data;
68
    }
69
70
71
    /**
72
     * @param array|\Iterator $data
73
     * @throws \Exception
74
     */
75 9
    public function setData($data) {
76 9
      if ($data instanceof \Iterator) {
77
        $data = iterator_to_array($data);
78
      }
79
80 9
      if (!is_array($data)) {
81 1
        throw new \Exception('Data should be an array');
82
      }
83
84
85 8
      $this->cleanValidationFlag();
86
87 8
      foreach ($this->elements as $element) {
88 7
        $name = $element->getName();
89
90 7
        if ($element instanceof Checkbox) {
91
          $data[$name] = isset($data[$name]) ? 1 : 0;
92 7
        } elseif ($element instanceof CheckboxList) {
93
          $data[$name] = isset($data[$name]) ? $data[$name] : [];
94
        }
95
96 7
        if (array_key_exists($name, $data)) {
97 6
          $element->setValue($data[$name]);
98 7
          $data[$name] = $element->getValue();
99
        }
100
101
      }
102
103 8
      $this->data = $data;
104 8
    }
105
106
107
    /**
108
     * @deprecated
109
     * @todo remove method
110
     */
111
    public function prepare() {
112
      return;
113
    }
114
115
116
    /**
117
     * @return string
118
     */
119 2
    public function getMethod() {
120 2
      if (!empty($this->attributes['method'])) {
121 2
        return strtolower($this->attributes['method']);
122
      }
123
124
      return false;
125
    }
126
127
128
    /**
129
     * @param string $method
130
     * @return $this
131
     */
132 2
    public function setMethod($method) {
133 2
      $this->attributes['method'] = $method;
134
135 2
      return $this;
136
    }
137
138
139
    /**
140
     *
141
     */
142 13
    protected function cleanValidationFlag() {
143 13
      $this->validationResult = null;
144 13
    }
145
146
147
    /**
148
     * Check if form is submitted and all elements are valid
149
     *
150
     * @return bool|null
151
     */
152 7
    public function isValid() {
153 7
      if ($this->validationResult !== null) {
154
        return $this->validationResult;
155
      }
156
157 7
      if (!$this->isSubmitted()) {
158 1
        return false;
159
      }
160
161 6
      $this->validationResult = true;
162 6
      foreach ($this->elements as $element) {
163 6
        if (!$element->validate()) {
164 6
          $this->validationResult = false;
165
        }
166
      }
167
168 6
      return $this->validationResult;
169
    }
170
171
172
    /**
173
     * Check if form is submitted
174
     *
175
     * @return bool
176
     */
177 9
    public function isSubmitted() {
178 9
      $data = $this->getData();
179
180 8
      return isset($data[$this->getUid()]);
181
    }
182
183
184
    /**
185
     * Return unique id of form
186
     *
187
     * @return string
188
     */
189 10
    public function getUid() {
190 10
      if (empty($this->uid)) {
191 10
        $this->uid = md5(get_called_class());
192
      }
193
194 10
      return $this->uid;
195
    }
196
197
198
    /**
199
     * @return \Fiv\Form\Element\Base[]
200
     */
201 1
    public function getElements() {
202 1
      return $this->elements;
203
    }
204
205
206
    /**
207
     * @param string $name
208
     * @param string|null $text
209
     * @return \Fiv\Form\Element\Input
210
     */
211 6
    public function input($name, $text = null) {
212 6
      $input = new Element\Input();
213 6
      $input->setName($name);
214 6
      $input->setText($text);
215 6
      $input->setType('text');
216 6
      $this->setElement($input);
217 6
      return $input;
218
    }
219
220
221
    /**
222
     * @param string $name
223
     * @param string|null $text
224
     * @return \Fiv\Form\Element\Input
225
     */
226
    public function password($name, $text = null) {
227
      $input = new Element\Password();
228
      $input->setName($name);
229
      $input->setText($text);
230
      $this->setElement($input);
231
      return $input;
232
    }
233
234
235
    /**
236
     * @param      $name
237
     * @param null $text
238
     * @return Select
239
     */
240
    public function select($name, $text = null) {
241
      $select = new Select();
242
      $select->setName($name);
243
      $select->setText($text);
244
      $this->setElement($select);
245
      return $select;
246
    }
247
248
249
    /**
250
     * @param        $name
251
     * @param string $text
252
     * @return RadioList
253
     */
254
    public function radioList($name, $text = null) {
255
      $radio = new RadioList();
256
      $radio->setName($name);
257
      $radio->setText($text);
258
      $this->setElement($radio);
259
      return $radio;
260
    }
261
262
263
    /**
264
     * @param      $name
265
     * @param null $text
266
     * @return TextArea
267
     */
268 3
    public function textarea($name, $text = null) {
269 3
      $input = new TextArea();
270 3
      $input->setName($name);
271 3
      $input->setText($text);
272 3
      $this->setElement($input);
273 3
      return $input;
274
    }
275
276
277
    /**
278
     * ```
279
     * $form->hidden('key', md5($this-user->id . HASH_A);
280
     * ```
281
     * @param      $name
282
     * @param null $value
283
     * @return \Fiv\Form\Element\Input
284
     */
285 2
    public function hidden($name, $value = null) {
286 2
      $hidden = new  \Fiv\Form\Element\Input();
287 2
      $hidden->setType('hidden');
288 2
      $hidden->setName($name);
289 2
      $hidden->setValue($value);
290 2
      $this->setElement($hidden);
291 2
      return $hidden;
292
    }
293
294
295
    /**
296
     * ```
297
     * $form->submit('register', 'зареєструватись');
298
     * ```
299
     * @param      $name
300
     * @param null $value
301
     * @return Submit
302
     */
303 1
    public function submit($name, $value = null) {
304 1
      $input = new Submit();
305 1
      $input->setName($name);
306 1
      $input->setValue($value);
307 1
      $this->setElement($input);
308 1
      return $input;
309
    }
310
311
312
    /**
313
     * ```
314
     * $form->checkbox('subscribe', 'Підписка на новини');
315
     * ```
316
     * @param string $name
317
     * @param string|null $label
318
     * @return Checkbox
319
     */
320
    public function checkbox($name, $label = null) {
321
      $checkbox = new Checkbox();
322
      $checkbox->setName($name);
323
      $checkbox->setLabel($label);
324
      $this->setElement($checkbox);
325
      return $checkbox;
326
    }
327
328
329
    /**
330
     * @param string $name
331
     * @param null $text
332
     * @return CheckboxList
333
     */
334
    public function checkboxList($name, $text = null) {
335
      $checkbox = new CheckboxList();
336
      $checkbox->setName($name);
337
      $checkbox->setText($text);
338
      $this->setElement($checkbox);
339
      return $checkbox;
340
    }
341
342
343
    /**
344
     * Connect element to block and to form
345
     *
346
     * @param Element\Base $element
347
     * @return $this
348
     */
349 12
    protected function setElement(\Fiv\Form\Element\Base $element) {
350 12
      $this->cleanValidationFlag();
351 12
      $this->elements[$element->getName()] = $element;
352 12
      return $this;
353
    }
354
355
356
    /**
357
     * Render full form
358
     *
359
     * @return string
360
     */
361 1
    public function render() {
362 1
      return $this->renderStart() . $this->renderElements() . $this->renderEnd();
363
    }
364
365
366
    /**
367
     * You can easy rewrite this method for custom design of your forms
368
     *
369
     * @return string
370
     */
371 1
    protected function renderElements() {
372 1
      $formHtml = '<dl>';
373
374 1
      foreach ($this->elements as $element) {
375
        $formHtml .=
376
          '<dt>' . $element->getText() . '</dt>' .
377
          '<dd>' . $element->render() . '</dd>';
378
      }
379
380 1
      $formHtml .= '</dl>';
381 1
      return $formHtml;
382
    }
383
384
385
    /**
386
     * @return string
387
     */
388 1
    public function renderStart() {
389 1
      $hidden = new Element\Input();
390 1
      $hidden->setType('hidden');
391 1
      $hidden->addAttributes([
392 1
        'name' => $this->getUid(),
393
      ]);
394 1
      $hidden->setValue(1);
395
396
397
      # get default attribute
398 1
      $method = $this->getMethod();
399 1
      $this->setAttribute('method', $method);
0 ignored issues
show
Security Bug introduced by
It seems like $method defined by $this->getMethod() on line 398 can also be of type false; however, Fiv\Form\Element\Html::setAttribute() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
400
401 1
      return '<form ' . $this->getAttributesAsString() . '>' . $hidden->render();
402
    }
403
404
405
    /**
406
     * @return string
407
     */
408 1
    public function renderEnd() {
409 1
      return '</form> ';
410
    }
411
412
  }