Passed
Push — master ( d855a0...34f20f )
by Enjoys
01:02 queued 12s
created

Checkbox::setDefault()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 9.2222
cc 6
nc 7
nop 1
crap 6
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 36
    public function __construct(string $name, string $title = null, bool $parent = true)
30
    {
31 36
        $this->parent = $parent;
32 36
        $this->originalName = $name;
33
34 36
        $construct_name = (!str_ends_with($name, '[]')) ? $name . '[]' : $name;
35
36 36
        parent::__construct($construct_name, $title);
37
38
        /** @psalm-suppress PossiblyNullOperand */
39 36
        $this->setAttributes(
40 36
            AttributeFactory::createFromArray([
41 36
                'id' => self::$prefix_id . $this->originalName,
42 36
                'value' => $this->originalName,
43
            ])
44
        );
45
46 36
        $this->setPrefixId($this->originalName . '_');
47 36
        $this->removeAttribute('name');
48
    }
49
50
51 36
    public function setPrefixId(string $prefix): self
52
    {
53 36
        if ($this->parent) {
54 36
            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 36
            $this->setAttribute(AttributeFactory::create('id', $this->originalName));
56
        }
57 36
        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
    /**
66
     * @psalm-suppress PossiblyNullReference
67
     */
68 19
    protected function setDefault(mixed $value = null): self
69
    {
70
71 19
        $this->setDefaultValue($value);
72
73 19
        if (is_array($value)) {
74 1
            if (in_array($this->getAttribute('value')->getValueString(), $value)) {
75 1
                $this->setAttribute(AttributeFactory::create('checked'));
76 1
                return $this;
77
            }
78
        }
79
80 19
        if (is_string($value) || is_numeric($value)) {
81 17
            if ($this->getAttribute('value')->getValueString() == $value) {
82 2
                $this->setAttribute(AttributeFactory::create('checked'));
83 2
                return $this;
84
            }
85
        }
86
87
88
89 19
        return $this;
90
    }
91
92
    /**
93
     * @psalm-suppress PossiblyNullReference
94
     */
95 3
    public function baseHtml(): string
96
    {
97 3
        $this->setAttribute(
98 3
            AttributeFactory::create('for', $this->getAttribute('id')->getValueString()),
99
            Form::ATTRIBUTES_LABEL
100
        );
101 3
        $this->setAttributes(AttributeFactory::createFromArray(['name' => $this->getParentName()]));
102 3
        return sprintf(
103
            '<input type="%s"%s><label%s>%s</label>',
104 3
            $this->getType(),
105 3
            $this->getAttributesString(),
106 3
            $this->getAttributesString(Form::ATTRIBUTES_LABEL),
107 3
            $this->getLabel() ?? ''
108
        );
109
    }
110
}
111