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\Interfaces\AttributeInterface; |
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 Select extends Element implements Fillable, Ruleable, Descriptionable |
18
|
|
|
{ |
19
|
|
|
use Fill; |
20
|
|
|
use Description; |
21
|
|
|
use Rules; |
22
|
|
|
|
23
|
|
|
protected string $type = 'option'; |
24
|
|
|
|
25
|
45 |
|
public function __construct(string $name, ?string $title = null) |
26
|
|
|
{ |
27
|
45 |
|
parent::__construct($name, $title); |
28
|
|
|
} |
29
|
|
|
|
30
|
3 |
|
public function setMultiple(): Select |
31
|
|
|
{ |
32
|
3 |
|
$this->setAttribute(AttributeFactory::create('multiple')); |
33
|
3 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
45 |
|
private function isMultiple(): void |
37
|
|
|
{ |
38
|
45 |
|
if ($this->getAttribute('multiple') !== null && !str_ends_with($this->getName(), '[]')) { |
39
|
9 |
|
$id = $this->getAttribute('id') ?? AttributeFactory::create('id', $this->getName()); |
40
|
9 |
|
$this->setName($this->getName() . '[]'); |
41
|
|
|
//т.к. id уже переписан ,восстанавливаем его |
42
|
9 |
|
$this->setAttribute($id); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @psalm-suppress MethodSignatureMismatch |
48
|
|
|
*/ |
49
|
45 |
|
public function setAttribute(AttributeInterface $attribute, string $namespace = 'general'): static |
50
|
|
|
{ |
51
|
45 |
|
parent::setAttribute($attribute, $namespace); |
52
|
45 |
|
$this->isMultiple(); |
53
|
45 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
12 |
|
protected function setDefault(mixed $value = null): static |
58
|
|
|
{ |
59
|
12 |
|
$this->setDefaultValue($value); |
60
|
12 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $label Аттрибут label для optgroup |
65
|
|
|
* @param array $data Массив для заполнения в функции fill() |
66
|
|
|
* @param array $attributes Аттрибуты для optgroup (id и name аттрибуты автоматически удалены) |
67
|
|
|
* @param bool $useTitleAsValue |
68
|
|
|
* @return $this |
69
|
|
|
* @since 2.4.0 |
70
|
|
|
* @since 3.4.0 added $useTitleAsValue, see Trait\Fill |
71
|
|
|
*/ |
72
|
5 |
|
public function setOptgroup( |
73
|
|
|
string $label, |
74
|
|
|
array $data = [], |
75
|
|
|
array $attributes = [], |
76
|
|
|
bool $useTitleAsValue = false |
77
|
|
|
): Select { |
78
|
5 |
|
$optgroup = new Optgroup($label, $this->getName(), $this->defaultValue); |
79
|
5 |
|
$optgroup->setAttributes(AttributeFactory::createFromArray($attributes)); |
80
|
5 |
|
$optgroup->fill($data, $useTitleAsValue); |
81
|
5 |
|
$this->elements[] = $optgroup; |
82
|
5 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|