ILIAS  trunk Revision v11.0_alpha-1811-gd2d5443e411
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Group.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
31 
35 trait Group
36 {
37  use ComponentHelper;
38 
42  protected array $inputs = [];
43 
49  public function getValue()
50  {
51  return array_map(fn($i) => $i->getValue(), $this->inputs);
52  }
53 
61  public function withValue($value): self
62  {
63  $this->checkArg("value", $this->isClientSideValueOk($value), "Display value does not match input type.");
64  $clone = clone $this;
65  foreach ($this->getInputs() as $k => $i) {
66  $clone->inputs[$k] = $i->withValue($value[$k]);
67  }
68  return $clone;
69  }
70 
77  public function withInput(InputData $input): self
78  {
79  if (empty($this->getInputs())) {
80  return $this;
81  }
82 
83  $clone = clone $this;
84 
85  $inputs = [];
86  $contents = [];
87  $error = false;
88 
89  foreach ($this->getInputs() as $key => $in) {
90  $inputs[$key] = $in->withInput($input);
91  $content = $inputs[$key]->getContent();
92  if ($content->isError()) {
93  $error = true;
94  } else {
95  $contents[$key] = $content->value();
96  }
97  }
98 
99  $clone->inputs = $inputs;
100  if ($error) {
101  $clone->content = $clone->getDataFactory()->error($this->getLanguage()->txt("ui_error_in_group"));
102  } else {
103  $clone->content = $clone->applyOperationsTo($contents);
104  }
105 
106  if ($clone->content->isError()) {
107  $clone->setError("" . $clone->content->error());
108  }
109 
110  return $clone;
111  }
112 
116  protected function nameInputs(NameSource $source, string $parent_name): array
117  {
118  $named_inputs = [];
119  foreach ($this->getInputs() as $key => $input) {
120  $named_inputs[$key] = $input->withNameFrom($source, $parent_name);
121  }
122 
123  return $named_inputs;
124  }
125 
131  protected function _isClientSideValueOk($value): bool
132  {
133  if (!is_array($value)) {
134  return false;
135  }
136  if (count($this->getInputs()) !== count($value)) {
137  return false;
138  }
139  foreach ($this->getInputs() as $key => $input) {
140  if (!array_key_exists($key, $value)) {
141  return false;
142  }
143  if (!$input->isClientSideValueOk($value[$key])) {
144  return false;
145  }
146  }
147  return true;
148  }
149 
153  public function getInputs(): array
154  {
155  return $this->inputs;
156  }
157 
163  protected function setInputs(array $inputs): void
164  {
165  $this->inputs = $inputs;
166  }
167 
171  abstract protected function applyOperationsTo($res): Result;
172 
176  abstract protected function setError(string $error): void;
177 
178  abstract protected function getLanguage(): Language;
179 
180  abstract protected function getDataFactory(): DataFactory;
181 }
$res
Definition: ltiservices.php:66
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.
Definition: Group.php:49
setInputs(array $inputs)
This setter should be used instead of accessing $this->inputs directly.
Definition: Group.php:163
withInput(InputData $input)
Collects the input, applies trafos and forwards the input to its children and returns a new input gro...
Definition: Group.php:77
nameInputs(NameSource $source, string $parent_name)
Definition: Group.php:116
_isClientSideValueOk($value)
ATTENTION: This is not the same as.
Definition: Group.php:131
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
ilErrorHandling $error
Definition: class.ilias.php:69
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.