Passed
Push — 5.x ( a00135...d40389 )
by Enjoys
03:00
created

Radio::resetPrefixId()   A

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 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Elements;
6
7
use Enjoys\Forms\AttributeFactory;
8
use Enjoys\Forms\Element;
9
use Enjoys\Forms\Form;
10
use Enjoys\Forms\Interfaces\Descriptionable;
11
use Enjoys\Forms\Interfaces\Fillable;
12
use Enjoys\Forms\Interfaces\Ruleable;
13
use Enjoys\Forms\Traits\Description;
14
use Enjoys\Forms\Traits\Fill;
15
use Enjoys\Forms\Traits\Rules;
16
17
class Radio extends Element implements Fillable, Ruleable, Descriptionable
18
{
19
    use Fill;
20
    use Description;
21
    use Rules;
22
23
    protected string $type = 'radio';
24
    private static ?string $prefix_id = null;
25
    private bool $parent;
26
    private string $originalName;
27
28
29 26
    public function __construct(string $name, string $title = null, bool $parent = true)
30
    {
31 26
        $this->parent = $parent;
32 26
        $this->originalName = $name;
33
34 26
        parent::__construct($name, $title);
35
36
37 26
        $this->setAttrs(
38 26
            AttributeFactory::createFromArray([
39 26
                'id' => self::$prefix_id . $this->originalName,
40
                'value' => $name,
41
            ])
42
        );
43 26
        $this->setPrefixId($this->originalName . '_');
44 26
        $this->removeAttr('name');
45
    }
46
47 26
    public function setPrefixId(string $prefix): self
48
    {
49 26
        if ($this->parent) {
50 26
            static::$prefix_id = $prefix;
0 ignored issues
show
Bug introduced by
Since $prefix_id is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $prefix_id to at least protected.
Loading history...
51 26
            $this->setAttr(AttributeFactory::create('id', $this->originalName));
52
        }
53 26
        return $this;
54
    }
55
56 1
    public function getPrefixId(): ?string
57
    {
58 1
        return static::$prefix_id;
0 ignored issues
show
Bug introduced by
Since $prefix_id is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $prefix_id to at least protected.
Loading history...
59
    }
60
61
    /**
62
     *
63
     * @param mixed $value
64
     * @return $this
65
     */
66 18
    protected function setDefault(mixed $value = null): self
67
    {
68 18
        if ($value === null) {
69 2
            $value = $this->getForm()->getDefaultsHandler()->getValue($this->getName());
70
        }
71 18
        $this->defaultValue = $value;
72
73 18
        if (is_array($value)) {
74 1
            if (in_array($this->getAttr('value')->getValueString(), $value)) {
75 1
                $this->setAttr(AttributeFactory::create('checked'));
76 1
                return $this;
77
            }
78
        }
79
80 18
        if (is_string($value) || is_numeric($value)) {
81 17
            if ($this->getAttr('value')->getValueString() == $value) {
82 1
                $this->setAttr(AttributeFactory::create('checked'));
83 1
                return $this;
84
            }
85
        }
86 18
        return $this;
87
    }
88
89 4
    public function baseHtml(): string
90
    {
91 4
        $this->setAttr($this->getAttr('id')->withName('for'), Form::ATTRIBUTES_LABEL);
92 4
        $this->setAttr(AttributeFactory::create('name', $this->getParentName()));
93 4
        return sprintf(
94
            '<input type="%s"%s><label%s>%s</label>',
95 4
            $this->getType(),
96 4
            $this->getAttributesString(),
97 4
            $this->getAttributesString(Form::ATTRIBUTES_LABEL),
98 4
            $this->getLabel()
99
        );
100
    }
101
}
102