ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
Group.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
21 
29 
33 trait Group
34 {
35  use ComponentHelper;
36 
40  protected array $inputs = [];
41 
47  public function getValue()
48  {
49  return array_map(fn ($i) => $i->getValue(), $this->inputs);
50  }
51 
59  public function withValue($value): LegacyInputInterface
60  {
61  $this->checkArg("value", $this->isClientSideValueOk($value), "Display value does not match input type.");
62  $clone = clone $this;
63  foreach ($this->getInputs() as $k => $i) {
64  $clone->inputs[$k] = $i->withValue($value[$k]);
65  }
66  return $clone;
67  }
68 
75  public function withInput(InputData $input): LegacyInputInterface
76  {
77  if (empty($this->getInputs())) {
78  return $this;
79  }
80 
81  $clone = clone $this;
82 
83  $inputs = [];
84  $contents = [];
85  $error = false;
86 
87  foreach ($this->getInputs() as $key => $in) {
88  $inputs[$key] = $in->withInput($input);
89  $content = $inputs[$key]->getContent();
90  if ($content->isError()) {
91  $error = true;
92  } else {
93  $contents[$key] = $content->value();
94  }
95  }
96 
97  $clone->inputs = $inputs;
98  if ($error) {
99  $clone->content = $clone->getDataFactory()->error($this->getLanguage()->txt("ui_error_in_group"));
100  } else {
101  $clone->content = $clone->applyOperationsTo($contents);
102  }
103 
104  if ($clone->content->isError()) {
105  $clone->setError("" . $clone->content->error());
106  }
107 
108  return $clone;
109  }
110 
114  protected function nameInputs(NameSource $source, string $parent_name): array
115  {
116  $named_inputs = [];
117  foreach ($this->getInputs() as $key => $input) {
118  $named_inputs[$key] = $input->withNameFrom($source, $parent_name);
119  }
120 
121  return $named_inputs;
122  }
123 
129  protected function _isClientSideValueOk($value): bool
130  {
131  if (!is_array($value)) {
132  return false;
133  }
134  if (count($this->getInputs()) !== count($value)) {
135  return false;
136  }
137  foreach ($this->getInputs() as $key => $input) {
138  if (!array_key_exists($key, $value)) {
139  return false;
140  }
141  if (!$input->isClientSideValueOk($value[$key])) {
142  return false;
143  }
144  }
145  return true;
146  }
147 
151  public function getInputs(): array
152  {
153  return $this->inputs;
154  }
155 
161  protected function setInputs(array $inputs): void
162  {
163  $this->inputs = $inputs;
164  }
165 
169  abstract protected function applyOperationsTo($res): Result;
170 
174  abstract protected function setError(string $error): void;
175 
176  abstract protected function getLanguage(): \ilLanguage;
177 
178  abstract protected function getDataFactory(): DataFactory;
179 }
$res
Definition: ltiservices.php:69
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:47
setInputs(array $inputs)
This setter should be used instead of accessing $this->inputs directly.
Definition: Group.php:161
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:14
withInput(InputData $input)
Collects the input, applies trafos and forwards the input to its children and returns a new input gro...
Definition: Group.php:75
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
nameInputs(NameSource $source, string $parent_name)
Definition: Group.php:114
_isClientSideValueOk($value)
ATTENTION: This is not the same as.
Definition: Group.php:129
string $key
Consumer key/client ID value.
Definition: System.php:193
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
ilErrorHandling $error
Definition: class.ilias.php:55
language handling
Describes a source for input names.
Definition: NameSource.php:26
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setError(string $error)
This setter will be used by withInput() to set possible errors.
$source
Definition: metadata.php:93
$i
Definition: metadata.php:41