ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
RequestDataCollector.php
Go to the documentation of this file.
1<?php
2
20
21use GuzzleHttp\Psr7\UploadedFile;
29use Psr\Http\Message\ServerRequestInterface;
30
32{
33 use BaseGUIRequest;
34
35 public function __construct(
38 protected readonly FileUpload $upload
39 ) {
40 $this->initRequest($http, $refinery);
41 }
42
43 public function getRequest(): ServerRequestInterface
44 {
45 return $this->http->request();
46 }
47
51 public function getProcessedUploads(): array
52 {
53 $uploads = [];
54
55 if ($this->upload->hasUploads()) {
56 if (!$this->upload->hasBeenProcessed()) {
57 $this->upload->process();
58 }
59 $uploads = $this->upload->getResults();
60 }
61
62 return $uploads;
63 }
64
70 public function getUploadFilename(array $http_names, int $index): ?string
71 {
72 $uploaded_files = $this->http->request()->getUploadedFiles();
73
74 while (($current_key = array_shift($http_names)) !== null) {
75 if (!isset($uploaded_files[$current_key])) {
76 return null;
77 }
78
79 $uploaded_files = $uploaded_files[$current_key];
80
81 if (isset($uploaded_files[$index]) && $http_names === []) {
83 $file = $uploaded_files[$index];
84 $c = \Closure::bind(static function (UploadedFile $file): ?string {
85 return $file->file ?? null;
86 }, null, $file);
87
88 return $c($file);
89 }
90 }
91
92 return null;
93 }
94
95 public function upload(): FileUpload
96 {
97 return $this->upload;
98 }
99
100 public function isset(string $key): bool
101 {
102 return $this->raw($key) !== null;
103 }
104
105 public function hasRefId(): bool
106 {
107 return $this->raw('ref_id') !== null;
108 }
109
110 public function getRefId(): int
111 {
112 return $this->int('ref_id');
113 }
114
115 public function hasQuestionId(): bool
116 {
117 return $this->raw('q_id') !== null;
118 }
119
120 public function getQuestionId(): int
121 {
122 return $this->int('q_id');
123 }
124
128 public function getIds(): array
129 {
130 return $this->strArray('id');
131 }
132
136 public function raw(string $key): mixed
137 {
138 return $this->get($key, $this->refinery->identity());
139 }
140
141 public function float(string $key): float
142 {
143 try {
144 return $this->get($key, $this->refinery->kindlyTo()->float()) ?? 0.0;
146 return 0.0;
147 }
148 }
149
150 public function string(string $key): string
151 {
152 return $this->get($key, $this->refinery->kindlyTo()->string()) ?? '';
153 }
154
155 public function bool(string $key): ?bool
156 {
157 return $this->get($key, $this->refinery->kindlyTo()->bool());
158 }
159
160 public function getParsedBody(): object|array|null
161 {
162 return $this->http->request()->getParsedBody();
163 }
164
168 public function getUnitIds(): array
169 {
170 return $this->intArray('unit_ids');
171 }
172
176 public function getUnitCategoryIds(): array
177 {
178 return $this->intArray('category_ids');
179 }
180
181 public function getMatchingPairs(): array
182 {
183 if (!$this->http->wrapper()->post()->has('matching')) {
184 return [];
185 }
186
187 return $this->http->wrapper()->post()->retrieve(
188 'matching',
189 $this->refinery->byTrying([
190 $this->refinery->container()->mapValues(
191 $this->refinery->custom()->transformation(
192 fn(string $v): array => $this->refinery->container()->mapValues(
193 $this->refinery->kindlyTo()->int()
194 )->transform(json_decode($v))
195 )
196 ),
197 $this->refinery->always([])
198 ])
199 );
200 }
201
202 public function getNumericGapValue(
203 int $gap_index
204 ): ?float {
205 return $this->http->wrapper()->post()->retrieve(
206 "gap_{$gap_index}_numeric",
207 $this->refinery->byTrying([
208 $this->refinery->kindlyTo()->float(),
209 $this->refinery->always(null)
210 ])
211 );
212 }
213
214 public function getPostKeys(): array
215 {
216 return $this->http->wrapper()->post()->keys();
217 }
218
219 public function getCmdIndex(string $key): int|string|null
220 {
221 $cmd = $this->rawArray('cmd');
222 if (!isset($cmd[$key]) || !is_array($cmd[$key])) {
223 return null;
224 }
225 return key($cmd[$key]);
226 }
227
231 public function strArray(string $key, int $depth = 1): array
232 {
233 return $this->retrieveArray($key, $depth, $this->refinery->kindlyTo()->string());
234 }
235
239 public function floatArray(string $key): array
240 {
241 return $this->retrieveArray($key, 1, $this->refinery->kindlyTo()->float());
242 }
243
247 public function intArray(string $key): array
248 {
249 return $this->retrieveArray($key, 1, $this->refinery->kindlyTo()->int());
250 }
251
255 public function rawArray(string $key): array
256 {
257 return $this->retrieveArray($key, 1, $this->refinery->identity());
258 }
259
260 public function getRowIdParameter(string $key): string|int
261 {
262 return $this->get($key, $this->refinery->byTrying([
263 $this->refinery->kindlyTo()->int(),
264 $this->refinery->kindlyTo()->string(),
265 $this->refinery->custom()->transformation(fn(array $v): string|int => $v[0])
266 ]));
267 }
268
272 public function getMultiSelectionIds(string $key): array|string
273 {
274 $query = $this->http->wrapper()->query();
275
276 if (!$query->has($key)) {
277 return [];
278 }
279
280 return $query->retrieve(
281 $key,
282 $this->refinery->custom()->transformation(
283 static fn(array|string $value): array|string => $value === 'ALL_OBJECTS' || $value[0] === 'ALL_OBJECTS'
284 ? 'ALL_OBJECTS'
285 : array_map('intval', $value)
286 )
287 );
288 }
289
290 public function getNumericQuestionSolutionSubmit(): ?float
291 {
292 return $this->http->wrapper()->post()->retrieve(
293 'numeric_result',
294 $this->refinery->byTrying([
295 $this->refinery->kindlyTo()->float(),
296 $this->refinery->always(null)
297 ])
298 );
299 }
300
301 private function retrieveArray(string $key, int $depth, Transformation $transformation): array
302 {
303 $chain = $this->refinery->kindlyTo()->dictOf($transformation);
304 for ($i = 1; $i < $depth; $i++) {
305 $chain = $this->refinery->kindlyTo()->dictOf($chain);
306 }
307
308 return $this->get(
309 $key,
310 $this->refinery->byTrying([
311 $chain,
312 $this->refinery->always([])
313 ])
314 ) ?? [];
315 }
316}
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
trait BaseGUIRequest
Base gui request wrapper.
retrieveArray(string $key, int $depth, Transformation $transformation)
__construct(Services $http, Factory $refinery, protected readonly FileUpload $upload)
$http
Definition: deliver.php:30
$c
Definition: deliver.php:25
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A transformation is a function from one datatype to another.
static http()
Fetches the global http state from ILIAS.
initRequest(HTTP\Services $http, Refinery\Factory $refinery, ?array $passed_query_params=null, ?array $passed_post_data=null)
Query params and post data parameters are used for testing.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...