TChecked::setValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_forms\Controls;
4
5
6
/**
7
 * Class TChecked
8
 * @package kalanis\kw_forms\Controls
9
 * Render input for selecting by radio checkbox
10
 */
11
trait TChecked
12
{
13
    /**
14
     * @param string|int|float|bool|null $value
15
     */
16 8
    public function setValue($value): void
17
    {
18 8
        $this->setChecked($value);
19 8
    }
20
21 3
    public function getValue()
22
    {
23 3
        return $this->getChecked() ? $this->originalValue : '' ;
24
    }
25
26
    /**
27
     * Set if radio is checked
28
     * @param string|int|float|bool|null $value
29
     * @return $this
30
     */
31 8
    protected function setChecked($value): self
32
    {
33 8
        if (!empty($value) && ('none' !== strval($value))) {
34 7
            $this->setAttribute('checked', 'checked');
35
        } else {
36 5
            $this->removeAttribute('checked');
37
        }
38 8
        return $this;
39
    }
40
41
    /**
42
     * Get if radio is checked
43
     * @return bool
44
     */
45 3
    protected function getChecked(): bool
46
    {
47 3
        return ('checked' == $this->getAttribute('checked'));
48
    }
49
50
    abstract public function setAttribute(string $name, string $value): void;
51
52
    abstract public function removeAttribute(string $name): void;
53
54
    abstract public function getAttribute(string $name): ?string;
55
}
56