|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Form\Field\Base\BaseField; |
|
8
|
|
|
use Yiisoft\Form\Field\Base\InputData\InputDataInterface; |
|
9
|
|
|
|
|
10
|
|
|
use function array_key_exists; |
|
11
|
|
|
|
|
12
|
|
|
final class ThemeContainer |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @psalm-var array<string,array> |
|
16
|
|
|
*/ |
|
17
|
|
|
private static array $configs = []; |
|
18
|
|
|
|
|
19
|
|
|
private static ?string $defaultConfig = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @psalm-var array<string,Theme|null> |
|
23
|
|
|
*/ |
|
24
|
|
|
private static array $themes = []; |
|
25
|
|
|
|
|
26
|
|
|
private static ?ValidationRulesEnricherInterface $validationRulesEnricher = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array<string,array> $configs Array of configurations with {@see Theme::__construct()} |
|
30
|
|
|
* arguments indexed by name. For example: |
|
31
|
|
|
* ```php |
|
32
|
|
|
* [ |
|
33
|
|
|
* 'default' => [ |
|
34
|
|
|
* 'containerClass' => 'formField', |
|
35
|
|
|
* ], |
|
36
|
|
|
* 'bulma' => [ |
|
37
|
|
|
* 'containerClass' => 'field', |
|
38
|
|
|
* 'inputClass' => 'input', |
|
39
|
|
|
* 'invalidClass' => 'has-background-danger', |
|
40
|
|
|
* 'validClass' => 'has-background-success', |
|
41
|
|
|
* 'template' => "{label}<div class=\"control\">\n{input}</div>\n{hint}\n{error}", |
|
42
|
|
|
* 'labelClass' => 'label', |
|
43
|
|
|
* 'errorClass' => 'has-text-danger is-italic', |
|
44
|
|
|
* 'hintClass' => 'help', |
|
45
|
|
|
* ], |
|
46
|
|
|
* 'bootstrap5' => [ |
|
47
|
|
|
* 'containerClass' => 'mb-3', |
|
48
|
|
|
* 'invalidClass' => 'is-invalid', |
|
49
|
|
|
* 'errorClass' => 'text-danger fst-italic', |
|
50
|
|
|
* 'hintClass' => 'form-text', |
|
51
|
|
|
* 'inputClass' => 'form-control', |
|
52
|
|
|
* 'labelClass' => 'form-label', |
|
53
|
|
|
* 'validClass' => 'is-valid', |
|
54
|
|
|
* ], |
|
55
|
|
|
* ] |
|
56
|
|
|
* ``` |
|
57
|
|
|
* @param string|null $defaultConfig Configuration name that will be used for create fields by default. |
|
58
|
|
|
*/ |
|
59
|
686 |
|
public static function initialize( |
|
60
|
|
|
array $configs = [], |
|
61
|
|
|
?string $defaultConfig = null, |
|
62
|
|
|
?ValidationRulesEnricherInterface $validationRulesEnricher = null, |
|
63
|
|
|
): void { |
|
64
|
686 |
|
self::$configs = $configs; |
|
65
|
686 |
|
self::$defaultConfig = $defaultConfig; |
|
66
|
686 |
|
self::$themes = []; |
|
67
|
686 |
|
self::$validationRulesEnricher = $validationRulesEnricher; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
676 |
|
public static function getTheme(?string $name = null): ?Theme |
|
71
|
|
|
{ |
|
72
|
676 |
|
$name ??= self::$defaultConfig; |
|
73
|
676 |
|
if ($name === null) { |
|
74
|
633 |
|
return null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
44 |
|
if (!array_key_exists($name, self::$themes)) { |
|
78
|
|
|
/** @psalm-suppress MixedArgument */ |
|
79
|
44 |
|
self::$themes[$name] = array_key_exists($name, self::$configs) |
|
80
|
44 |
|
? new Theme(...self::$configs[$name]) |
|
|
|
|
|
|
81
|
|
|
: null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
44 |
|
return self::$themes[$name]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
45 |
|
public static function getEnrichment(BaseField $field, InputDataInterface $inputData): array |
|
88
|
|
|
{ |
|
89
|
45 |
|
return self::$validationRulesEnricher?->process($field, $inputData->getValidationRules()) ?? []; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|