-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTCGdex.php
More file actions
550 lines (485 loc) · 14.4 KB
/
Copy pathTCGdex.php
File metadata and controls
550 lines (485 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
namespace TCGdex;
use Buzz\Client\Curl;
use Exception;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Psr16Cache;
use TCGdex\Model\Card;
use TCGdex\Model\CardResume;
use TCGdex\Model\Serie;
use TCGdex\Model\SerieResume;
use TCGdex\Model\StringEndpoint;
use TCGdex\Request;
use Composer\InstalledVersions;
use TCGdex\Endpoints\Endpoint;
use TCGdex\Endpoints\SetEndpoint;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.TooManyMethods)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
*/
class TCGdex
{
/**
* @deprecated use `TCGdex::getVersion()` instead
*/
public const VERSION = "2.x.x";
/**
* @return string
*/
public static function getVersion()
{
try {
return InstalledVersions::getVersion("tcgdex/sdk");
} catch (Exception $e) {
return "2.x.x";
}
}
public const BASE_URI = "https://api.tcgdex.net/v2";
/**
* @var \Psr\SimpleCache\CacheInterface|null
*/
public static $cache;
/**
* @var \Psr\Http\Message\ResponseFactoryInterface|null
*/
public static $responseFactory;
/**
* @var \Psr\Http\Message\RequestFactoryInterface|null
*/
public static $requestFactory;
/**
* @var \Psr\Http\Client\ClientInterface|null
*/
public static $client;
/**
* @var int
*/
public static $ttl = 60 * 1000 * 1000;
/**
* @var string
* Possible values: en, fr
*/
public $lang = "en";
public function __construct(?string $lang = null)
{
if (is_null(TCGdex::$cache)) {
// no PSR16 implementation found, try loading the base one
TCGdex::$cache = new Psr16Cache(new ArrayAdapter());
}
if (is_null(TCGdex::$responseFactory)) {
// no PSR17 implementation found, try loading the base one
TCGdex::$responseFactory = new Psr17Factory();
}
if (is_null(TCGdex::$requestFactory)) {
// no PSR17 implementation found, try loading the base one
TCGdex::$requestFactory = new Psr17Factory();
}
if (is_null(TCGdex::$client) && !is_null(TCGdex::$responseFactory)) {
// no PSR18 implementation found, try loading the base one
TCGdex::$client = new Curl(TCGdex::$responseFactory);
}
if (!is_null($lang)) {
$this->lang = $lang;
}
// Setup the endpoints
$this->card = new Endpoint($this, Card::class, CardResume::class, 'cards');
$this->variant = new Endpoint($this, StringEndpoint::class, null, 'variants');
$this->trainerType = new Endpoint($this, StringEndpoint::class, null, 'trainer-types');
$this->suffix = new Endpoint($this, StringEndpoint::class, null, 'suffixes');
$this->stage = new Endpoint($this, StringEndpoint::class, null, 'stages');
$this->regulationMark = new Endpoint($this, StringEndpoint::class, null, 'regulation-marks');
$this->energyType = new Endpoint($this, StringEndpoint::class, null, 'energy-types');
$this->dexId = new Endpoint($this, StringEndpoint::class, null, 'dex-ids');
$this->type = new Endpoint($this, StringEndpoint::class, null, 'types');
$this->set = new SetEndpoint($this);
$this->serie = new Endpoint($this, Serie::class, SerieResume::class, 'series');
$this->retreat = new Endpoint($this, StringEndpoint::class, null, 'retreats');
$this->rarity = new Endpoint($this, StringEndpoint::class, null, 'rarities');
$this->illustrator = new Endpoint($this, StringEndpoint::class, null, 'illustrators');
$this->hp = new Endpoint($this, StringEndpoint::class, null, 'hp');
$this->category = new Endpoint($this, StringEndpoint::class, null, 'categories');
}
/**
* The card endpoint of the TCGdex API
* @var Endpoint<Card, CardResume> $card
*/
public readonly Endpoint $card;
/**
* The variant endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $variant
*/
public readonly Endpoint $variant;
/**
* The trainerType endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $trainerType
*/
public readonly Endpoint $trainerType;
/**
* The suffix endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $suffix
*/
public readonly Endpoint $suffix;
/**
* The stage endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $stage
*/
public readonly Endpoint $stage;
/**
* The regulationMark endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $regulationMark
*/
public readonly Endpoint $regulationMark;
/**
* The energyType endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $energyType
*/
public readonly Endpoint $energyType;
/**
* The dexId endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $dexId
*/
public readonly Endpoint $dexId;
/**
* The type endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $type
*/
public readonly Endpoint $type;
/**
* The set endpoint of the TCGdex API
*/
public readonly SetEndpoint $set;
/**
* The serie endpoint of the TCGdex API
* @var Endpoint<Serie, SerieResume> $serie
*/
public readonly Endpoint $serie;
/**
* The retreat endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $retreat
*/
public readonly Endpoint $retreat;
/**
* The rarity endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $rarity
*/
public readonly Endpoint $rarity;
/**
* The illustrator endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $illustrator
*/
public readonly Endpoint $illustrator;
/**
* The hp endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $hp
*/
public readonly Endpoint $hp;
/**
* The category endpoint of the TCGdex API
* @var Endpoint<StringEndpoint, string> $category
*/
public readonly Endpoint $category;
/**
* @param string|null $endpoint
* @return mixed|null
*/
public function fetch(...$endpoint)
{
return Request::fetch(TCGdex::BASE_URI, $this->lang, ...$endpoint);
}
/**
* same as `$tcgdex->fetch` but it allows to add params to the URL
* @param Array<string> $endpoint the endpoint paths as an array
* @param Array<string> $params the list of parameters to add
* @return mixed|null
*/
public function fetchWithParams(array $endpoint, ?array $params = null)
{
return Request::fetchWithParams(
Request::makePath(TCGdex::BASE_URI, $this->lang, ...$endpoint),
$params
);
}
/**
* Fetch a card by its ID or local id if the set is named
* @param String $id
* @param String|null $set
* @return Card|null
* @deprecated 2.2.0 use `$this->set->getCard($set, $id);` or `$this->card->get($id);` instead.
*/
public function fetchCard(string $id, ?string $set = null)
{
if (!is_null($set)) {
return $this->set->getCard($set, $id);
}
return $this->card->get($id);
}
/**
* @return Array<CardResume>
* @deprecated 2.2.0 use `$tcgdex->card->list();` instead.
*/
public function fetchCards()
{
$res = $this->card->list();
return $res;
}
/**
* @param string $category
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->category->get($category);` instead.
*/
public function fetchCategory(string $category)
{
return $this->category->get($category);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->category->list();` instead.
*/
public function fetchCategories()
{
return $this->category->list();
}
/**
* @param string $hp
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->hp->get($hp);` instead.
*/
public function fetchHp(string $hp)
{
return $this->hp->get($hp);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->hp->list();` instead.
*/
public function fetchHps()
{
return $this->hp->list();
}
/**
* @param string $illustrator
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->illustrator->get($illustrator);` instead.
*/
public function fetchIllustrator(string $illustrator)
{
return $this->illustrator->get($illustrator);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->illustrator->list();` instead.
*/
public function fetchIllustrators()
{
return $this->illustrator->list();
}
/**
* @param string $rarity
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->rarity->get($rarity);` instead.
*/
public function fetchRarity(string $rarity)
{
return $this->rarity->get($rarity);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->rarity->list();` instead.
*/
public function fetchRarities()
{
return $this->rarity->list();
}
/**
* @param string $retreat
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->retreat->get($retreat);` instead.
*/
public function fetchRetreat(string $retreat)
{
return $this->retreat->get($retreat);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->retreat->list();` instead.
*/
public function fetchRetreats()
{
return $this->retreat->list();
}
/**
* @param string $serie
* @return Serie|null
* @deprecated 2.2.0 use `$tcgdex->serie->get($serie);` instead.
*/
public function fetchSerie(string $serie)
{
return $this->serie->get($serie);
}
/**
* @return SerieResume[]|null
* @deprecated 2.2.0 use `$tcgdex->serie->list();` instead.
*/
public function fetchSeries()
{
return $this->serie->list();
}
/**
* @param string $set
* @return Set|null
* @deprecated 2.2.0 use `$tcgdext->get($set);` instead.
*/
public function fetchSet(string $set)
{
return $this->set->get($set);
}
/**
* @return SetResume[]
* @deprecated 2.2.0 use `$tcgdex->set->list();` instead.
*/
public function fetchSets()
{
return $this->set->list();
}
/**
* @param string $type
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->type->get($type);` instead.
*/
public function fetchType(string $type)
{
return $this->type->get($type);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->type->list();` instead.
*/
public function fetchTypes()
{
return $this->type->list();
}
/**
* @param string $dexId
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->dexId->get($dexId);` instead.
*/
public function fetchDexId(string $dexId)
{
return $this->dexId->get($dexId);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->dexId->list();` instead.
*/
public function fetchDexIds()
{
return $this->dexId->list();
}
/**
* @param string $energyType
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->energyType->get($energyType);` instead.
*/
public function fetchEnergyType(string $energyType)
{
return $this->energyType->get($energyType);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->energyType->list();` instead.
*/
public function fetchEnergyTypes()
{
return $this->energyType->list();
}
/**
* @param string $regulationMark
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->regulationMark->get($regulationMark);` instead.
*/
public function fetchRegulationMark(string $regulationMark)
{
return $this->regulationMark->get($regulationMark);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->regulationMark->list();` instead.
*/
public function fetchRegulationMarks()
{
return $this->regulationMark->list();
}
/**
* @param string $stage
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->stage->get($stage);` instead.
*/
public function fetchStage(string $stage)
{
return $this->stage->get($stage);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->stage->list();` instead.
*/
public function fetchStages()
{
return $this->stage->list();
}
/**
* @param string $suffix
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->suffix->get($suffix);` instead.
*/
public function fetchSuffix(string $suffix)
{
return $this->suffix->get($suffix);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->suffix->list();` instead.
*/
public function fetchSuffixes()
{
return $this->suffix->list();
}
/**
* @param string $trainerType
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->trainerType->get($trainerType);` instead.
*/
public function fetchTrainerType(string $trainerType)
{
return $this->trainerType->get($trainerType);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->trainerType->list();` instead.
*/
public function fetchTrainerTypes()
{
return $this->trainerType->list();
}
/**
* @param string $variant
* @return StringEndpoint|null
* @deprecated 2.2.0 use `$tcgdex->variant->get($variant);` instead.
*/
public function fetchVariant(string $variant)
{
return $this->variant->get($variant);
}
/**
* @return string[]
* @deprecated 2.2.0 use `$tcgdex->variant->list();` instead.
*/
public function fetchVariants()
{
return $this->variant->list();
}
}