ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
UserLanguageDefinition.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
31use Closure;
32
34{
39 public function __construct(
40 private readonly UI $ui,
41 private readonly array $installed_languages,
42 private readonly Closure $required
43 ) {
44 }
45
46 public function formGroup(array $arguments = []): Group
47 {
48 $languages = array_combine($this->installed_languages, array_map($this->translatedLanguage(...), $this->installed_languages));
49
50 return $this->ui->create()->input()->field()->group([
51 'lng' => $this->radio(
52 $this->ui->txt('language'),
53 $languages,
54 $arguments['lng'] ?? null
55 )->withRequired(true, ($this->required)($languages))
56 ], $this->ui->txt('crit_type_usr_language'), $this->ui->txt('crit_type_usr_language_info'));
57 }
58
59 public function withCriterion(CriterionContent $criterion): Condition
60 {
61 return new UserLanguage($criterion, $this, $this->ui);
62 }
63
64 public function translatedType(): string
65 {
66 return $this->ui->txt('crit_type_usr_language');
67 }
68
69 public function translatedLanguage(string $language): string
70 {
71 return $this->ui->txt('meta_l_' . $language);
72 }
73
78 private function radio(string $lang_key, array $options, $value): Radio
79 {
80 $field = $this->ui->create()->input()->field()->radio($lang_key);
81 foreach ($options as $key => $label) {
82 $field = $field->withOption((string) $key, $label);
83 }
84
85 return $value !== null && isset($options[(string) $value]) ? $field->withValue((string) $value) : $field;
86 }
87}
__construct(private readonly UI $ui, private readonly array $installed_languages, private readonly Closure $required)
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
Describes the monoid operation of grouping form inputs.
Definition: Group.php:32
This is what a radio-input looks like.
Definition: Radio.php:29