ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
GroupHelper.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
10use ILIAS\Data\Factory as DataFactory;
11use ILIAS\Validation\Factory as ValidationFactory;
12use ILIAS\Transformation\Factory as TransformationFactory;
13
22{
28 protected $inputs = [];
29
30
34 protected function isClientSideValueOk($value)
35 {
36 return true;
37 }
38
39
45 public function getGroupValues()
46 {
47 $values = [];
48 foreach ($this->getInputs() as $key => $input) {
49 $values[$key] = $input->getValue();
50 }
51
52 return $values;
53 }
54
55
65 public function withGroupValues($values)
66 {
67 $this->checkArg("value", $this->isClientSideValueOk($values), "Values given do not match given inputs in group.");
68
69 $clone = clone $this;
70 $inputs = [];
71
72 foreach ($this->getInputs() as $key => $input) {
73 $inputs[$key] = $input->withValue($values[$key]);
74 }
75
76 $clone->inputs = $inputs;
77
78 return $clone;
79 }
80
81
89 protected function isClientGroupSideValueOk($value)
90 {
91 if (!is_array($value)) {
92 return false;
93 }
94 if (!sizeof($this->getInputs() == sizeof($value))) {
95 return false;
96 }
97
98 foreach ($this->getInputs() as $key => $input) {
99 if (!array_key_exists($key, $value)) {
100 return false;
101 }
102 }
103
104 return true;
105 }
106
107
114 public function withInput(PostData $post_input)
115 {
116 $clone = parent::withInput($post_input);
120 if ($clone->getError()) {
121 return $clone;
122 }
123
124 return $clone->withGroupInput($post_input);
125 }
126
127
133 protected function withGroupInput(PostData $post_input)
134 {
135 $clone = $this;
136
137 if (sizeof($this->getInputs()) === 0) {
138 return $clone;
139 }
140
141 $inputs = [];
142 $values = [];
143 $error = false;
144 foreach ($this->getInputs() as $key => $input) {
145 $filled = $input->withInput($post_input);
149 //Todo: Is this correct here or should it be getValue? Design decision...
150 $content = $filled->getContent();
151 if ($content->isOk()) {
152 $values[$key] = $content->value();
153 } else {
154 $error = true;
155 }
156 $inputs[$key] = $filled;
157 }
158 $clone->inputs = $inputs;
159 if ($error) {
160 //Todo: Improve this error message on the group
161 $clone->content = $clone->data_factory->error("error in grouped input");
162
163 return $clone;
164 }
165
166 if ($clone->content->value()) {
167 $group_content = $clone->applyOperationsTo($values);
168 $f = $this->data_factory;
169 $clone->content = $clone->content->then(function ($value) use ($f, $group_content) {
170 if ($group_content->isError()) {
171 return $f->error($group_content->error());
172 }
173
174 return $f->ok(["value" => $value, "group_values" => $group_content->value()]);
175 });
176 } else {
177 $clone->content = $clone->applyOperationsTo($values);
178 }
179
180 if ($clone->content->isError()) {
181 return $clone->withError($clone->content->error());
182 }
183
184 return $clone;
185 }
186
187
191 public function withNameFrom(NameSource $source)
192 {
193 $clone = parent::withNameFrom($source);
197 $named_inputs = [];
198 foreach ($this->getInputs() as $key => $input) {
199 $named_inputs[$key] = $input->withNameFrom($source);
200 }
201
202 $clone->inputs = $named_inputs;
203
204 return $clone;
205 }
206
207
211 public function getInputs()
212 {
213 return $this->inputs;
214 }
215
219 protected function getConstraintForRequirement()
220 {
221 return null;
222 }
223}
$source
Definition: linkback.php:22
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:15
Factory for basic transformations.
Definition: Factory.php:12
$key
Definition: croninfo.php:18
Describes a source for input names.
Definition: NameSource.php:11
Describes how Input-Elements want to interact with posted data.
Definition: PostData.php:13
trait GroupHelper
The code of Group is used in Checkbox, e.g., but a checkbox is not a group.
Definition: GroupHelper.php:22
withGroupValues($values)
Get an input like this with children with other values displayed on the client side.
Definition: GroupHelper.php:65
getGroupValues()
Get the value that is displayed in the groups input as Generator instance.
Definition: GroupHelper.php:45
isClientGroupSideValueOk($value)
Default implementation for groups.
Definition: GroupHelper.php:89
checkArg($which, $check, $message)
/** Throw an InvalidArgumentException containing the message if $check is false.
$values