1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of laravel.su package. |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace App\GraphQL\Kernel\Paginator; |
10
|
|
|
|
11
|
|
|
use GraphQL\Type\Definition\Type; |
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
use Illuminate\Database\Eloquent\Builder; |
14
|
|
|
use App\GraphQL\Serializers\RawSerializer; |
15
|
|
|
use App\GraphQL\Serializers\AbstractSerializer; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class PaginatorConfiguration |
19
|
|
|
* @package App\GraphQL\Kernel\Paginator |
20
|
|
|
*/ |
21
|
|
|
class PaginatorConfiguration implements \IteratorAggregate |
22
|
|
|
{ |
23
|
|
|
public const QUERY_LIMIT = '_limit'; |
24
|
|
|
public const QUERY_PAGE = '_page'; |
25
|
|
|
|
26
|
|
|
private const LIMIT_DEFAULT = 10; |
27
|
|
|
private const LIMIT_MIN = 1; |
28
|
|
|
private const LIMIT_MAX = 100; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array|PaginatorConfiguration[] |
32
|
|
|
*/ |
33
|
|
|
private static $configs = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var null|AbstractSerializer |
37
|
|
|
*/ |
38
|
|
|
protected $serializer = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var null|Builder |
42
|
|
|
*/ |
43
|
|
|
protected $query; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $limit = self::LIMIT_DEFAULT; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $page = 1; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var null|string |
57
|
|
|
*/ |
58
|
|
|
private $alias; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var int |
62
|
|
|
*/ |
63
|
|
|
private $perPage = self::LIMIT_DEFAULT; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var int |
67
|
|
|
*/ |
68
|
|
|
private $count; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* PaginatorConfiguration constructor. |
72
|
|
|
* @param Builder $builder |
73
|
|
|
*/ |
74
|
|
|
public function __construct(Builder $builder, int $count) |
75
|
|
|
{ |
76
|
|
|
self::$configs[] = $this; |
77
|
|
|
$this->query = $builder; |
78
|
|
|
$this->count = $count; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $alias |
83
|
|
|
* @return PaginatorConfiguration |
84
|
|
|
*/ |
85
|
|
|
public static function find(string $alias): ?PaginatorConfiguration |
86
|
|
|
{ |
87
|
|
|
foreach (self::$configs as $config) { |
88
|
|
|
if ($config->getAlias() === $alias) { |
89
|
|
|
return $config; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
|
|
public function getCount(): int |
100
|
|
|
{ |
101
|
|
|
return $this->count; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return int |
106
|
|
|
*/ |
107
|
|
|
public function getPerPage(): int |
108
|
|
|
{ |
109
|
|
|
return $this->perPage; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
public function getPages(): int |
116
|
|
|
{ |
117
|
|
|
return (int)ceil($this->count / $this->getLimit()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public static function getConfigs(): array |
124
|
|
|
{ |
125
|
|
|
return self::$configs; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return AbstractSerializer|null |
130
|
|
|
*/ |
131
|
|
|
public function getSerializer() |
132
|
|
|
{ |
133
|
|
|
return $this->serializer; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return Builder |
138
|
|
|
*/ |
139
|
|
|
public function getQuery(): Builder |
140
|
|
|
{ |
141
|
|
|
return $this->query; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return int|null |
146
|
|
|
*/ |
147
|
|
|
public function getLimit() |
148
|
|
|
{ |
149
|
|
|
return $this->limit; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return int |
154
|
|
|
*/ |
155
|
|
|
public function getPage(): int |
156
|
|
|
{ |
157
|
|
|
return $this->page; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return int |
162
|
|
|
*/ |
163
|
|
|
public function getSkip(): int |
164
|
|
|
{ |
165
|
|
|
return (int)($this->page - 1) * $this->limit; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return null|string |
170
|
|
|
*/ |
171
|
|
|
public function getAlias() |
172
|
|
|
{ |
173
|
|
|
return $this->alias; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param array $original |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
public static function withPaginatorArguments(array $original): array |
181
|
|
|
{ |
182
|
|
|
return array_merge($original, [ |
183
|
|
|
static::QUERY_LIMIT => [ |
184
|
|
|
'type' => Type::int(), |
185
|
|
|
'description' => 'Items per page: in 1...1000 range', |
186
|
|
|
], |
187
|
|
|
static::QUERY_PAGE => [ |
188
|
|
|
'type' => Type::int(), |
189
|
|
|
'description' => 'Current page number (Usage without "_limit" argument gives no effect)', |
190
|
|
|
], |
191
|
|
|
]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $args |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
|
|
public function withArgs(array $args) |
199
|
|
|
{ |
200
|
|
|
$this->limit = $args[static::QUERY_LIMIT] ?? self::LIMIT_DEFAULT; |
201
|
|
|
$this->limit = min(self::LIMIT_MAX, $this->limit); |
202
|
|
|
$this->limit = max(self::LIMIT_MIN, $this->limit); |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
$this->page = max(1, $args[static::QUERY_PAGE] ?? 1); |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param $data |
212
|
|
|
* @return Collection |
213
|
|
|
*/ |
214
|
|
|
private function serializeCollection($data) |
215
|
|
|
{ |
216
|
|
|
if ($this->serializer === null) { |
217
|
|
|
return RawSerializer::collection($data); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $this->serializer::collection($data); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param null|string $serializer |
225
|
|
|
* @return $this|PaginatorConfiguration |
226
|
|
|
*/ |
227
|
|
|
public function use(?string $serializer): PaginatorConfiguration |
228
|
|
|
{ |
229
|
|
|
$this->serializer = $serializer; |
|
|
|
|
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param string $graphQlQuery |
236
|
|
|
* @return PaginatorConfiguration |
237
|
|
|
*/ |
238
|
|
|
public function as(string $graphQlQuery): PaginatorConfiguration |
239
|
|
|
{ |
240
|
|
|
$this->alias = $graphQlQuery; |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return Collection |
247
|
|
|
*/ |
248
|
|
|
public function getIterator() |
249
|
|
|
{ |
250
|
|
|
$query = clone $this->query; |
251
|
|
|
|
252
|
|
|
if ($this->limit !== null) { |
253
|
|
|
$query = $query->take((int)$this->limit); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if ($this->page > 0 && $this->limit !== null) { |
257
|
|
|
$query = $query->skip($this->getSkip()); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$result = $query->get(); |
261
|
|
|
|
262
|
|
|
$this->perPage = $result->count(); |
263
|
|
|
|
264
|
|
|
return $this->serializeCollection($result); |
265
|
|
|
} |
266
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.