ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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 
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 
85  {
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)) {
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 }
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