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