ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Group.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\UI\Implementation\Component\ComponentHelper;
24use ILIAS\Refinery\Factory as Refinery;
26use ILIAS\Data\Factory as DataFactory;
31
35trait 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}
Builds data types.
Definition: Factory.php:36
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:31
ilErrorHandling $error
Definition: class.ilias.php:69
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
Describes a source for input names.
Definition: NameSource.php:27
$res
Definition: ltiservices.php:69
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
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
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:49
_isClientSideValueOk($value)
ATTENTION: This is not the same as.
Definition: Group.php:131
setError(string $error)
This setter will be used by withInput() to set possible errors.
setInputs(array $inputs)
This setter should be used instead of accessing $this->inputs directly.
Definition: Group.php:163