Skip to content

Commit a377fac

Browse files
committed
Add RequestMethodInterface
1 parent 7672d34 commit a377fac

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
},
2121
"require": {
2222
"php": "^7.4",
23-
"fig/http-message-util": "^1.1",
2423
"httpsoft/http-stream": "^1.0",
2524
"httpsoft/http-uploaded-file": "^1.0",
2625
"httpsoft/http-uri": "^1.0"

src/Request.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace HttpSoft\Request;
66

7-
use Fig\Http\Message\RequestMethodInterface;
87
use HttpSoft\Uri\UriData;
98
use Psr\Http\Message\RequestInterface;
109
use Psr\Http\Message\StreamInterface;

src/RequestMethodInterface.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HttpSoft\Request;
6+
7+
interface RequestMethodInterface
8+
{
9+
/**
10+
* @link https://tools.ietf.org/html/rfc7231#section-4.3.1
11+
*/
12+
public const METHOD_GET = 'GET';
13+
14+
/**
15+
* @link https://tools.ietf.org/html/rfc7231#section-4.3.2
16+
*/
17+
public const METHOD_HEAD = 'HEAD';
18+
19+
/**
20+
* @link https://tools.ietf.org/html/rfc7231#section-4.3.3
21+
*/
22+
public const METHOD_POST = 'POST';
23+
24+
/**
25+
* @link https://tools.ietf.org/html/rfc7231#section-4.3.4
26+
*/
27+
public const METHOD_PUT = 'PUT';
28+
29+
/**
30+
* @link https://tools.ietf.org/html/rfc5789#section-2
31+
*/
32+
public const METHOD_PATCH = 'PATCH';
33+
34+
/**
35+
* @link https://tools.ietf.org/html/rfc7231#section-4.3.5
36+
*/
37+
public const METHOD_DELETE = 'DELETE';
38+
39+
/**
40+
* @link https://tools.ietf.org/html/rfc7231#section-4.3.6
41+
*/
42+
public const METHOD_CONNECT = 'CONNECT';
43+
44+
/**
45+
* @link https://tools.ietf.org/html/rfc7231#section-4.3.7
46+
*/
47+
public const METHOD_OPTIONS = 'OPTIONS';
48+
49+
/**
50+
* @link https://tools.ietf.org/html/rfc7231#section-4.3.8
51+
*/
52+
public const METHOD_TRACE = 'TRACE';
53+
54+
/**
55+
* Array/List of all defined methods.
56+
*/
57+
public const METHODS = [
58+
self::METHOD_GET,
59+
self::METHOD_HEAD,
60+
self::METHOD_POST,
61+
self::METHOD_PUT,
62+
self::METHOD_PATCH,
63+
self::METHOD_DELETE,
64+
self::METHOD_CONNECT,
65+
self::METHOD_OPTIONS,
66+
self::METHOD_TRACE,
67+
];
68+
}

src/RequestTrait.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace HttpSoft\Request;
66

7-
use Fig\Http\Message\RequestMethodInterface;
87
use HttpSoft\Stream\MessageTrait;
98
use HttpSoft\Stream\StreamFactory;
109
use HttpSoft\Uri\UriData;
@@ -16,10 +15,12 @@
1615

1716
use function gettype;
1817
use function get_class;
18+
use function in_array;
1919
use function is_object;
2020
use function is_string;
2121
use function preg_match;
2222
use function sprintf;
23+
use function strtoupper;
2324

2425
/**
2526
* Trait implementing the methods defined in `Psr\Http\Message\RequestInterface`.
@@ -249,7 +250,10 @@ private function init(
249250
*/
250251
private function setMethod(string $method): void
251252
{
252-
if (!preg_match('/^[!#$%&\'*+.^_`|~0-9a-z-]+$/i', $method)) {
253+
if (
254+
!in_array(strtoupper($method), RequestMethodInterface::METHODS)
255+
&& !preg_match('/^[!#$%&\'*+.^_`|~0-9a-z-]+$/i', $method)
256+
) {
253257
throw new InvalidArgumentException(sprintf('`%s` is not valid HTTP method.', $method));
254258
}
255259

src/ServerRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace HttpSoft\Request;
66

7-
use Fig\Http\Message\RequestMethodInterface;
87
use HttpSoft\Uri\UriData;
98
use InvalidArgumentException;
109
use Psr\Http\Message\ServerRequestInterface;

0 commit comments

Comments
 (0)