ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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;
27use Psr\Http\Message\ServerRequestInterface;
28
29use function array_map;
30
32{
33 use BaseGUIRequest;
34
35 protected array $params;
36
37 public function __construct(
38 HTTPServices $http,
40 ) {
41 $this->initRequest($http, $refinery);
42 }
43
44 public function getRequest(): ServerRequestInterface
45 {
46 return $this->http->request();
47 }
48
49 public function isset(string $key): bool
50 {
51 return $this->raw($key) !== null;
52 }
53
54 public function hasRefId(): bool
55 {
56 return $this->raw('ref_id') !== null;
57 }
58
59 public function getRefId(): int
60 {
61 return $this->int("ref_id");
62 }
63
65 public function getIds(): array
66 {
67 return $this->strArray("id");
68 }
69
70 public function hasQuestionId(): bool
71 {
72 return $this->raw('q_id') !== null;
73 }
74
75 public function getQuestionId(): int
76 {
77 return $this->int('q_id');
78 }
79
80 public function getQuestionIds(): array
81 {
82 return $this->intArray('q_id');
83 }
84
85 public function getNextCommand(): string
86 {
87 return $this->str('nextCommand');
88 }
89
90 public function getActiveId(): int
91 {
92 return $this->int('active_id');
93 }
94
95 public function getPassId(): int
96 {
97 return $this->int('pass_id');
98 }
99
100 public function retrieveBoolFromPost(string $key): ?bool
101 {
102 if (!$this->http->wrapper()->post()->has($key)) {
103 return null;
104 }
105
106 return $this->http->wrapper()->post()->retrieve(
107 $key,
108 $this->refinery->byTrying([
109 $this->refinery->kindlyTo()->bool(),
110 $this->refinery->always(null)
111 ])
112 );
113 }
114
115 public function isInstanceResponseRequested(): bool
116 {
117 if (!$this->http->wrapper()->query()->has('instresp')) {
118 return false;
119 }
120
121 return $this->http->wrapper()->query()->retrieve(
122 'instresp',
123 $this->refinery->byTrying([
124 $this->refinery->kindlyTo()->bool(),
125 $this->refinery->always(false)
126 ])
127 );
128 }
129
133 public function raw(string $key): mixed
134 {
135 return $this->get($key, $this->refinery->identity());
136 }
137
138 public function strVal(string $key): string
139 {
140 return $this->str($key);
141 }
142
143 public function getParsedBody(): ?array
144 {
145 return $this->http->request()->getParsedBody();
146 }
147
151 public function getPostKeys(): array
152 {
153 return $this->http->wrapper()->post()->keys();
154 }
155
156 public function isPostRequest(): bool
157 {
158 return $this->http->request()->getMethod() === 'POST';
159 }
160
164 public function retrieveArrayOfStringsFromPost(string $key): array
165 {
166 return $this->retrieveArrayFromPost($key, $this->refinery->kindlyTo()->string());
167 }
168
172 public function retrieveArrayOfIntsFromPost(string $key): array
173 {
174 return $this->retrieveArrayFromPost($key, $this->refinery->kindlyTo()->int());
175 }
176
177 private function retrieveArrayFromPost(string $key, Transformation $transformation): array
178 {
179 return $this->http->wrapper()->post()->retrieve(
180 $key,
181 $this->refinery->byTrying([
182 $this->refinery->kindlyTo()->listOf($transformation),
183 $this->refinery->always([])
184 ])
185 );
186 }
187
191 public function getMultiSelectionIds(string $key): array|string
192 {
193 $p = $this->http->wrapper()->query();
194 $r = $this->refinery;
195
196 if (!$p->has($key)) {
197 return [];
198 }
199
200 return $p->retrieve(
201 $key,
202 $r->custom()->transformation(function ($value) {
203 return $value === 'ALL_OBJECTS' || $value[0] === 'ALL_OBJECTS' ? 'ALL_OBJECTS' : array_map('intval', $value);
204 })
205 );
206 }
207}
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.