ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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 
44  public function __construct(
45  DataFactory $data_factory,
47  Language $lng,
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 
59  protected function getConstraintForRequirement(): ?Constraint
60  {
61  if ($this->requirement_constraint !== null) {
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 
80  {
83  }
84 
88  public function withValue($value): self
89  {
90  if (is_string($value) || is_int($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  {
113  $key = FormInput::getValue();
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): self
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(int|string $key, \Closure $operation): array
187  {
188  $inputs = $this->getInputs();
189  if (!array_key_exists($key, $inputs)) {
190  throw new LogicException("Key '$key' does not exist in inputs.");
191  }
192  $inputs[$key] = $operation($inputs[$key]);
193  return $inputs;
194  }
195 }
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
value()
Get the encapsulated value.
getContent()
Get the current content of the input.
Definition: Group.php:129
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
getValue()
Get the value that is displayed in the input client side.mixed
Interface Observer Contains several chained tasks and infos about them.
getOr(string $name, $default)
Get a named value from the data and fallback to default if that name does not exist.
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
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
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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:31