ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
RequestDataCollector.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Test;
22
23use ILIAS\HTTP\Services as HTTPServices;
24use ILIAS\Refinery\Factory as Refinery;
28use Psr\Http\Message\ServerRequestInterface;
29
31{
32 use BaseGUIRequest;
33
34 protected array $params;
35
36 public function __construct(
37 HTTPServices $http,
39 ) {
40 $this->initRequest($http, $refinery);
41 }
42
43 public function getRequest(): ServerRequestInterface
44 {
45 return $this->http->request();
46 }
47
48 public function isset(string $key): bool
49 {
50 return $this->raw($key) !== null;
51 }
52
53 public function hasRefId(): bool
54 {
55 return $this->raw('ref_id') !== null;
56 }
57
58 public function getRefId(): int
59 {
60 return $this->int('ref_id');
61 }
62
64 public function getIds(): array
65 {
66 return $this->strArray('id');
67 }
68
69 public function hasQuestionId(): bool
70 {
71 return $this->raw('q_id') !== null;
72 }
73
74 public function getQuestionId(): int
75 {
76 return $this->int('q_id');
77 }
78
79 public function getQuestionIds(): array
80 {
81 return $this->intArray('q_id');
82 }
83
84 public function getNextCommand(): string
85 {
86 return $this->str('nextCommand');
87 }
88
89 public function getActiveId(): int
90 {
91 return $this->int('active_id');
92 }
93
94 public function getPassId(): int
95 {
96 return $this->int('pass_id');
97 }
98
99 public function retrieveBoolFromPost(string $key): ?bool
100 {
101 if (!$this->http->wrapper()->post()->has($key)) {
102 return null;
103 }
104
105 return $this->http->wrapper()->post()->retrieve(
106 $key,
107 $this->refinery->byTrying([
108 $this->refinery->kindlyTo()->bool(),
109 $this->refinery->always(null)
110 ])
111 );
112 }
113
114 public function isInstanceResponseRequested(): bool
115 {
116 if (!$this->http->wrapper()->query()->has('instresp')) {
117 return false;
118 }
119
120 return $this->http->wrapper()->query()->retrieve(
121 'instresp',
122 $this->refinery->byTrying([
123 $this->refinery->kindlyTo()->bool(),
124 $this->refinery->always(false)
125 ])
126 );
127 }
128
132 public function raw(string $key): mixed
133 {
134 return $this->get($key, $this->refinery->identity());
135 }
136
137 public function strVal(string $key): string
138 {
139 return $this->str($key);
140 }
141
142 public function getParsedBody(): ?array
143 {
144 return $this->http->request()->getParsedBody();
145 }
146
150 public function getPostKeys(): array
151 {
152 return $this->http->wrapper()->post()->keys();
153 }
154
155 public function isPostRequest(): bool
156 {
157 return $this->http->request()->getMethod() === 'POST';
158 }
159
163 public function retrieveArrayOfStringsFromPost(string $key): array
164 {
165 return $this->retrieveArrayFromPost($key, $this->refinery->kindlyTo()->string());
166 }
167
171 public function retrieveArrayOfIntsFromPost(string $key): array
172 {
173 return $this->retrieveArrayFromPost($key, $this->refinery->kindlyTo()->int());
174 }
175
176 private function retrieveArrayFromPost(string $key, Transformation $transformation): array
177 {
178 return $this->http->wrapper()->post()->retrieve(
179 $key,
180 $this->refinery->byTrying([
181 $this->refinery->kindlyTo()->listOf($transformation),
182 $this->refinery->always([])
183 ])
184 );
185 }
186
187 public function getRowIdParameter(string $key): string|int
188 {
189 return $this->get($key, $this->refinery->byTrying([
190 $this->refinery->kindlyTo()->int(),
191 $this->refinery->kindlyTo()->string(),
192 $this->refinery->custom()->transformation(fn(array $v): string|int => $v[0])
193 ]));
194 }
195
199 public function getMultiSelectionIds(string $key): array|string
200 {
201 $query = $this->http->wrapper()->query();
202
203 if (!$query->has($key)) {
204 return [];
205 }
206
207 return $query->retrieve(
208 $key,
209 $this->refinery->custom()->transformation(
210 static fn(array|string $value): array|string => $value === 'ALL_OBJECTS' || $value[0] === 'ALL_OBJECTS'
211 ? 'ALL_OBJECTS'
212 : array_map('intval', $value)
213 )
214 );
215 }
216}
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
trait BaseGUIRequest
Base gui request wrapper.
retrieveArrayFromPost(string $key, Transformation $transformation)
__construct(HTTPServices $http, Refinery $refinery)
$http
Definition: deliver.php:30
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.