Completed
Push — master ( 768def...b15d59 )
by Shcherbak
9s
created

Checkbox::check()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
  namespace Fiv\Form\Element;
4
5
  use Fiv\Form\Element;
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 Element\Input {
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 1
    public function isValid() {
28 1
      return true;
29
    }
30
31
32
    /**
33
     * @inheritdoc
34
     */
35 8
    public function handle(FormData $data) {
36 8
      $this->setChecked($data->has($this->getName()));
37 8
      return $this;
38
    }
39
40
41
    /**
42
     * @return bool
43
     */
44 7
    public function isChecked() {
45 7
      return $this->isChecked;
46
    }
47
48
49
    /**
50
     * @param bool $isChecked
51
     * @return $this
52
     */
53 8
    public function setChecked($isChecked) {
54 8
      $this->isChecked = ($isChecked === true);
55 8
      if ($this->isChecked) {
56 8
        $this->setAttribute('checked', 'checked');
57
      } else {
58 2
        $this->removeAttribute('checked');
59
      }
60 8
      return $this;
61
    }
62
63
64
    /**
65
     * @inheritdoc
66
     */
67 9
    public function setAttribute($name, $value) {
68 9
      if ($name == 'checked') {
69 8
        $this->isChecked = true;
70
      }
71 9
      return parent::setAttribute($name, $value);
72
    }
73
74
75
    /**
76
     * @inheritdoc
77
     */
78 2
    public function removeAttribute($name) {
79 2
      if ($name == 'checked') {
80 2
        $this->isChecked = false;
81
      }
82 2
      return parent::removeAttribute($name);
83
    }
84
85
86
    /**
87
     * @param string $text
88
     * @return $this
89
     */
90
    public function setLabel($text) {
91
      $this->label = $text;
92
      return $this;
93
    }
94
95
96
    /**
97
     * @return string
98
     */
99 1
    public function render() {
100 1
      return '<label>' . static::tag($this->tag, $this->attributes) . $this->label . '</label>';
101
    }
102
103
104
    /**
105
     * @return $this
106
     * @deprecated
107
     */
108
    public function check() {
109
      trigger_error('Deprecated', E_USER_DEPRECATED);
110
      $this->setChecked(true);
111
      return $this;
112
    }
113
114
115
    /**
116
     * Set value to 0
117
     * @return $this
118
     * @deprecated
119
     */
120
    public function unCheck() {
121
      trigger_error('Deprecated', E_USER_DEPRECATED);
122
      $this->setChecked(false);
123
      return $this;
124
    }
125
126
127
    /**
128
     * @deprecated
129
     */
130
    public function getValue() {
131
      return $this->isChecked() ? 1 : 0;
132
    }
133
134
135
    /**
136
     * @deprecated
137
     */
138
    public function setValue($value) {
139
      trigger_error('Deprecated', E_USER_DEPRECATED);
140
      return parent::setValue($value);
141
    }
142
143
  }