ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
SwitchableGroup.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 use LogicException;
32 
36 class SwitchableGroup extends Group implements I\SwitchableGroup
37 {
39  use Triggerer;
40 
41  protected bool $disabled_group_switch = false;
42 
46  public function __construct(
47  DataFactory $data_factory,
49  Language $lng,
50  array $inputs,
51  string $label,
52  ?string $byline = null
53  ) {
54  $this->checkArgListElements('inputs', $inputs, [I\Group::class]);
55  parent::__construct($data_factory, $refinery, $lng, $inputs, $label, $byline);
56  }
57 
61  protected function getConstraintForRequirement(): ?Constraint
62  {
63  if ($this->requirement_constraint !== null) {
65  }
66 
67  return null;
68  }
69 
73  protected function isClientSideValueOk($value): bool
74  {
75  if (!is_string($value) && !is_int($value)) {
76  return false;
77  }
78  return array_key_exists($value, $this->getInputs());
79  }
80 
82  {
85  }
86 
90  public function withValue($value): self
91  {
92  if (is_string($value) || is_int($value)) {
95  }
96  if (!is_array($value) || count($value) !== 2) {
97  throw new InvalidArgumentException(
98  "Expected one key and a group value or one key only as value."
99  . " got '" . print_r($value, true) . "' instead."
100  );
101  }
102  list($key, $group_value) = $value;
103 
105  $clone = FormInput::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 = FormInput::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 
198  public function withDisabledGroupSwitch(bool $flag): self
199  {
200  $clone = clone $this;
201  $clone->disabled_group_switch = $flag;
202  return $clone;
203  }
204 
205  public function getDisabledGroupSwitch(): bool
206  {
208  }
209 }
isOK()
Get to know if the result is ok.
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Input.php:101
getOr(string $name, $default)
Get a named value from the data and fallback to default if that name does not exist.
value()
Get the encapsulated value.
getContent()
Get the current content of the input.
Definition: Group.php:129
getValue()
Get the value that is displayed in the input client side.mixed
Interface Observer Contains several chained tasks and infos about them.
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
withRequired($is_required, ?Constraint $requirement_constraint=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
Factory for Date Formats.
Definition: Factory.php:26
Result $content
This is the current content of the input in the abstraction.
Definition: Input.php:68
__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.
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...
getInputsWithOperationForKey(int|string $key, \Closure $operation)
Returns the inputs for.
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Definition: FormInput.php:120
getValue()
Get the value that is displayed in the input client side.
Definition: Input.php:89
__construct(Container $dic, ilPlugin $plugin)
global $lng
Definition: privfeed.php:32