Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Method
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 validate
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework HTTP Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\HTTP;
11
12use InvalidArgumentException;
13
14/**
15 * Class Method.
16 *
17 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
18 *
19 * @package http
20 */
21class Method
22{
23    /**
24     * The HTTP CONNECT method starts two-way communications with the requested
25     * resource. It can be used to open a tunnel.
26     *
27     * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT
28     *
29     * @var string
30     */
31    public const CONNECT = 'CONNECT';
32    /**
33     * The HTTP DELETE request method deletes the specified resource.
34     *
35     * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE
36     *
37     * @var string
38     */
39    public const DELETE = 'DELETE';
40    /**
41     * The HTTP GET method requests a representation of the specified resource.
42     * Requests using GET should only be used to request data (they shouldn't
43     * include data).
44     *
45     * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
46     *
47     * @var string
48     */
49    public const GET = 'GET';
50    /**
51     * The HTTP HEAD method requests the headers that would be returned if the
52     * HEAD request's URL was instead requested with the HTTP GET method.
53     *
54     * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD
55     *
56     * @var string
57     */
58    public const HEAD = 'HEAD';
59    /**
60     * The HTTP OPTIONS method requests permitted communication options for a
61     * given URL or server. A client can specify a URL with this method, or an
62     * asterisk (*) to refer to the entire server.
63     *
64     * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
65     *
66     * @var string
67     */
68    public const OPTIONS = 'OPTIONS';
69    /**
70     * The HTTP PATCH request method applies partial modifications to a resource.
71     *
72     * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH
73     *
74     * @var string
75     */
76    public const PATCH = 'PATCH';
77    /**
78     * The HTTP POST method sends data to the server. The type of the body of
79     * the request is indicated by the Content-Type header.
80     *
81     * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
82     * @see Header::CONTENT_TYPE
83     *
84     * @var string
85     */
86    public const POST = 'POST';
87    /**
88     * The HTTP PUT request method creates a new resource or replaces a
89     * representation of the target resource with the request payload.
90     *
91     * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT
92     *
93     * @var string
94     */
95    public const PUT = 'PUT';
96    /**
97     * The HTTP TRACE method performs a message loop-back test along the path to
98     * the target resource, providing a useful debugging mechanism.
99     *
100     * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE
101     *
102     * @var string
103     */
104    public const TRACE = 'TRACE';
105    /**
106     * @var array<string>
107     */
108    protected static array $methods = [
109        'CONNECT',
110        'DELETE',
111        'GET',
112        'HEAD',
113        'OPTIONS',
114        'PATCH',
115        'POST',
116        'PUT',
117        'TRACE',
118    ];
119
120    /**
121     * @param string $method
122     *
123     * @throws InvalidArgumentException for invalid method
124     *
125     * @return string
126     */
127    public static function validate(string $method) : string
128    {
129        $valid = \strtoupper($method);
130        if (\in_array($valid, static::$methods, true)) {
131            return $valid;
132        }
133        throw new InvalidArgumentException('Invalid request method: ' . $method);
134    }
135}