Skip to content

Commit 8f3cf8b

Browse files
authored
Merge pull request #6995 from kenjis/feat-request-is
feat: add IncomingRequest::is() method
2 parents cfd0208 + a338610 commit 8f3cf8b

7 files changed

Lines changed: 134 additions & 0 deletions

File tree

system/HTTP/CLIRequest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,15 @@ public function getLocale(): string
304304
{
305305
return Locale::getDefault();
306306
}
307+
308+
/**
309+
* Checks this request type.
310+
*
311+
* @param string $type HTTP verb or 'json' or 'ajax'
312+
* @phpstan-param string|'get'|'post'|'put'|'delete'|'head'|'patch'|'options'|'json'|'ajax' $type
313+
*/
314+
public function is(string $type): bool
315+
{
316+
return false;
317+
}
307318
}

system/HTTP/IncomingRequest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,33 @@ public function negotiate(string $type, array $supported, bool $strictMatch = fa
375375
throw HTTPException::forInvalidNegotiationType($type);
376376
}
377377

378+
/**
379+
* Checks this request type.
380+
*
381+
* @param string $type HTTP verb or 'json' or 'ajax'
382+
* @phpstan-param string|'get'|'post'|'put'|'delete'|'head'|'patch'|'options'|'json'|'ajax' $type
383+
*/
384+
public function is(string $type): bool
385+
{
386+
$valueUpper = strtoupper($type);
387+
388+
$httpMethods = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS'];
389+
390+
if (in_array($valueUpper, $httpMethods, true)) {
391+
return strtoupper($this->getMethod()) === $valueUpper;
392+
}
393+
394+
if ($valueUpper === 'JSON') {
395+
return strpos($this->getHeaderLine('Content-Type'), 'application/json') !== false;
396+
}
397+
398+
if ($valueUpper === 'AJAX') {
399+
return $this->isAJAX();
400+
}
401+
402+
throw new InvalidArgumentException('Unknown type: ' . $type);
403+
}
404+
378405
/**
379406
* Determines if this request was made from the command line (CLI).
380407
*/

tests/system/HTTP/CLIRequestTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,4 +625,9 @@ public function testGetCookie()
625625

626626
$this->assertSame($this->request->getCookie(), []);
627627
}
628+
629+
public function testIs()
630+
{
631+
$this->assertFalse($this->request->is('get'));
632+
}
628633
}

tests/system/HTTP/IncomingRequestTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use CodeIgniter\HTTP\Files\UploadedFile;
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use Config\App;
19+
use Generator;
20+
use InvalidArgumentException;
1921
use TypeError;
2022

2123
/**
@@ -615,6 +617,63 @@ public function testCanGrabGetRawInputVar($rawstring, $var, $expected, $filter,
615617
$this->assertSame($expected, $request->getRawInputVar($var, $filter, $flag));
616618
}
617619

620+
/**
621+
* @dataProvider provideIsHTTPMethods
622+
*/
623+
public function testIsHTTPMethodLowerCase(string $value)
624+
{
625+
$request = $this->request->withMethod($value);
626+
627+
$this->assertTrue($request->is(strtolower($value)));
628+
}
629+
630+
public function provideIsHTTPMethods(): Generator
631+
{
632+
yield from [
633+
['GET'],
634+
['POST'],
635+
['PUT'],
636+
['DELETE'],
637+
['HEAD'],
638+
['PATCH'],
639+
['OPTIONS'],
640+
];
641+
}
642+
643+
/**
644+
* @dataProvider provideIsHTTPMethods
645+
*/
646+
public function testIsHTTPMethodUpperCase(string $value)
647+
{
648+
$request = $this->request->withMethod($value);
649+
650+
$this->assertTrue($request->is($value));
651+
}
652+
653+
public function testIsInvalidValue()
654+
{
655+
$this->expectException(InvalidArgumentException::class);
656+
$this->expectExceptionMessage('Unknown type: invalid');
657+
658+
$request = $this->request->withMethod('GET');
659+
660+
$request->is('invalid');
661+
}
662+
663+
public function testIsJson()
664+
{
665+
$request = $this->request->setHeader('Content-Type', 'application/json');
666+
667+
$this->assertTrue($request->is('json'));
668+
}
669+
670+
public function testIsWithAjax()
671+
{
672+
$request = $this->request->setHeader('X-Requested-With', 'XMLHttpRequest');
673+
674+
$this->assertTrue($request->is('ajax'));
675+
}
676+
618677
public function testIsCLI()
619678
{
620679
$this->assertFalse($this->request->isCLI());

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ Others
323323
- **Config:** Added ``Config\Session`` class to handle session configuration.
324324
- **Debug:** Kint has been updated to 5.0.1.
325325
- **Request:** Added new ``$request->getRawInputVar()`` method to return a specified variable from raw stream. See :ref:`Retrieving Raw data <incomingrequest-retrieving-raw-data>`.
326+
- **Request:** Added new ``$request->is()`` method to query the request type.
327+
See :ref:`Determining Request Type <incomingrequest-is>`.
326328

327329
Message Changes
328330
***************

user_guide_src/source/incoming/incomingrequest.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ be checked with the ``isAJAX()`` and ``isCLI()`` methods:
4040
which in some cases is not sent by default in XHR requests via JavaScript (i.e., fetch).
4141
See the :doc:`AJAX Requests </general/ajax>` section on how to avoid this problem.
4242

43+
.. _incomingrequest-is:
44+
45+
is()
46+
====
47+
48+
.. versionadded:: 4.3.0
49+
50+
Since v4.3.0, you can use the ``is()`` method. It returns boolean.
51+
52+
.. literalinclude:: incomingrequest/040.php
53+
54+
getMethod()
55+
===========
56+
4357
You can check the HTTP method that this request represents with the ``getMethod()`` method:
4458

4559
.. literalinclude:: incomingrequest/005.php
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
// Checks HTTP methods. Returns boolean.
4+
$request->is('get');
5+
$request->is('post');
6+
$request->is('put');
7+
$request->is('delete');
8+
$request->is('head');
9+
$request->is('patch');
10+
$request->is('options');
11+
12+
// Checks if it is an AJAX request. The same as `$request->isAJAX()`.
13+
$request->is('ajax');
14+
15+
// Checks if it is a JSON request.
16+
$request->is('json');

0 commit comments

Comments
 (0)