1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yiisoft\Form; |
||
6 | |||
7 | use Yiisoft\Form\Field\Base\EnrichFromValidationRules\EnrichFromValidationRulesInterface; |
||
8 | use Yiisoft\Form\Field\Base\InputField; |
||
9 | use Yiisoft\Form\Field\Base\PartsField; |
||
10 | use Yiisoft\Form\Field\Base\Placeholder\PlaceholderInterface; |
||
11 | use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface; |
||
12 | |||
13 | final class Theme |
||
14 | { |
||
15 | /** |
||
16 | * @param array[] $fieldConfigs |
||
17 | */ |
||
18 | 146 | public function __construct( |
|
19 | private ?string $containerTag = null, |
||
20 | private array $containerAttributes = [], |
||
21 | private string|array|null $containerClass = null, |
||
22 | private ?bool $useContainer = null, |
||
23 | private ?string $template = null, |
||
24 | private ?string $templateBegin = null, |
||
25 | private ?string $templateEnd = null, |
||
26 | private ?bool $setInputId = null, |
||
27 | private array $inputAttributes = [], |
||
28 | private string|array|null $inputClass = null, |
||
29 | private ?string $inputContainerTag = null, |
||
30 | private array $inputContainerAttributes = [], |
||
31 | private string|array|null $inputContainerClass = null, |
||
32 | string|array|null $labelClass = null, |
||
33 | private array $labelConfig = [], |
||
34 | string|array|null $hintClass = null, |
||
35 | private array $hintConfig = [], |
||
36 | string|array|null $errorClass = null, |
||
37 | private array $errorConfig = [], |
||
38 | private ?bool $usePlaceholder = null, |
||
39 | private ?string $validClass = null, |
||
40 | private ?string $invalidClass = null, |
||
41 | private ?string $inputValidClass = null, |
||
42 | private ?string $inputInvalidClass = null, |
||
43 | private ?bool $enrichFromValidationRules = null, |
||
44 | private array $fieldConfigs = [], |
||
45 | ) { |
||
46 | 146 | if ($labelClass !== null) { |
|
47 | 4 | $this->labelConfig['class()'] = is_array($labelClass) ? $labelClass : [$labelClass]; |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
48 | } |
||
49 | 146 | if ($hintClass !== null) { |
|
50 | 4 | $this->hintConfig['class()'] = is_array($hintClass) ? $hintClass : [$hintClass]; |
|
0 ignored issues
–
show
|
|||
51 | } |
||
52 | 146 | if ($errorClass !== null) { |
|
53 | 4 | $this->errorConfig['class()'] = is_array($errorClass) ? $errorClass : [$errorClass]; |
|
0 ignored issues
–
show
|
|||
54 | } |
||
55 | } |
||
56 | |||
57 | 77 | public function getLabelConfig(): array |
|
58 | { |
||
59 | 77 | return $this->labelConfig; |
|
60 | } |
||
61 | |||
62 | 79 | public function getHintConfig(): array |
|
63 | { |
||
64 | 79 | return $this->hintConfig; |
|
65 | } |
||
66 | |||
67 | 79 | public function getErrorConfig(): array |
|
68 | { |
||
69 | 79 | return $this->errorConfig; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * @psalm-param class-string $class |
||
74 | */ |
||
75 | 123 | public function getFieldConfig(string $class): array |
|
76 | { |
||
77 | 123 | $config = []; |
|
78 | |||
79 | 123 | if ($this->containerTag !== null) { |
|
80 | 50 | $config['containerTag()'] = [$this->containerTag]; |
|
81 | } |
||
82 | 123 | if ($this->containerAttributes !== []) { |
|
83 | 6 | $config['containerAttributes()'] = [$this->containerAttributes]; |
|
84 | } |
||
85 | 123 | if ($this->containerClass !== null) { |
|
86 | 3 | $config['containerClass()'] = is_array($this->containerClass) |
|
87 | 1 | ? $this->containerClass |
|
88 | 2 | : [$this->containerClass]; |
|
89 | } |
||
90 | 123 | if ($this->useContainer !== null) { |
|
91 | 1 | $config['useContainer()'] = [$this->useContainer]; |
|
92 | } |
||
93 | |||
94 | 123 | if (is_a($class, PartsField::class, true)) { |
|
95 | 120 | if ($this->template !== null) { |
|
96 | 1 | $config['template()'] = [$this->template]; |
|
97 | } |
||
98 | 120 | if ($this->templateBegin !== null) { |
|
99 | 1 | $config['templateBegin()'] = [$this->templateBegin]; |
|
100 | } |
||
101 | 120 | if ($this->templateEnd !== null) { |
|
102 | 1 | $config['templateEnd()'] = [$this->templateEnd]; |
|
103 | } |
||
104 | 120 | if ($this->inputContainerTag !== null) { |
|
105 | 4 | $config['inputContainerTag()'] = [$this->inputContainerTag]; |
|
106 | } |
||
107 | 120 | if ($this->inputContainerAttributes !== []) { |
|
108 | 1 | $config['inputContainerAttributes()'] = [$this->inputContainerAttributes]; |
|
109 | } |
||
110 | 120 | if ($this->inputContainerClass !== null) { |
|
111 | 3 | $config['inputContainerClass()'] = is_array($this->inputContainerClass) |
|
112 | 2 | ? $this->inputContainerClass |
|
113 | 1 | : [$this->inputContainerClass]; |
|
114 | } |
||
115 | 120 | if ($this->labelConfig !== []) { |
|
116 | 2 | $config['labelConfig()'] = [$this->labelConfig]; |
|
117 | } |
||
118 | 120 | if ($this->hintConfig !== []) { |
|
119 | 2 | $config['hintConfig()'] = [$this->hintConfig]; |
|
120 | } |
||
121 | 120 | if ($this->errorConfig !== []) { |
|
122 | 2 | $config['errorConfig()'] = [$this->errorConfig]; |
|
123 | } |
||
124 | } |
||
125 | |||
126 | 123 | if (is_a($class, InputField::class, true)) { |
|
127 | 98 | if ($this->setInputId !== null) { |
|
128 | 1 | $config['setInputId()'] = [$this->setInputId]; |
|
129 | } |
||
130 | 98 | if ($this->inputAttributes !== []) { |
|
131 | 2 | $config['inputAttributes()'] = [$this->inputAttributes]; |
|
132 | } |
||
133 | 98 | if ($this->inputClass !== null) { |
|
134 | 5 | $config['inputClass()'] = is_array($this->inputClass) |
|
135 | 2 | ? $this->inputClass |
|
136 | 3 | : [$this->inputClass]; |
|
137 | } |
||
138 | } |
||
139 | |||
140 | 123 | if (is_a($class, PlaceholderInterface::class, true)) { |
|
141 | 64 | if ($this->usePlaceholder !== null) { |
|
142 | 2 | $config['usePlaceholder()'] = [$this->usePlaceholder]; |
|
143 | } |
||
144 | } |
||
145 | |||
146 | 123 | if (is_a($class, EnrichFromValidationRulesInterface::class, true)) { |
|
147 | 90 | if ($this->enrichFromValidationRules !== null) { |
|
148 | 2 | $config['enrichFromValidationRules()'] = [$this->enrichFromValidationRules]; |
|
149 | } |
||
150 | } |
||
151 | |||
152 | 123 | if (is_a($class, ValidationClassInterface::class, true)) { |
|
153 | 103 | if ($this->validClass !== null) { |
|
154 | 14 | $config['validClass()'] = [$this->validClass]; |
|
155 | } |
||
156 | 103 | if ($this->invalidClass !== null) { |
|
157 | 14 | $config['invalidClass()'] = [$this->invalidClass]; |
|
158 | } |
||
159 | 103 | if ($this->inputValidClass !== null) { |
|
160 | 12 | $config['inputValidClass()'] = [$this->inputValidClass]; |
|
161 | } |
||
162 | 103 | if ($this->inputInvalidClass !== null) { |
|
163 | 12 | $config['inputInvalidClass()'] = [$this->inputInvalidClass]; |
|
164 | } |
||
165 | } |
||
166 | |||
167 | 123 | if (!empty($this->fieldConfigs[$class])) { |
|
168 | 2 | $config = array_merge($config, $this->fieldConfigs[$class]); |
|
169 | } |
||
170 | |||
171 | 123 | return $config; |
|
172 | } |
||
173 | } |
||
174 |