ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
SwitchableGroup.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data\Factory as DataFactory;
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,
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
69 protected function isClientSideValueOk($value): bool
70 {
71 if (is_string($value) || is_int($value)) {
72 return array_key_exists($value, $this->getInputs());
73 }
74 if (!is_array($value)) {
75 return false;
76 }
77 [$key, $group_value] = $value;
78 if (!array_key_exists($key, $this->getInputs())) {
79 return false;
80 }
81 return $this->inputs[$key]->isClientSideValueOk($group_value);
82 }
83
84 public function withRequired($is_required, ?Constraint $requirement_constraint = null): self
85 {
87 return FormInput::withRequired($is_required, $requirement_constraint);
88 }
89
95 public function withValue($value): self
96 {
97 $this->checkArg('value', $this->isClientSideValueOk($value), 'Display value does not match input type.');
98 if (is_string($value) || is_int($value)) {
100 return FormInput::withValue($value);
101 }
102 [$key, $group_value] = $value;
104 $clone = FormInput::withValue($key);
105 $clone->setInputs($clone->getInputsWithOperationForKey($key, fn($i) => $i->withValue($group_value)));
106 return $clone;
107 }
108
112 public function getValue()
113 {
114 $key = FormInput::getValue();
115 if (is_null($key)) {
116 return null;
117 }
118
119 $input = $this->getInputs()[$key] ?? null;
120 if (null === $input) {
121 return null;
122 }
123
124 return [$key, $input->getValue()];
125 }
126
130 public function withInput(InputData $input): self
131 {
132 if ($this->getName() === null) {
133 throw new LogicException("Can only collect if input has a name.");
134 }
135
136 $key = $input->getOr($this->getName(), "");
137 $clone = clone $this;
138
139 if ($key === "") {
140 if ($this->isRequired()) {
141 $clone->content = $clone->data_factory->error($this->lng->txt("ui_error_switchable_group_required"));
142 return $clone->withError("" . $clone->content->error());
143 }
144
145 $clone->content = $clone->data_factory->ok([$key, []]);
146 return $clone;
147 }
148
149 if (!$this->isDisabled()) {
150 $clone = $clone->withValue($key);
151 $clone->setInputs($clone->getInputsWithOperationForKey($key, fn($i) => $i->withInput($input)));
152 }
153
155 $inputs = $clone->getInputs();
156 if (!array_key_exists($key, $inputs)) {
157 $clone->content = $clone->data_factory->ok([$key, []]);
158 return $clone;
159 }
160
161 if ($inputs[$key]->getContent()->isError()) {
162 $clone->content = $clone->data_factory->error($this->lng->txt("ui_error_in_group"));
163 return $clone;
164 }
165
166 $contents = [];
167 foreach ($inputs[$key]->getInputs() as $subkey => $group_input) {
168 $content = $group_input->getContent();
169 if ($content->isOK()) {
170 $contents[$subkey] = $content->value();
171 }
172 }
173
174 $clone->content = $this->applyOperationsTo([$key, $contents]);
175 if ($clone->content->isError()) {
176 return $clone->withError("" . $clone->content->error());
177 }
178
179 return $clone;
180 }
181
187 protected function getInputsWithOperationForKey(int|string $key, \Closure $operation): array
188 {
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:36
This implements the group input.
Definition: Group.php:41
getContent()
Get the current content of the input.
Definition: Group.php:129
withRequired($is_required, ?Constraint $requirement_constraint=null)
getValue()
Get the value that is displayed in the input client side.mixed
getInputsWithOperationForKey(int|string $key, \Closure $operation)
Returns the inputs for.
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, Language $lng, array $inputs, string $label, ?string $byline=null)
Only adds a check to the original group-constructor.
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).
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.
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.
getName()
The name of the input as used in HTML.
withInput(InputData $input)
Get an input like this with input from post data.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
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
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31