ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
SwitchableGroup.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data\Factory as DataFactory;
31use LogicException;
32use InvalidArgumentException;
33
37class SwitchableGroup extends Group implements I\SwitchableGroup
38{
40 use Triggerer;
41
45 public function __construct(
46 DataFactory $data_factory,
49 array $inputs,
50 string $label,
51 ?string $byline = null
52 ) {
53 $this->checkArgListElements('inputs', $inputs, [I\Group::class]);
54 parent::__construct($data_factory, $refinery, $lng, $inputs, $label, $byline);
55 }
56
61 {
62 if ($this->requirement_constraint !== null) {
63 return $this->requirement_constraint;
64 }
65
66 return null;
67 }
68
70 protected function isClientSideValueOk($value): bool
71 {
72 if (is_string($value) || is_int($value)) {
73 return array_key_exists($value, $this->getInputs());
74 }
75 if (!is_array($value)) {
76 return false;
77 }
78 [$key, $group_value] = $value;
79 if (!array_key_exists($key, $this->getInputs())) {
80 return false;
81 }
82 return $this->inputs[$key]->isClientSideValueOk($group_value);
83 }
84
85 public function withRequired($is_required, ?Constraint $requirement_constraint = null): self
86 {
88 return FormInput::withRequired($is_required, $requirement_constraint);
89 }
90
96 public function withValue($value): self
97 {
98 $this->checkArg('value', null === $value || $this->isClientSideValueOk($value), 'Display value does not match input type.');
99 if (null === $value || is_string($value) || is_int($value)) {
101 return Input::withValue($value);
102 }
103 [$key, $group_value] = $value;
105 $clone = Input::withValue($key);
106 $clone->setInputs($clone->getInputsWithOperationForKey($key, fn($i) => $i->withValue($group_value)));
107 return $clone;
108 }
109
113 public function getValue()
114 {
115 $key = Input::getValue();
116 if (is_null($key)) {
117 return null;
118 }
119
120 $input = $this->getInputs()[$key] ?? null;
121 if (null === $input) {
122 return null;
123 }
124
125 return [$key, $input->getValue()];
126 }
127
131 public function withInput(InputData $input): self
132 {
133 if ($this->getName() === null) {
134 throw new LogicException("Can only collect if input has a name.");
135 }
136
137 $key = $input->getOr($this->getName(), "");
138 $clone = clone $this;
139
140 if ($key === "") {
141 if ($this->isRequired()) {
142 $clone->content = $clone->data_factory->error($this->lng->txt("ui_error_switchable_group_required"));
143 return $clone->withError("" . $clone->content->error());
144 }
145
146 $clone->content = $clone->data_factory->ok([$key, []]);
147 return $clone;
148 }
149
150 if (!$this->isDisabled()) {
151 $clone = $clone->withValue($key);
152 $clone->setInputs($clone->getInputsWithOperationForKey($key, fn($i) => $i->withInput($input)));
153 }
154
156 $inputs = $clone->getInputs();
157 if (!array_key_exists($key, $inputs)) {
158 $clone->content = $clone->data_factory->ok([$key, []]);
159 return $clone;
160 }
161
162 if ($inputs[$key]->getContent()->isError()) {
163 $clone->content = $clone->data_factory->error($this->lng->txt("ui_error_in_group"));
164 return $clone;
165 }
166
167 $contents = [];
168 foreach ($inputs[$key]->getInputs() as $subkey => $group_input) {
169 $content = $group_input->getContent();
170 if ($content->isOK()) {
171 $contents[$subkey] = $content->value();
172 }
173 }
174
175 $clone->content = $this->applyOperationsTo([$key, $contents]);
176 if ($clone->content->isError()) {
177 return $clone->withError("" . $clone->content->error());
178 }
179
180 return $clone;
181 }
182
188 protected function getInputsWithOperationForKey(int|string $key, \Closure $operation): array
189 {
190 $inputs = $this->getInputs();
191 if (!array_key_exists($key, $inputs)) {
192 throw new LogicException("Key '$key' does not exist in inputs.");
193 }
194 $inputs[$key] = $operation($inputs[$key]);
195 return $inputs;
196 }
197}
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.
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Input.php:101
getValue()
Get the value that is displayed in the input client side.
Definition: Input.php:89
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.
This describes commonalities between all inputs.
Definition: Input.php:47
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:26