1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zmaglica\RickAndMortyApiWrapper\Model; |
4
|
|
|
|
5
|
|
|
class Character extends AbstractModel |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Get all locations without running API call. Useful if you want to perform additional filtering. |
9
|
|
|
* |
10
|
|
|
* @param bool $removeDuplicates |
11
|
|
|
* @return array|\Zmaglica\RickAndMortyApiWrapper\Api\AbstractApi |
12
|
|
|
*/ |
13
|
|
|
public function locations($removeDuplicates = false) |
14
|
|
|
{ |
15
|
|
|
$ids = $this->extractIdsFromResponseBasedOnKey('location', $removeDuplicates); |
16
|
|
|
|
17
|
|
|
return $this->api->location()->whereId($ids); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get all locations. |
22
|
|
|
* @param bool $removeDuplicates |
23
|
|
|
* @return mixed |
24
|
|
|
*/ |
25
|
|
|
public function getLocations($removeDuplicates = false) |
26
|
|
|
{ |
27
|
|
|
return $this->locations($removeDuplicates)->get(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get all origin locations. Useful if you want to perform additional filtering. |
32
|
|
|
* |
33
|
|
|
* @param bool $removeDuplicates |
34
|
|
|
* @return array|\Zmaglica\RickAndMortyApiWrapper\Api\AbstractApi |
35
|
|
|
*/ |
36
|
|
|
public function origins($removeDuplicates = false) |
37
|
|
|
{ |
38
|
|
|
$ids = $this->extractIdsFromResponseBasedOnKey('origin', $removeDuplicates); |
39
|
|
|
|
40
|
|
|
return $this->api->location()->whereId($ids); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get all origin locations. |
45
|
|
|
* |
46
|
|
|
* @param bool $removeDuplicates |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function getOrigins($removeDuplicates = false) |
50
|
|
|
{ |
51
|
|
|
return $this->origins($removeDuplicates)->get(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all episodes. Useful if you want to perform additional filtering. |
56
|
|
|
* |
57
|
|
|
* @param bool $removeDuplicates |
58
|
|
|
* @return \Zmaglica\RickAndMortyApiWrapper\Api\AbstractApi |
59
|
|
|
*/ |
60
|
|
|
public function episodes($removeDuplicates = false) |
61
|
|
|
{ |
62
|
|
|
$ids = $this->extractIdsFromResponseBasedOnKey('episode', $removeDuplicates); |
63
|
|
|
|
64
|
|
|
return $this->api->episode()->whereId($ids); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get all episodes |
69
|
|
|
* |
70
|
|
|
* @param bool $removeDuplicates |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
|
|
public function getEpisodes($removeDuplicates = false) |
74
|
|
|
{ |
75
|
|
|
return $this->episodes($removeDuplicates)->get(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* This helper function will extract all URL ids by key. Key can be location, residence or episode |
80
|
|
|
* @param $key |
81
|
|
|
* @param $removeDuplicates |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
private function extractIdsFromResponseBasedOnKey($key, $removeDuplicates) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$ids = []; |
87
|
|
|
$urls = []; |
88
|
|
|
if ($this->info) { |
89
|
|
|
$urls = array_column($this->data['results'], 'location'); |
90
|
|
|
} elseif (isset($this->data[0])) { |
91
|
|
|
$urls = array_column($this->data, 'location'); |
92
|
|
|
} else { |
93
|
|
|
$urls[] = $this->data['location']; |
94
|
|
|
} |
95
|
|
|
$urls = array_column($urls, 'url'); |
96
|
|
|
if ($removeDuplicates) { |
97
|
|
|
$urls = array_unique($urls); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
foreach ($urls as $url) { |
101
|
|
|
$ids[] = substr(strrchr($url, '/'), 1); |
102
|
|
|
; |
103
|
|
|
} |
104
|
|
|
if (!$ids) { |
|
|
|
|
105
|
|
|
return []; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $ids; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.