ILIAS  release_7 Revision v7.30-3-g800a261c036
Group.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2017 Timon Amstutz <timon.amstutz@ilub.unibe.ch> Extended GPL, see
4docs/LICENSE */
5
7
14use ILIAS\Data\Factory as DataFactory;
15
19class Group extends Input implements C\Input\Field\Group
20{
22
28 protected $inputs = [];
29
33 protected $lng;
34
45 public function __construct(
46 DataFactory $data_factory,
47 \ILIAS\Refinery\Factory $refinery,
49 array $inputs,
50 string $label,
51 string $byline = null
52 ) {
53 parent::__construct($data_factory, $refinery, $label, $byline);
54 $this->checkArgListElements("inputs", $inputs, InputInternal::class);
55 $this->inputs = $inputs;
56 $this->lng = $lng;
57 }
58
59 public function withDisabled($is_disabled)
60 {
61 $clone = parent::withDisabled($is_disabled);
62 $clone->inputs = array_map(function ($i) use ($is_disabled) {
63 return $i->withDisabled($is_disabled);
64 }, $this->inputs);
65 return $clone;
66 }
67
68 public function withRequired($is_required)
69 {
70 $clone = parent::withRequired($is_required);
71 $inputs = [];
72 $clone->inputs = array_map(function ($i) use ($is_required) {
73 return $i->withRequired($is_required);
74 }, $this->inputs);
75 return $clone;
76 }
77
78 public function isRequired()
79 {
80 if($this->is_required) {
81 return true;
82 }
83 foreach ($this->getInputs() as $input) {
84 if ($input->isRequired()) {
85 return true;
86 }
87 }
88 return false;
89 }
90
91 public function withOnUpdate(Signal $signal)
92 {
93 $clone = parent::withOnUpdate($signal);
94 $clone->inputs = array_map(function ($i) use ($signal) {
95 return $i->withOnUpdate($signal);
96 }, $this->inputs);
97 return $clone;
98 }
99
103 protected function isClientSideValueOk($value) : bool
104 {
105 if (!is_array($value)) {
106 return false;
107 }
108 if (count($this->getInputs()) !== count($value)) {
109 return false;
110 }
111 foreach ($this->getInputs() as $key => $input) {
112 if (!array_key_exists($key, $value)) {
113 return false;
114 }
115 if (!$input->isClientSideValueOk($value[$key])) {
116 return false;
117 }
118 }
119 return true;
120 }
121
127 public function getValue()
128 {
129 return array_map(function ($i) {
130 return $i->getValue();
131 }, $this->inputs);
132 }
133
134
144 public function withValue($value)
145 {
146 $this->checkArg("value", $this->isClientSideValueOk($value), "Display value does not match input type.");
147 $clone = clone $this;
148 foreach ($this->inputs as $k => $i) {
149 $clone->inputs[$k] = $i->withValue($value[$k]);
150 }
151 return $clone;
152 }
153
160 public function withInput(InputData $post_input)
161 {
162 if (sizeof($this->getInputs()) === 0) {
163 return $this;
164 }
165
169 $clone = clone $this;
170
171 $inputs = [];
172 $contents = [];
173 $error = false;
174
175 foreach ($this->getInputs() as $key => $input) {
176 $inputs[$key] = $input->withInput($post_input);
177 $content = $inputs[$key]->getContent();
178 if ($content->isError()) {
179 $error = true;
180 } else {
181 $contents[$key] = $content->value();
182 }
183 }
184
185 $clone->inputs = $inputs;
186 if ($error) {
187 $clone->content = $clone->data_factory->error($this->lng->txt("ui_error_in_group"));
188 } else {
189 $clone->content = $clone->applyOperationsTo($contents);
190 }
191
192 if ($clone->content->isError()) {
193 $clone = $clone->withError("" . $clone->content->error());
194 }
195
196 return $clone;
197 }
198
202 public function withNameFrom(NameSource $source)
203 {
204 $clone = parent::withNameFrom($source);
208 $named_inputs = [];
209 foreach ($this->getInputs() as $key => $input) {
210 $named_inputs[$key] = $input->withNameFrom($source);
211 }
212
213 $clone->inputs = $named_inputs;
214
215 return $clone;
216 }
217
221 public function getInputs()
222 {
223 return $this->inputs;
224 }
225
229 protected function getConstraintForRequirement()
230 {
231 return null;
232 }
233
237 public function getUpdateOnLoadCode() : \Closure
238 {
239 return function () {
240 /*
241 * Currently, there is no use case for Group here. The single Inputs
242 * within the Group are responsible for handling getUpdateOnLoadCode().
243 */
244 };
245 }
246
250 public function getContent()
251 {
252 if (0 === count($this->getInputs())) {
253 return new \ILIAS\Data\Result\Ok([]);
254 }
255 return parent::getContent();
256 }
257}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:144
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:127
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, \ilLanguage $lng, array $inputs, string $label, string $byline=null)
Group constructor.
Definition: Group.php:45
language handling
This describes a group of inputs.
Definition: Group.php:14
This describes commonalities between all inputs.
Definition: Input.php:32
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
Describes a source for input names.
Definition: NameSource.php:11
$i
Definition: metadata.php:24
$source
Definition: metadata.php:76
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
checkArg($which, $check, $message)
/** Throw an InvalidArgumentException containing the message if $check is false.
checkArgListElements($which, array &$values, $classes)
Check every element of the list if it is an instance of one of the given classes.
trait ComponentHelper
Provides common functionality for component implementations.
Class ChatMainBarProvider \MainMenu\Provider.
$lng