|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zmaglica\RickAndMortyApiWrapper; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Zmaglica\RickAndMortyApiWrapper\Api\Character; |
|
7
|
|
|
use Zmaglica\RickAndMortyApiWrapper\Api\Episode; |
|
8
|
|
|
use Zmaglica\RickAndMortyApiWrapper\Api\Location; |
|
9
|
|
|
|
|
10
|
|
|
class RickAndMortyApiWrapper |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The Guzzle instance used for HTTP requests |
|
14
|
|
|
* @var \GuzzleHttp\Client |
|
15
|
|
|
*/ |
|
16
|
|
|
private $client; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Guzzle HTTP request options |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $options = [ |
|
23
|
|
|
'base_uri' => 'https://rickandmortyapi.com/api/', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* RickAndMortyApiWrapper constructor. |
|
28
|
|
|
* @param array $options |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($options = []) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->options = array_merge($options, $this->options); |
|
33
|
|
|
$this->client = new Client($this->options); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return Client |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getClient(): Client |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->client; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Client $client |
|
46
|
|
|
* @return RickAndMortyApiWrapper |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setClient(Client $client): RickAndMortyApiWrapper |
|
49
|
|
|
{ |
|
50
|
|
|
$this->client = $client; |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getOptions(): array |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->options; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Set Guzzle options |
|
65
|
|
|
* @param array $options |
|
66
|
|
|
* @return RickAndMortyApiWrapper |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setOptions(array $options): RickAndMortyApiWrapper |
|
69
|
|
|
{ |
|
70
|
|
|
$this->options = $options; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Wrapper for Character API |
|
77
|
|
|
* @return Character |
|
78
|
|
|
*/ |
|
79
|
|
|
public function character() |
|
80
|
|
|
{ |
|
81
|
|
|
return new Character($this->client); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Wrapper for Location API |
|
86
|
|
|
* @return Location |
|
87
|
|
|
*/ |
|
88
|
|
|
public function location() |
|
89
|
|
|
{ |
|
90
|
|
|
return new Location($this->client); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Wrapper for Episode API |
|
95
|
|
|
* @return Episode |
|
96
|
|
|
*/ |
|
97
|
|
|
public function episode() |
|
98
|
|
|
{ |
|
99
|
|
|
return new Episode($this->client); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|