ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
trait.BaseGUIRequest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Repository;
22
23use ILIAS\HTTP;
25
34{
35 protected HTTP\Services $http;
36 protected Refinery\Factory $refinery;
37 protected ?array $passed_query_params;
38 protected ?array $passed_post_data;
39
48 protected function initRequest(
51 ?array $passed_query_params = null,
52 ?array $passed_post_data = null
53 ): void {
54 $this->http = $http;
55 $this->refinery = $refinery;
56 $this->passed_post_data = $passed_post_data;
57 $this->passed_query_params = $passed_query_params;
58 }
59
60 // get integer parameter kindly
61 public function int(string $key): int
62 {
63 if ($this->isArray($key) || $this->str($key) === "") {
64 return 0;
65 }
66 $t = $this->refinery->kindlyTo()->int();
67 return (int) ($this->get($key, $t) ?? 0);
68 }
69
70 // get integer array kindly
71 protected function intArray(string $key): array
72 {
73 if (!$this->isArray($key)) {
74 return [];
75 }
76 $t = $this->refinery->custom()->transformation(
77 function ($arr) {
78 // keep keys(!), transform all values to int
79 return array_column(
80 array_map(
81 static function ($k, $v): array {
82 return [$k, (int) $v];
83 },
84 array_keys($arr),
85 $arr
86 ),
87 1,
88 0
89 );
90 }
91 );
92 return (array) ($this->get($key, $t) ?? []);
93 }
94
95 protected function strip(string $input): string
96 {
97 // see https://www.ilias.de/mantis/view.php?id=19727
98 $str = \ilUtil::stripSlashes($input);
99 if ($str !== $input) {
100 $str = \ilUtil::stripSlashes(str_replace("<", "< ", $input));
101 }
102 return $str;
103 }
104
105 // get string parameter kindly
106 protected function str(string $key): string
107 {
108 if ($this->isArray($key)) {
109 return "";
110 }
111 $t = $this->refinery->kindlyTo()->string();
112 return $this->strip((string) ($this->get($key, $t) ?? ""));
113 }
114
115 // get string array kindly
116 protected function strArray(string $key): array
117 {
118 if (!$this->isArray($key)) {
119 return [];
120 }
121 $t = $this->refinery->custom()->transformation(
122 function ($arr) {
123 // keep keys(!), transform all values to string
124 return array_column(
125 array_map(
126 function ($k, $v): array {
127 if (is_array($v)) {
128 $v = "";
129 }
130 return [$k, $this->strip((string) $v)];
131 },
132 array_keys($arr),
133 $arr
134 ),
135 1,
136 0
137 );
138 }
139 );
140 return (array) ($this->get($key, $t) ?? []);
141 }
142
143 // get string array kindly
144 protected function arrayArray(string $key): array
145 {
146 if (!$this->isArray($key)) {
147 return [];
148 }
149 $t = $this->refinery->custom()->transformation(
150 function ($arr) {
151 // keep keys(!), transform all values to string
152 return array_column(
153 array_map(
154 static function ($k, $v): array {
155 return [$k, (array) $v];
156 },
157 array_keys($arr),
158 $arr
159 ),
160 1,
161 0
162 );
163 }
164 );
165 return (array) ($this->get($key, $t) ?? []);
166 }
167
171 protected function isArray(string $key): bool
172 {
173 if ($this->passed_query_params === null && $this->passed_post_data === null) {
174 $no_transform = $this->refinery->identity();
175 $w = $this->http->wrapper();
176 if ($w->post()->has($key)) {
177 return is_array($w->post()->retrieve($key, $no_transform));
178 }
179 if ($w->query()->has($key)) {
180 return is_array($w->query()->retrieve($key, $no_transform));
181 }
182 }
183 if (isset($this->passed_post_data[$key])) {
184 return is_array($this->passed_post_data[$key]);
185 }
186 if (isset($this->passed_query_params[$key])) {
187 return is_array($this->passed_query_params[$key]);
188 }
189 return false;
190 }
191
195 protected function raw(string $key)
196 {
197 $no_transform = $this->refinery->identity();
198 return $this->get($key, $no_transform);
199 }
200
201
202
209 protected function get(string $key, Refinery\Transformation $t)
210 {
211 if ($this->passed_query_params === null && $this->passed_post_data === null) {
212 $w = $this->http->wrapper();
213 if ($w->post()->has($key)) {
214 return $w->post()->retrieve($key, $t);
215 }
216 if ($w->query()->has($key)) {
217 return $w->query()->retrieve($key, $t);
218 }
219 }
220 if (isset($this->passed_post_data[$key])) {
221 return $t->transform($this->passed_post_data[$key]);
222 }
223 if (isset($this->passed_query_params[$key])) {
224 return $t->transform($this->passed_query_params[$key]);
225 }
226 return null;
227 }
228}
Builds data types.
Definition: Factory.php:36
trait BaseGUIRequest
Base gui request wrapper.
Refinery Factory $refinery
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
$http
Definition: deliver.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
isArray(string $key)
Check if parameter is an array.
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.