yiisoft /
form
| 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 | 877 | public static function initialize( |
|
| 60 | array $configs = [], |
||
| 61 | ?string $defaultConfig = null, |
||
| 62 | ?ValidationRulesEnricherInterface $validationRulesEnricher = null, |
||
| 63 | ): void { |
||
| 64 | 877 | self::$configs = $configs; |
|
| 65 | 877 | self::$defaultConfig = $defaultConfig; |
|
| 66 | 877 | self::$themes = []; |
|
| 67 | 877 | self::$validationRulesEnricher = $validationRulesEnricher; |
|
| 68 | } |
||
| 69 | |||
| 70 | 862 | public static function getTheme(?string $name = null): ?Theme |
|
| 71 | { |
||
| 72 | 862 | $name ??= self::$defaultConfig; |
|
| 73 | 862 | if ($name === null) { |
|
| 74 | 765 | return null; |
|
| 75 | } |
||
| 76 | |||
| 77 | 146 | if (!array_key_exists($name, self::$themes)) { |
|
| 78 | /** @psalm-suppress MixedArgument */ |
||
| 79 | 146 | self::$themes[$name] = array_key_exists($name, self::$configs) |
|
| 80 | 146 | ? new Theme(...self::$configs[$name]) |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 81 | 1 | : null; |
|
| 82 | } |
||
| 83 | |||
| 84 | 146 | return self::$themes[$name]; |
|
| 85 | } |
||
| 86 | |||
| 87 | 11 | public static function getEnrichment(BaseField $field, InputDataInterface $inputData): array |
|
| 88 | { |
||
| 89 | 11 | return self::$validationRulesEnricher?->process($field, $inputData->getValidationRules()) ?? []; |
|
| 90 | } |
||
| 91 | } |
||
| 92 |