Passed
Push — master ( 57ebbd...f70266 )
by Shahrad
01:49
created

Request::getProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace EasyHttp;
4
5
/**
6
 * Class Request
7
 *
8
 * This class is used to make HTTP requests interface.
9
 *
10
 * @link    https://github.com/shahradelahi/easy-http
11
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
12
 * @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License)
13
 *
14
 * @method static Request request(string $method, string $url, array $options = [])
15
 * @method static Request get(string $url, array $options = [])
16
 * @method static Request post(string $url, array $options = [])
17
 * @method static Request put(string $url, array $options = [])
18
 * @method static Request patch(string $url, array $options = [])
19
 * @method static Request delete(string $url, array $options = [])
20
 */
21
class Request
22
{
23
24
    /**
25
     * @var string
26
     */
27
    private string $method;
28
29
    /**
30
     * @var string
31
     */
32
    private string $url;
33
34
    /**
35
     * @var array
36
     */
37
    private array $options;
38
39
    /**
40
     * Get array of properties
41
     *
42
     * @return array
43
     */
44
    public function getProperties(): array
45
    {
46
        return [
47
            'method' => $this->method,
48
            'url' => $this->url,
49
            'options' => $this->options
50
        ];
51
    }
52
53
    /**
54
     *
55
     * @param string $method
56
     * @param string $url
57
     * @param array $options
58
     */
59
    public function __construct(string $method, string $url, array $options = [])
60
    {
61
        $this->method = $method;
62
        $this->url = $url;
63
        $this->options = $options;
64
    }
65
66
    /**
67
     * If the method is not defined, it will return the Request class.
68
     * otherwise it will return the response of the request.
69
     *
70
     * @param string $name The method name
71
     * @param array $arguments The method arguments
72
     *
73
     * @return mixed
74
     */
75
    public function __call(string $name, array $arguments): mixed
76
    {
77
        if (in_array($name, ['request', 'get', 'post', 'put', 'patch', 'delete'])) {
78
            return (new Client())->{$name}(...$arguments);
79
        }
80
        return $this->{$name}(...$arguments);
81
    }
82
83
}