1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: User |
5
|
|
|
* Date: 10.10.2019. |
6
|
|
|
* Time: 18:15 |
7
|
|
|
*/ |
8
|
|
|
namespace Zmaglica\RickAndMortyApiWrapper\Model; |
9
|
|
|
|
10
|
|
|
use Zmaglica\RickAndMortyApiWrapper\RickAndMortyApiWrapper; |
11
|
|
|
|
12
|
|
|
abstract class AbstractModel |
13
|
|
|
{ |
14
|
|
|
protected $api; |
15
|
|
|
|
16
|
|
|
protected $response; |
17
|
|
|
|
18
|
|
|
protected $data; |
19
|
|
|
|
20
|
|
|
protected $parent; |
21
|
|
|
|
22
|
|
|
protected $info; |
23
|
|
|
|
24
|
|
|
public function __construct($response, $parent) |
25
|
|
|
{ |
26
|
|
|
$this->api = new RickAndMortyApiWrapper(); |
27
|
|
|
$this->parent = $parent; |
28
|
|
|
$this->response = $response; |
29
|
|
|
$this->data = json_decode($response->getBody(), true); |
30
|
|
|
$this->info = $this->data['info'] ?? null; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function toArray() |
34
|
|
|
{ |
35
|
|
|
return $this->data; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function toJson() |
39
|
|
|
{ |
40
|
|
|
return json_encode($this->data); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check if is first page of response |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function isFirstPage() |
48
|
|
|
{ |
49
|
|
|
if ($this->info && $this->info['prev']) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Check if is last page of request |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function isLastPage() |
62
|
|
|
{ |
63
|
|
|
if ($this->info && $this->info['next']) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get total number of records |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
|
public function count() |
75
|
|
|
{ |
76
|
|
|
if ($this->info) { |
77
|
|
|
return $this->info['count']; |
78
|
|
|
} |
79
|
|
|
/* |
80
|
|
|
* Check if there is a multiple results (case when we send multiple ids) |
81
|
|
|
*/ |
82
|
|
|
if (isset($this->data[0])) { |
83
|
|
|
return count($this->data); |
84
|
|
|
} |
85
|
|
|
/* |
86
|
|
|
* When we pass single ID we got results as single (not multidimensional array) |
87
|
|
|
*/ |
88
|
|
|
return 1; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** Get total number of pages |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
|
|
public function pages() |
95
|
|
|
{ |
96
|
|
|
return $this->info['pages'] ?? 1; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get previous page based on api response |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
public function prev() |
104
|
|
|
{ |
105
|
|
|
if ($this->isFirstPage()) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->parent->prevPage()->sendRequest(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get next page based on api response |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
public function next() |
117
|
|
|
{ |
118
|
|
|
if ($this->isLastPage()) { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->parent->nextPage()->sendRequest(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function firstPage() |
126
|
|
|
{ |
127
|
|
|
if ($this->info) { |
128
|
|
|
return $this->parent->setPage(1)->sendRequest(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function goToPage(int $page) |
135
|
|
|
{ |
136
|
|
|
if ($this->info) { |
137
|
|
|
return $this->parent->setPage($page)->sendRequest(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function lastPage() |
142
|
|
|
{ |
143
|
|
|
if ($this->info) { |
144
|
|
|
return $this->parent->setPage($this->info['pages'])->sendRequest(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return null; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|