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