Checkbox::create()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.1315
c 0
b 0
f 0
cc 8
nc 12
nop 0
1
<?php
2
3
namespace Faulancer\Form\Type\Base;
4
5
use Faulancer\Form\Type\AbstractType;
6
7
/**
8
 * Class Checkbox
9
 *
10
 * @package Faulancer\Form\Type\Base
11
 * @author Florian Knapp <[email protected]>
12
 */
13
class Checkbox extends AbstractType
14
{
15
16
    /** @var string */
17
    protected $inputType = 'input';
18
19
    /** @var string */
20
    protected $element = '';
21
22
    /**
23
     * @return self
24
     */
25
    public function create()
26
    {
27
        parent::create();
28
29
        $output = '';
30
31
        if (!empty($this->definition['default'])) {
32
33
            $output .= '<' . $this->inputType;
34
            $output .= ' type="hidden"';
35
            $output .= ' name="' . $this->definition['attributes']['name'] . '"';
36
            $output .= ' ' . 'value="' . $this->definition['default'] . '"';
37
            $output .= '/>';
38
39
        }
40
41
        $output .= '<' . $this->inputType;
42
43
        foreach ($this->definition['attributes'] as $attr => $value) {
44
            $output .= ' ' . $attr . '="' . $value . '" ';
45
        }
46
47
        if (!empty($this->getValue()) && $this->definition['attributes']['value'] === $this->getValue()) {
48
            $output .= ' checked="checked"';
49
        } elseif (empty($this->getValue()) && isset($this->definition['checked']) && $this->definition['checked'] === true) {
50
            $output .= ' checked="checked"';
51
        }
52
53
        $output .= '/>';
54
55
        $this->element = $output;
56
57
        return $this;
58
    }
59
60
}