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; |
|
|
|
|
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; |
|
|
|
|
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
|
|
|
|