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
|
|
|
protected bool $allowSameNames = true; |
29
|
|
|
|
30
|
|
|
|
31
|
37 |
|
public function __construct(string $name, ?string $title = null, bool $parent = true) |
32
|
|
|
{ |
33
|
37 |
|
$this->parent = $parent; |
34
|
37 |
|
$this->originalName = trim($name); |
35
|
|
|
|
36
|
37 |
|
$construct_name = (!str_ends_with($name, '[]')) ? $name . '[]' : $name; |
37
|
|
|
|
38
|
37 |
|
parent::__construct($construct_name, $title); |
39
|
|
|
|
40
|
|
|
/** @psalm-suppress PossiblyNullOperand */ |
41
|
37 |
|
$this->setAttributes( |
42
|
37 |
|
AttributeFactory::createFromArray([ |
43
|
37 |
|
'id' => self::$prefix_id . $this->originalName, |
44
|
37 |
|
'value' => $this->originalName, |
45
|
37 |
|
]) |
46
|
37 |
|
); |
47
|
|
|
|
48
|
37 |
|
$this->setPrefixId($this->originalName . '_'); |
49
|
37 |
|
$this->removeAttribute('name'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
37 |
|
public function setPrefixId(string $prefix): self |
54
|
|
|
{ |
55
|
37 |
|
if ($this->parent) { |
56
|
37 |
|
static::$prefix_id = $prefix; |
|
|
|
|
57
|
37 |
|
$this->setAttribute(AttributeFactory::create('id', $this->originalName)); |
58
|
|
|
} |
59
|
37 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function getPrefixId(): ?string |
63
|
|
|
{ |
64
|
1 |
|
return static::$prefix_id; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @psalm-suppress PossiblyNullReference |
69
|
|
|
*/ |
70
|
20 |
|
protected function setDefault(mixed $value = null): static |
71
|
|
|
{ |
72
|
|
|
|
73
|
20 |
|
$this->setDefaultValue($value); |
74
|
|
|
|
75
|
20 |
|
if (is_array($value)) { |
76
|
2 |
|
if (in_array($this->getAttribute('value')->getValueString(), $value)) { |
77
|
2 |
|
$this->setAttribute(AttributeFactory::create('checked')); |
78
|
2 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
20 |
|
if (is_string($value) || is_numeric($value)) { |
83
|
17 |
|
if ($this->getAttribute('value')->getValueString() == $value) { |
84
|
2 |
|
$this->setAttribute(AttributeFactory::create('checked')); |
85
|
2 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
20 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @psalm-suppress PossiblyNullReference |
96
|
|
|
*/ |
97
|
3 |
|
public function baseHtml(): string |
98
|
|
|
{ |
99
|
3 |
|
$this->setAttribute( |
100
|
3 |
|
AttributeFactory::create('for', $this->getAttribute('id')->getValueString()), |
101
|
3 |
|
Form::ATTRIBUTES_LABEL |
102
|
3 |
|
); |
103
|
3 |
|
$this->setAttributes(AttributeFactory::createFromArray(['name' => $this->getParentName()])); |
104
|
3 |
|
return sprintf( |
105
|
3 |
|
'<input type="%s"%s><label%s>%s</label>', |
106
|
3 |
|
$this->getType(), |
107
|
3 |
|
$this->getAttributesString(), |
108
|
3 |
|
$this->getAttributesString(Form::ATTRIBUTES_LABEL), |
109
|
3 |
|
$this->getLabel() ?? '' |
110
|
3 |
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|