TChecked   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 44
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getChecked() 0 3 1
A setValue() 0 3 1
A getValue() 0 3 2
A setChecked() 0 8 3
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