Skip to content

Commit 6def820

Browse files
author
Carlos Garcia
committed
Add inputOrQuery and queryOrInput methods; deprecate existing get methods
1 parent 71ab38a commit 6def820

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

Core/Request.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
final class Request
2727
{
2828
const METHOD_GET = 'GET';
29+
const METHOD_PATCH = 'PATCH';
2930
const METHOD_POST = 'POST';
3031
const METHOD_PUT = 'PUT';
31-
const METHOD_PATCH = 'PATCH';
32-
3332

3433
/** @var SubRequest */
3534
public $cookies;
@@ -118,6 +117,9 @@ public function fullUrl(): string
118117
return $this->protocol() . '://' . $this->host() . $this->urlWithQuery();
119118
}
120119

120+
/**
121+
* @deprecated use inputOrQuery() or queryOrInput() instead
122+
*/
121123
public function get(string $key, $default = null): ?string
122124
{
123125
if ($this->request->has($key)) {
@@ -127,6 +129,9 @@ public function get(string $key, $default = null): ?string
127129
return $this->query->get($key, $default);
128130
}
129131

132+
/**
133+
* @deprecated use inputOrQuery() or queryOrInput() instead
134+
*/
130135
public function getArray(string $key, bool $allowNull = true): ?array
131136
{
132137
if ($this->request->has($key)) {
@@ -136,6 +141,9 @@ public function getArray(string $key, bool $allowNull = true): ?array
136141
return $this->query->getArray($key, $allowNull);
137142
}
138143

144+
/**
145+
* @deprecated use inputOrQuery() or queryOrInput() instead
146+
*/
139147
public function getAlnum(string $key): string
140148
{
141149
if ($this->request->has($key)) {
@@ -145,6 +153,9 @@ public function getAlnum(string $key): string
145153
return $this->query->getAlnum($key);
146154
}
147155

156+
/**
157+
* @deprecated use inputOrQuery() or queryOrInput() instead
158+
*/
148159
public function getBool(string $key, bool $allowNull = true): ?bool
149160
{
150161
if ($this->request->has($key)) {
@@ -154,6 +165,9 @@ public function getBool(string $key, bool $allowNull = true): ?bool
154165
return $this->query->getBool($key, $allowNull);
155166
}
156167

168+
/**
169+
* @deprecated use inputOrQuery() or queryOrInput() instead
170+
*/
157171
public function getDate(string $key, bool $allowNull = true): ?string
158172
{
159173
if ($this->request->has($key)) {
@@ -163,6 +177,9 @@ public function getDate(string $key, bool $allowNull = true): ?string
163177
return $this->query->getDate($key, $allowNull);
164178
}
165179

180+
/**
181+
* @deprecated use inputOrQuery() or queryOrInput() instead
182+
*/
166183
public function getDateTime(string $key, bool $allowNull = true): ?string
167184
{
168185
if ($this->request->has($key)) {
@@ -172,6 +189,9 @@ public function getDateTime(string $key, bool $allowNull = true): ?string
172189
return $this->query->getDateTime($key, $allowNull);
173190
}
174191

192+
/**
193+
* @deprecated use inputOrQuery() or queryOrInput() instead
194+
*/
175195
public function getEmail(string $key, bool $allowNull = true): ?string
176196
{
177197
if ($this->request->has($key)) {
@@ -181,6 +201,9 @@ public function getEmail(string $key, bool $allowNull = true): ?string
181201
return $this->query->getEmail($key, $allowNull);
182202
}
183203

204+
/**
205+
* @deprecated use inputOrQuery() or queryOrInput() instead
206+
*/
184207
public function getFloat(string $key, bool $allowNull = true): ?float
185208
{
186209
if ($this->request->has($key)) {
@@ -190,6 +213,9 @@ public function getFloat(string $key, bool $allowNull = true): ?float
190213
return $this->query->getFloat($key, $allowNull);
191214
}
192215

216+
/**
217+
* @deprecated use inputOrQuery() or queryOrInput() instead
218+
*/
193219
public function getHour(string $key, bool $allowNull = true): ?string
194220
{
195221
if ($this->request->has($key)) {
@@ -199,6 +225,9 @@ public function getHour(string $key, bool $allowNull = true): ?string
199225
return $this->query->getHour($key, $allowNull);
200226
}
201227

228+
/**
229+
* @deprecated use inputOrQuery() or queryOrInput() instead
230+
*/
202231
public function getInt(string $key, bool $allowNull = true): ?int
203232
{
204233
if ($this->request->has($key)) {
@@ -217,6 +246,9 @@ public function getMethod(): string
217246
return $this->method();
218247
}
219248

249+
/**
250+
* @deprecated use inputOrQuery() or queryOrInput() instead
251+
*/
220252
public function getOnly(string $key, array $values): ?string
221253
{
222254
if ($this->request->has($key)) {
@@ -226,6 +258,9 @@ public function getOnly(string $key, array $values): ?string
226258
return $this->query->getOnly($key, $values);
227259
}
228260

261+
/**
262+
* @deprecated use inputOrQuery() or queryOrInput() instead
263+
*/
229264
public function getString(string $key, bool $allowNull = true): ?string
230265
{
231266
if ($this->request->has($key)) {
@@ -235,6 +270,9 @@ public function getString(string $key, bool $allowNull = true): ?string
235270
return $this->query->getString($key, $allowNull);
236271
}
237272

273+
/**
274+
* @deprecated use inputOrQuery() or queryOrInput() instead
275+
*/
238276
public function getUrl(string $key, bool $allowNull = true): ?string
239277
{
240278
if ($this->request->has($key)) {
@@ -281,6 +319,13 @@ public function input(string $key, $default = null): ?string
281319
return $this->request->get($key, $default);
282320
}
283321

322+
public function inputOrQuery(string $key, $default = null): ?string
323+
{
324+
return $this->request->has($key) ?
325+
$this->request->get($key) :
326+
$this->query->get($key, $default);
327+
}
328+
284329
public function ip(): string
285330
{
286331
foreach (['HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR'] as $field) {
@@ -355,6 +400,13 @@ public function query(string $key, $default = null): ?string
355400
return $this->query->get($key, $default);
356401
}
357402

403+
public function queryOrInput(string $key, $default = null): ?string
404+
{
405+
return $this->query->has($key) ?
406+
$this->query->get($key) :
407+
$this->request->get($key, $default);
408+
}
409+
358410
public function isSecure(): bool
359411
{
360412
return $this->protocol() === 'https';

Test/Core/RequestTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,36 @@ public function testParseRequestData(): void
661661
}
662662
}
663663

664+
public function testInputOrQuery(): void
665+
{
666+
$request = $this->createRequest([
667+
'query' => ['param1' => 'query_value', 'shared' => 'query_shared'],
668+
'request' => ['param2' => 'request_value', 'shared' => 'request_shared']
669+
]);
670+
671+
// Prioridad a request sobre query
672+
$this->assertEquals('request_value', $request->inputOrQuery('param2'));
673+
$this->assertEquals('query_value', $request->inputOrQuery('param1'));
674+
$this->assertEquals('request_shared', $request->inputOrQuery('shared'));
675+
$this->assertNull($request->inputOrQuery('nonexistent'));
676+
$this->assertEquals('default', $request->inputOrQuery('nonexistent', 'default'));
677+
}
678+
679+
public function testQueryOrInput(): void
680+
{
681+
$request = $this->createRequest([
682+
'query' => ['param1' => 'query_value', 'shared' => 'query_shared'],
683+
'request' => ['param2' => 'request_value', 'shared' => 'request_shared']
684+
]);
685+
686+
// Prioridad a query sobre request
687+
$this->assertEquals('query_value', $request->queryOrInput('param1'));
688+
$this->assertEquals('request_value', $request->queryOrInput('param2'));
689+
$this->assertEquals('query_shared', $request->queryOrInput('shared'));
690+
$this->assertNull($request->queryOrInput('nonexistent'));
691+
$this->assertEquals('default', $request->queryOrInput('nonexistent', 'default'));
692+
}
693+
664694
public function testConstants(): void
665695
{
666696
$this->assertEquals('GET', Request::METHOD_GET);

0 commit comments

Comments
 (0)