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

Checkbox::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 Checkbox extends Element implements Fillable, Ruleable, Descriptionable
18
{
19
    use Fill;
20
    use Description;
21
    use Rules;
22
23
    protected string $type = 'checkbox';
24
    private static ?string $prefix_id = null;
25
    private bool $parent;
26
    private string $originalName;
27
28
29 35
    public function __construct(string $name, string $title = null, bool $parent = true)
30
    {
31 35
        $this->parent = $parent;
32 35
        $this->originalName = $name;
33
34 35
        $construct_name = (!str_ends_with($name, '[]')) ? $name . '[]' : $name;
35
36 35
        parent::__construct($construct_name, $title);
37
38 35
        $this->setAttrs(
39 35
            AttributeFactory::createFromArray([
40 35
                'id' => self::$prefix_id . $this->originalName,
41 35
                'value' => $this->originalName,
42
            ])
43
        );
44
45 35
        $this->setPrefixId($this->originalName . '_');
46 35
        $this->removeAttr('name');
47
48
    }
49
50
51 35
    public function setPrefixId(string $prefix): self
52
    {
53 35
        if ($this->parent) {
54 35
            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...
55 35
            $this->setAttr(AttributeFactory::create('id', $this->originalName));
56
        }
57 35
        return $this;
58
    }
59
60 1
    public function getPrefixId(): ?string
61
    {
62 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...
63
    }
64
65 18
    protected function setDefault(mixed $value = null): self
66
    {
67 18
        if ($value === null) {
68 3
            $value = $this->getForm()->getDefaultsHandler()->getValue($this->getName());
69
        }
70 18
        $this->defaultValue = $value;
71
72 18
        if (is_array($value)) {
73 1
            if (in_array($this->getAttr('value')->getValueString(), $value)) {
74 1
                $this->setAttr(AttributeFactory::create('checked'));
75 1
                return $this;
76
            }
77
        }
78
79 18
        if (is_string($value) || is_numeric($value)) {
80 16
            if ($this->getAttr('value')->getValueString() == $value) {
81 1
                $this->setAttr(AttributeFactory::create('checked'));
82 1
                return $this;
83
            }
84
        }
85 18
        return $this;
86
    }
87
88 3
    public function baseHtml(): string
89
    {
90 3
        $this->setAttr(AttributeFactory::create('for', $this->getAttr('id')->getValueString()), Form::ATTRIBUTES_LABEL);
91 3
        $this->setAttrs(AttributeFactory::createFromArray(['name' => $this->getParentName()]));
92 3
        return sprintf(
93
            '<input type="%s"%s><label%s>%s</label>',
94 3
            $this->getType(),
95 3
            $this->getAttributesString(),
96 3
            $this->getAttributesString(Form::ATTRIBUTES_LABEL),
97 3
            $this->getLabel()
98
        );
99
    }
100
}
101