Checkbox   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 14
c 2
b 1
f 0
lcom 2
cbo 2
dl 0
loc 104
ccs 0
cts 49
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A isValid() 0 3 1
A handle() 0 4 1
A isChecked() 0 3 1
A setChecked() 0 9 2
A setAttribute() 0 6 2
A removeAttribute() 0 6 2
A setLabel() 0 4 1
A render() 0 4 1
A getName() 0 3 1
1
<?php
2
3
  namespace Fiv\Form\Element;
4
5
  use Fiv\Form\Elements\DataElementInterface;
6
  use Fiv\Form\FormData;
7
8
  /**
9
   * Generate <input type="submit" /> element
10
   *
11
   * @author Ivan Shcherbak <[email protected]>
12
   */
13
  class Checkbox extends Html implements DataElementInterface {
14
15
    protected $attributes = [
16
      'type' => 'checkbox',
17
    ];
18
19
    /**
20
     * @var string
21
     */
22
    protected $label = '';
23
24
    protected $isChecked = false;
25
26
27
    public function __construct(string $name, string $label = null) {
28
      $this->tag = 'input';
29
30
      $this->setAttribute('name', $name);
31
      if ($label !== null) {
32
        $this->setLabel($label);
33
      }
34
    }
35
36
37
    public function isValid() {
38
      return true;
39
    }
40
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function handle(FormData $data) {
46
      $this->setChecked($data->has($this->getName()));
47
      return $this;
48
    }
49
50
51
    public function isChecked() : bool {
52
      return $this->isChecked;
53
    }
54
55
56
    public function setChecked(bool $isChecked) : self {
57
      $this->isChecked = ($isChecked === true);
58
      if ($this->isChecked) {
59
        $this->setAttribute('checked', 'checked');
60
      } else {
61
        $this->removeAttribute('checked');
62
      }
63
      return $this;
64
    }
65
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function setAttribute($name, $value) {
71
      if ($name == 'checked') {
72
        $this->isChecked = true;
73
      }
74
      return parent::setAttribute($name, $value);
75
    }
76
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function removeAttribute($name) {
82
      if ($name == 'checked') {
83
        $this->isChecked = false;
84
      }
85
      return parent::removeAttribute($name);
86
    }
87
88
89
    /**
90
     * @param string $text
91
     * @return $this
92
     */
93
    public function setLabel(string $text) : self {
94
      $this->label = $text;
95
      return $this;
96
    }
97
98
99
    /**
100
     * @return string
101
     */
102
    public function render() {
103
      $label = static::tag('span', ['class' => 'label-text'], $this->label);
104
      return '<label>' . static::tag($this->tag, $this->attributes) . $label . '</label>';
105
    }
106
107
108
    /**
109
     * @return string
110
     */
111
    public function getName() {
112
      return $this->getAttribute('name');
113
    }
114
115
116
  }