|
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
|
|
|
|
|
24
|
|
|
protected string $type = 'option'; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
41 |
|
public function __construct(string $name, string $title = null) |
|
29
|
|
|
{ |
|
30
|
41 |
|
parent::__construct($name, $title); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public function setMultiple(): self |
|
34
|
|
|
{ |
|
35
|
2 |
|
$this->setAttr(AttributeFactory::create('multiple')); |
|
36
|
2 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
41 |
|
private function isMultiple(): void |
|
40
|
|
|
{ |
|
41
|
41 |
|
if ($this->getAttr('multiple') !== null && !str_ends_with($this->getName(), '[]')) { |
|
42
|
8 |
|
$id = $this->getAttr('id') ?? AttributeFactory::create('id', $this->getName()); |
|
43
|
8 |
|
$this->setName($this->getName() . '[]'); |
|
44
|
|
|
//т.к. id уже переписан ,восстанавливаем его |
|
45
|
8 |
|
$this->setAttr($id); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return $this |
|
51
|
|
|
*/ |
|
52
|
41 |
|
public function setAttr(AttributeInterface $attribute, string $namespace = 'general') |
|
53
|
|
|
{ |
|
54
|
41 |
|
parent::setAttr($attribute, $namespace); |
|
55
|
41 |
|
$this->isMultiple(); |
|
56
|
41 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
* @return $this |
|
64
|
|
|
*/ |
|
65
|
8 |
|
protected function setDefault(): self |
|
66
|
|
|
{ |
|
67
|
|
|
// $this->defaultValue = $this->getForm()->getDefaultsHandler()->getValue($this->getName()); |
|
68
|
8 |
|
$this->setDefaultValue($this->getForm()->getDefaultsHandler()->getValue($this->getName())); |
|
69
|
8 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $label Аттрибут label для optgroup |
|
74
|
|
|
* @param array $data Массив для заполнения в функции fill() |
|
75
|
|
|
* @param array $attributes Аттрибуты для optgroup (id и name аттрибуты автоматически удалены) |
|
76
|
|
|
* @param bool $useTitleAsValue |
|
77
|
|
|
* @return $this |
|
78
|
|
|
* @since 2.4.0 |
|
79
|
|
|
* @since 3.4.0 added $useTitleAsValue, see Trait\Fill |
|
80
|
|
|
* |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function setOptgroup(string $label, array $data = [], array $attributes = [], $useTitleAsValue = false): self |
|
83
|
|
|
{ |
|
84
|
3 |
|
$optgroup = new Optgroup($label, $this->getName(), $this->defaultValue); |
|
85
|
3 |
|
$optgroup->setAttrs(AttributeFactory::createFromArray($attributes)); |
|
86
|
3 |
|
$optgroup->fill($data, $useTitleAsValue); |
|
87
|
3 |
|
$this->elements[] = $optgroup; |
|
88
|
3 |
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|