ILIAS  release_8 Revision v8.24
SwitchableGroup.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data\Factory as DataFactory;
29use ilLanguage;
30use LogicException;
31use InvalidArgumentException;
32
36class SwitchableGroup extends Group implements I\SwitchableGroup
37{
39 use Triggerer;
40
44 public function __construct(
45 DataFactory $data_factory,
46 \ILIAS\Refinery\Factory $refinery,
48 array $inputs,
49 string $label,
50 ?string $byline = null
51 ) {
52 $this->checkArgListElements('inputs', $inputs, [I\Group::class]);
53 parent::__construct($data_factory, $refinery, $lng, $inputs, $label, $byline);
54 }
55
60 {
61 if ($this->requirement_constraint !== null) {
62 return $this->requirement_constraint;
63 }
64
65 return null;
66 }
67
71 protected function isClientSideValueOk($value): bool
72 {
73 if (!is_string($value) && !is_int($value)) {
74 return false;
75 }
76 return array_key_exists($value, $this->getInputs());
77 }
78
79 public function withRequired($is_required, ?Constraint $requirement_constraint = null): I\Input
80 {
82 return FormInput::withRequired($is_required, $requirement_constraint);
83 }
84
88 public function withValue($value): I\Input
89 {
90 if (is_string($value) || is_int($value)) {
92 return FormInput::withValue($value);
93 }
94 if (!is_array($value) || count($value) !== 2) {
95 throw new InvalidArgumentException(
96 "Expected one key and a group value or one key only as value."
97 . " got '" . print_r($value, true) . "' instead."
98 );
99 }
100 list($key, $group_value) = $value;
101
103 $clone = FormInput::withValue($key);
104 $clone->setInputs($clone->getInputsWithOperationForKey($key, fn ($i) => $i->withValue($group_value)));
105 return $clone;
106 }
107
111 public function getValue()
112 {
114 if (is_null($key)) {
115 return null;
116 }
117
118 $input = $this->getInputs()[$key] ?? null;
119 if (null === $input) {
120 return null;
121 }
122
123 return [$key, $input->getValue()];
124 }
125
129 public function withInput(InputData $input): I\Input
130 {
131 if ($this->getName() === null) {
132 throw new LogicException("Can only collect if input has a name.");
133 }
134
135 $key = $input->getOr($this->getName(), "");
136 $clone = clone $this;
137
138 if ($key === "") {
139 if ($this->isRequired()) {
140 $clone->content = $clone->data_factory->error($this->lng->txt("ui_error_switchable_group_required"));
141 return $clone->withError("" . $clone->content->error());
142 }
143
144 $clone->content = $clone->data_factory->ok([$key, []]);
145 return $clone;
146 }
147
148 if (!$this->isDisabled()) {
149 $clone = $clone->withValue($key);
150 $clone->setInputs($clone->getInputsWithOperationForKey($key, fn ($i) => $i->withInput($input)));
151 }
152
154 $inputs = $clone->getInputs();
155 if (!array_key_exists($key, $inputs)) {
156 $clone->content = $clone->data_factory->ok([$key, []]);
157 return $clone;
158 }
159
160 if ($inputs[$key]->getContent()->isError()) {
161 $clone->content = $clone->data_factory->error($this->lng->txt("ui_error_in_group"));
162 return $clone;
163 }
164
165 $contents = [];
166 foreach ($inputs[$key]->getInputs() as $subkey => $group_input) {
167 $content = $group_input->getContent();
168 if ($content->isOK()) {
169 $contents[$subkey] = $content->value();
170 }
171 }
172
173 $clone->content = $this->applyOperationsTo([$key, $contents]);
174 if ($clone->content->isError()) {
175 return $clone->withError("" . $clone->content->error());
176 }
177
178 return $clone;
179 }
180
186 protected function getInputsWithOperationForKey($key, \Closure $operation): array
187 {
188 $this->checkArg("key", is_int($key) || is_string($key), "Key must be int or string.");
189 $inputs = $this->getInputs();
190 if (!array_key_exists($key, $inputs)) {
191 throw new LogicException("Key '$key' does not exist in inputs.");
192 }
193 $inputs[$key] = $operation($inputs[$key]);
194 return $inputs;
195 }
196}
Factory for Date Formats.
Definition: Factory.php:27
Builds data types.
Definition: Factory.php:21
This implements the group input.
Definition: Group.php:43
getContent()
Get the current content of the input.
Definition: Group.php:133
This is a legacy support of Implementation\Component\Input\Field\Input that has been moved to Impleme...
Definition: Input.php:32
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, ilLanguage $lng, array $inputs, string $label, ?string $byline=null)
Only adds a check to the original group-constructor.
withRequired($is_required, ?Constraint $requirement_constraint=null)
getValue()
Get the value that is displayed in the input client side.mixed
isClientSideValueOk($value)
ATTENTION:GroupInternals::_isClientSideValueOk()
getInputsWithOperationForKey($key, \Closure $operation)
Returns the inputs for.
language handling
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Get an input like this, but set the field to be required (or not).
getValue()
Get the value that is displayed in the input client side.
withValue($value)
Get an input like this with another value displayed on the client side.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
getOr(string $name, $default)
Get a named value from the data and fallback to default if that name does not exist.
getName()
The name of the input as used in HTML.
withInput(InputData $input)
Get an input like this with input from post data.
$i
Definition: metadata.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193
Refinery Factory $refinery
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
Class ChatMainBarProvider \MainMenu\Provider.