ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Group.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
21 
28 
32 trait Group
33 {
34  use ComponentHelper;
35 
39  protected array $inputs = [];
40 
46  public function getValue()
47  {
48  return array_map(fn($i) => $i->getValue(), $this->inputs);
49  }
50 
58  public function withValue($value): self
59  {
60  $this->checkArg("value", $this->isClientSideValueOk($value), "Display value does not match input type.");
61  $clone = clone $this;
62  foreach ($this->getInputs() as $k => $i) {
63  $clone->inputs[$k] = $i->withValue($value[$k]);
64  }
65  return $clone;
66  }
67 
74  public function withInput(InputData $input): self
75  {
76  if (empty($this->getInputs())) {
77  return $this;
78  }
79 
80  $clone = clone $this;
81 
82  $inputs = [];
83  $contents = [];
84  $error = false;
85 
86  foreach ($this->getInputs() as $key => $in) {
87  $inputs[$key] = $in->withInput($input);
88  $content = $inputs[$key]->getContent();
89  if ($content->isError()) {
90  $error = true;
91  } else {
92  $contents[$key] = $content->value();
93  }
94  }
95 
96  $clone->inputs = $inputs;
97  if ($error) {
98  $clone->content = $clone->getDataFactory()->error($this->getLanguage()->txt("ui_error_in_group"));
99  } else {
100  $clone->content = $clone->applyOperationsTo($contents);
101  }
102 
103  if ($clone->content->isError()) {
104  $clone->setError("" . $clone->content->error());
105  }
106 
107  return $clone;
108  }
109 
113  protected function nameInputs(NameSource $source, string $parent_name): array
114  {
115  $named_inputs = [];
116  foreach ($this->getInputs() as $key => $input) {
117  $named_inputs[$key] = $input->withNameFrom($source, $parent_name);
118  }
119 
120  return $named_inputs;
121  }
122 
128  protected function _isClientSideValueOk($value): bool
129  {
130  if (!is_array($value)) {
131  return false;
132  }
133  if (count($this->getInputs()) !== count($value)) {
134  return false;
135  }
136  foreach ($this->getInputs() as $key => $input) {
137  if (!array_key_exists($key, $value)) {
138  return false;
139  }
140  if (!$input->isClientSideValueOk($value[$key])) {
141  return false;
142  }
143  }
144  return true;
145  }
146 
150  public function getInputs(): array
151  {
152  return $this->inputs;
153  }
154 
160  protected function setInputs(array $inputs): void
161  {
162  $this->inputs = $inputs;
163  }
164 
168  abstract protected function applyOperationsTo($res): Result;
169 
173  abstract protected function setError(string $error): void;
174 
175  abstract protected function getLanguage(): \ilLanguage;
176 
177  abstract protected function getDataFactory(): DataFactory;
178 }
$res
Definition: ltiservices.php:69
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:46
setInputs(array $inputs)
This setter should be used instead of accessing $this->inputs directly.
Definition: Group.php:160
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:74
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
nameInputs(NameSource $source, string $parent_name)
Definition: Group.php:113
_isClientSideValueOk($value)
ATTENTION: This is not the same as.
Definition: Group.php:128
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:58
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.