ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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 getPostKeys(): array
203 {
204 return $this->http->wrapper()->post()->keys();
205 }
206
207 public function getCmdIndex(string $key): int|string|null
208 {
209 $cmd = $this->rawArray('cmd');
210 if (!isset($cmd[$key]) || !is_array($cmd[$key])) {
211 return null;
212 }
213 return key($cmd[$key]);
214 }
215
219 public function strArray(string $key, int $depth = 1): array
220 {
221 return $this->retrieveArray($key, $depth, $this->refinery->kindlyTo()->string());
222 }
223
227 public function floatArray(string $key): array
228 {
229 return $this->retrieveArray($key, 1, $this->refinery->kindlyTo()->float());
230 }
231
235 public function intArray(string $key): array
236 {
237 return $this->retrieveArray($key, 1, $this->refinery->kindlyTo()->int());
238 }
239
243 public function rawArray(string $key): array
244 {
245 return $this->retrieveArray($key, 1, $this->refinery->identity());
246 }
247
248 public function getRowIdParameter(string $key): string|int
249 {
250 return $this->get($key, $this->refinery->byTrying([
251 $this->refinery->kindlyTo()->int(),
252 $this->refinery->kindlyTo()->string(),
253 $this->refinery->custom()->transformation(fn(array $v): string|int => $v[0])
254 ]));
255 }
256
260 public function getMultiSelectionIds(string $key): array|string
261 {
262 $query = $this->http->wrapper()->query();
263
264 if (!$query->has($key)) {
265 return [];
266 }
267
268 return $query->retrieve(
269 $key,
270 $this->refinery->custom()->transformation(
271 static fn(array|string $value): array|string => $value === 'ALL_OBJECTS' || $value[0] === 'ALL_OBJECTS'
272 ? 'ALL_OBJECTS'
273 : array_map('intval', $value)
274 )
275 );
276 }
277
278 private function retrieveArray(string $key, int $depth, Transformation $transformation): array
279 {
280 $chain = $this->refinery->kindlyTo()->dictOf($transformation);
281 for ($i = 1; $i < $depth; $i++) {
282 $chain = $this->refinery->kindlyTo()->dictOf($chain);
283 }
284
285 return $this->get(
286 $key,
287 $this->refinery->byTrying([
288 $chain,
289 $this->refinery->always([])
290 ])
291 ) ?? [];
292 }
293}
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
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...