TSelected::setSelected()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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