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
|
|
|
} |