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