|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lyal\Checkr\Traits; |
|
4
|
|
|
|
|
5
|
|
|
trait Listable |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Abstract functions to imppose requirements for the exhibiting class. |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract public function getResourceName($object = null); |
|
11
|
|
|
|
|
12
|
|
|
abstract public function getAttributes($sanitized = true); |
|
13
|
|
|
|
|
14
|
|
|
abstract public function processPath($path = null, array $values = null); |
|
15
|
|
|
|
|
16
|
|
|
abstract public function postRequest($path, array $options = []); |
|
17
|
|
|
|
|
18
|
|
|
abstract public function getClient(); |
|
19
|
|
|
|
|
20
|
|
|
abstract public function setValues($values); |
|
21
|
|
|
|
|
22
|
|
|
abstract public function getAttribute($key); |
|
23
|
|
|
|
|
24
|
|
|
abstract public function setAttribute($key, $value); |
|
25
|
|
|
|
|
26
|
|
|
abstract protected function getRequest($path = null, $parameters = null); |
|
27
|
|
|
|
|
28
|
|
|
abstract protected function __construct($values, $client); |
|
29
|
|
|
|
|
30
|
|
|
protected $listPath; |
|
31
|
|
|
|
|
32
|
|
|
private $perPage = 25; |
|
33
|
|
|
private $currentPage = 1; |
|
34
|
|
|
private $count; |
|
35
|
|
|
private $list = []; |
|
36
|
|
|
|
|
37
|
|
|
private $hasNextPage; |
|
38
|
|
|
|
|
39
|
|
|
public function setListPath($path) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->listPath = $path; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getListPath() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->listPath; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Number of results to return per request. |
|
51
|
|
|
* |
|
52
|
|
|
* @return int |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getPerPage() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->perPage; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param int $perPage |
|
61
|
|
|
* |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setPerPage(int $perPage) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->perPage = $perPage; |
|
67
|
|
|
$this->setAttribute('per_page', $perPage); |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param int $currentPage |
|
74
|
|
|
* |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setCurrentPage(int $currentPage) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->currentPage = $currentPage; |
|
80
|
|
|
$this->setAttribute('page', $currentPage); |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return int |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getCurrentPage() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->currentPage; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function nextPage() |
|
94
|
|
|
{ |
|
95
|
|
|
return ++$this->currentPage; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getCount() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->count; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function setCount($count) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->count = $count; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function setPagination($page = null, $perPage = null) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->setCurrentPage($page ?? $this->getCurrentPage()); |
|
111
|
|
|
$this->setPerPage($perPage ?? $this->getPerPage()); |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function hasNextPage() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->hasNextPage !== false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function setNextPage() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->hasNextPage = (($this->getPerPage() * $this->getCurrentPage()) < $this->getCount()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getList($page = null, $perPage = null) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->setPagination($page, $perPage); |
|
129
|
|
|
|
|
130
|
|
|
$path = $this->getListPath() ?? null; |
|
131
|
|
|
|
|
132
|
|
|
return $this->processList($this->getRequest($this->processPath($path))); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param \stdClass $items |
|
137
|
|
|
* |
|
138
|
|
|
* @return \Illuminate\Support\Collection|array |
|
139
|
|
|
*/ |
|
140
|
|
|
public function processList($items) |
|
141
|
|
|
{ |
|
142
|
|
|
$list = []; |
|
143
|
|
|
|
|
144
|
|
|
$this->setCount($items->count); |
|
145
|
|
|
$this->setNextPage(); |
|
146
|
|
|
|
|
147
|
|
|
foreach ((array) $items->data as $item) { |
|
148
|
|
|
$list[] = new $this($item, $this->getClient()); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $this->getClient()->useCollections ? collect($list) : $list; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function all() |
|
155
|
|
|
{ |
|
156
|
|
|
$list = collect([]); |
|
157
|
|
|
|
|
158
|
|
|
while ($this->hasNextPage()) { |
|
159
|
|
|
$list = $list->merge($this->getList()); |
|
160
|
|
|
$this->nextPage(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $this->getClient()->useCollections ? $list : $list->toArray(); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|