1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Form; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Yiisoft\Form\Field\Checkbox; |
9
|
|
|
use Yiisoft\Form\Field\Date; |
10
|
|
|
use Yiisoft\Form\Field\DateTimeLocal; |
11
|
|
|
use Yiisoft\Form\Field\Email; |
12
|
|
|
use Yiisoft\Form\Field\Hidden; |
13
|
|
|
use Yiisoft\Form\Field\Part\Error; |
14
|
|
|
use Yiisoft\Form\Field\Part\Hint; |
15
|
|
|
use Yiisoft\Form\Field\Part\Label; |
16
|
|
|
use Yiisoft\Form\Field\Text; |
17
|
|
|
|
18
|
|
|
use function array_key_exists; |
19
|
|
|
|
20
|
|
|
final class Field |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @psalm-var array<string,array> |
24
|
|
|
*/ |
25
|
|
|
private static array $configs = [ |
26
|
|
|
'default' => [], |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
private static string $defaultConfigName = 'default'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @psalm-var array<string,FieldFactory> |
33
|
|
|
*/ |
34
|
|
|
private static array $factories = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @psalm-param array<string,array> $configs |
38
|
|
|
*/ |
39
|
|
|
public static function initialize(array $configs = [], string $defaultConfigName = 'default'): void |
40
|
|
|
{ |
41
|
|
|
self::$configs = array_merge(self::$configs, $configs); |
42
|
|
|
self::$defaultConfigName = $defaultConfigName; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public static function checkbox(FormModelInterface $formModel, string $attribute, array $config = []): Checkbox |
46
|
|
|
{ |
47
|
1 |
|
return self::getFactory()->checkbox($formModel, $attribute, $config); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public static function date(FormModelInterface $formModel, string $attribute, array $config = []): Date |
51
|
|
|
{ |
52
|
1 |
|
return self::getFactory()->date($formModel, $attribute, $config); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public static function dateTimeLocal( |
56
|
|
|
FormModelInterface $formModel, |
57
|
|
|
string $attribute, |
58
|
|
|
array $config = [] |
59
|
|
|
): DateTimeLocal { |
60
|
1 |
|
return self::getFactory()->dateTimeLocal($formModel, $attribute, $config); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public static function email(FormModelInterface $formModel, string $attribute, array $config = []): Email |
64
|
|
|
{ |
65
|
1 |
|
return self::getFactory()->email($formModel, $attribute, $config); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public static function hidden(FormModelInterface $formModel, string $attribute, array $config = []): Hidden |
69
|
|
|
{ |
70
|
1 |
|
return self::getFactory()->hidden($formModel, $attribute, $config); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public static function text(FormModelInterface $formModel, string $attribute, array $config = []): Text |
74
|
|
|
{ |
75
|
1 |
|
return self::getFactory()->text($formModel, $attribute, $config); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public static function label(FormModelInterface $formModel, string $attribute, array $config = []): Label |
79
|
|
|
{ |
80
|
1 |
|
return self::getFactory()->label($formModel, $attribute, $config); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public static function hint(FormModelInterface $formModel, string $attribute, array $config = []): Hint |
84
|
|
|
{ |
85
|
1 |
|
return self::getFactory()->hint($formModel, $attribute, $config); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public static function error(FormModelInterface $formModel, string $attribute, array $config = []): Error |
89
|
|
|
{ |
90
|
1 |
|
return self::getFactory()->error($formModel, $attribute, $config); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @psalm-template T |
95
|
|
|
* @psalm-param class-string<T> $class |
96
|
|
|
* @psalm-return T |
97
|
|
|
*/ |
98
|
|
|
public function field(string $class, FormModelInterface $formModel, string $attribute, array $config = []): object |
99
|
|
|
{ |
100
|
|
|
return self::getFactory()->field($class, $formModel, $attribute, $config); |
101
|
|
|
} |
102
|
|
|
|
103
|
9 |
|
public static function getFactory(?string $name = null): FieldFactory |
104
|
|
|
{ |
105
|
9 |
|
$name = $name ?? self::$defaultConfigName; |
106
|
|
|
|
107
|
9 |
|
if (!array_key_exists($name, self::$factories)) { |
108
|
1 |
|
if (!array_key_exists($name, self::$configs)) { |
109
|
|
|
throw new RuntimeException( |
110
|
|
|
sprintf('Configuration with name "%s" not found.', $name) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** @psalm-suppress MixedArgument */ |
115
|
1 |
|
self::$factories[$name] = new FieldFactory(...self::$configs[$name]); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
9 |
|
return self::$factories[$name]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|