Passed
Push — develop ( 5c3296...f8ad08 )
by axel
02:33
created

MalScraper::getMangaRecommendation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * rl404 - MalScraper.
4
 *
5
 * Unofficial PHP API which scraps and parses page source of MyAnimeList.
6
 * API Documentation: https://github.com/rl404/MAL-Scraper
7
 *
8
 * @author Axel Oktavian Antonio
9
 *
10
 * @since 26-09-2018
11
 *
12
 * @version 1.5.1
13
 *
14
 * @license MIT https://opensource.org/licenses/MIT
15
 */
16
17
namespace MalScraper;
18
19
use Cache;
20
use MalScraper\Helper\Helper;
21
use MalScraper\Model\Additional\AnimeMangaRecommendationModel as AnimeMangaRecommendation;
22
use MalScraper\Model\Additional\AnimeMangaReviewModel as AnimeMangaReview;
23
use MalScraper\Model\Additional\CharacterPeoplePictureModel as CharacterPeoplePicture;
24
use MalScraper\Model\Additional\CharacterStaffModel as CharacterStaff;
25
use MalScraper\Model\Additional\EpisodeModel as Episode;
26
use MalScraper\Model\Additional\PictureModel as Picture;
27
use MalScraper\Model\Additional\StatModel as Stat;
28
use MalScraper\Model\Additional\VideoModel as Video;
29
use MalScraper\Model\General\CharacterModel as Character;
30
use MalScraper\Model\General\InfoModel as Info;
31
use MalScraper\Model\General\PeopleModel as People;
32
use MalScraper\Model\General\ProducerModel as Producer;
33
use MalScraper\Model\General\RecommendationModel as Recommendation;
34
use MalScraper\Model\General\ReviewModel as Review;
35
use MalScraper\Model\Lists\AllGenreModel as AllGenre;
36
use MalScraper\Model\Lists\AllProducerModel as AllProducer;
37
use MalScraper\Model\Lists\AllRecommendationModel as AllRecommendation;
38
use MalScraper\Model\Lists\AllReviewModel as AllReview;
39
use MalScraper\Model\Search\SearchAnimeMangaModel as SearchAnimeManga;
40
use MalScraper\Model\Search\SearchCharacterPeopleModel as SearchCharacterPeople;
41
use MalScraper\Model\Search\SearchUserModel as SearchUser;
42
use MalScraper\Model\Seasonal\SeasonModel as Season;
43
use MalScraper\Model\Top\TopCharacterModel as TopCharacter;
44
use MalScraper\Model\Top\TopModel as Top;
45
use MalScraper\Model\Top\TopPeopleModel as TopPeople;
46
use MalScraper\Model\User\FriendModel as Friend;
47
use MalScraper\Model\User\HistoryModel as History;
48
use MalScraper\Model\User\UserCoverModel as UserCover;
49
use MalScraper\Model\User\UserListModel as UserList;
50
use MalScraper\Model\User\UserModel as User;
51
52
/**
53
 * Class MalScraper.
54
 */
55
class MalScraper
56
{
57
    /**
58
     * Cache class.
59
     *
60
     * @var Cache
61
     */
62
    private $_cache;
63
64
    /**
65
     * Cache feature.
66
     *
67
     * @var bool
68
     */
69
    private $_enable_cache = false;
70
71
    /**
72
     * Cache expiration time.
73
     *
74
     * @var int
75
     */
76
    private $_cache_time = 86400;
77
78
    /**
79
     * Convert to http response.
80
     *
81
     * @var bool
82
     */
83
    private $_to_api = false;
84
85
    /**
86
     * Default constructor.
87
     *
88
     * @param array [optional] $config
89
     *
90
     * @return void
91
     */
92
    public function __construct($config = false)
93
    {
94
        if (!empty($config['enable_cache']) && $config['enable_cache'] === true) {
95
            // enable cache function
96
            $this->_enable_cache = $config['enable_cache'];
97
98
            // create cache class
99
            $this->_cache = new Cache();
100
            $this->_cache->setCachePath(dirname(__FILE__).'/Cache/');
101
102
            // set cache time
103
            if (!empty($config['cache_time'])) {
104
                $this->_cache_time = $config['cache_time'];
105
            }
106
        }
107
108
        // to http response function
109
        if (!empty($config['to_api']) && $config['to_api'] === true) {
110
            $this->_to_api = $config['to_api'];
111
        }
112
    }
113
114
    /**
115
     * Default call.
116
     *
117
     * @param string $method
118
     * @param array  $arguments
119
     *
120
     * @return string|array
121
     */
122
    public function __call($method, $arguments)
123
    {
124
        $result = '';
125
126
        // if cache function enabled
127
        if ($this->_enable_cache === true) {
128
            $this->_cache->setCache(str_replace('get', '', $method));
129
            $this->_cache->eraseExpired($this->_cache_time);
130
131
            $cacheName = $method.'('.implode(',', $arguments).')';
132
            $isCached = $this->_cache->isCached($cacheName);
133
134
            // if cached
135
            if ($isCached) {
136
                $result = $this->_cache->retrieve($cacheName);
137
            } else {
138
                $data = call_user_func_array([$this, $method], $arguments);
139
                $this->_cache->store($cacheName, $data, $this->_cache_time);
140
                $result = $data;
141
            }
142
        } else {
143
            $result = call_user_func_array([$this, $method], $arguments);
144
        }
145
146
        // if to api function enabled
147
        if ($this->_to_api === true) {
148
            return Helper::response($result);
149
        }
150
151
        return Helper::toResponse($result);
152
    }
153
154
    /**
155
     * Get anime/manga information.
156
     *
157
     * @param string     $type anime or manga
158
     * @param int|string $id   id of the anime or manga
159
     *
160
     * @return array
161
     */
162
    private function getInfo($type, $id)
163
    {
164
        return (new Info($type, $id))->getAllInfo();
165
    }
166
167
    /**
168
     * Get character information.
169
     *
170
     * @param int|string $id id of the character
171
     *
172
     * @return array
173
     */
174
    private function getCharacter($id)
175
    {
176
        return (new Character($id))->getAllInfo();
177
    }
178
179
    /**
180
     * Get people information.
181
     *
182
     * @param int|string $id id of the people
183
     *
184
     * @return array
185
     */
186
    private function getPeople($id)
187
    {
188
        return (new People($id))->getAllInfo();
189
    }
190
191
    /**
192
     * Get review information.
193
     *
194
     * @param int|string $id id of the review
195
     *
196
     * @return array
197
     */
198
    private function getReview($id)
199
    {
200
        return (new Review($id))->getAllInfo();
201
    }
202
203
    /**
204
     * Get recommendation information.
205
     *
206
     * @param string     $type Either anime or manga
207
     * @param int|string $id   id of the first anime/manga
208
     * @param int|string $id   id of the second anime/manga
209
     *
210
     * @return array
211
     */
212
    private function getRecommendation($type, $id1, $id2)
213
    {
214
        return (new Recommendation($type, $id1, $id2))->getAllInfo();
215
    }
216
217
    /**
218
     * Get anime/manga character + staff complete list.
219
     *
220
     * @param string     $type Either anime or manga
221
     * @param int|string $id   id of the anime or manga
222
     *
223
     * @return array
224
     */
225
    private function getCharacterStaff($type, $id)
226
    {
227
        return (new CharacterStaff($type, $id))->getAllInfo();
228
    }
229
230
    /**
231
     * Get anime/manga detail stat.
232
     *
233
     * @param string     $type Either anime or manga
234
     * @param int|string $id   id of the anime or manga
235
     *
236
     * @return array
237
     */
238
    private function getStat($type, $id)
239
    {
240
        return (new Stat($type, $id))->getAllInfo();
241
    }
242
243
    /**
244
     * Get anime video.
245
     *
246
     * @param int|string $id   id of the anime
247
     * @param int|string $page (Optional) Page number
248
     *
249
     * @return array
250
     */
251
    private function getVideo($id, $page = 1)
252
    {
253
        return (new Video($id, $page))->getAllInfo();
254
    }
255
256
    /**
257
     * Get anime episode.
258
     *
259
     * @param int|string $id   id of the anime
260
     * @param int|string $page (Optional) Page number
261
     *
262
     * @return array
263
     */
264
    private function getEpisode($id, $page = 1)
265
    {
266
        return (new Episode($id, $page))->getAllInfo();
267
    }
268
269
    /**
270
     * Get anime/manga additional pictures.
271
     *
272
     * @param string     $type Either anime or manga
273
     * @param int|string $id   id of the anime or manga
274
     *
275
     * @return array
276
     */
277
    private function getPicture($type, $id)
278
    {
279
        return (new Picture($type, $id))->getAllInfo();
280
    }
281
282
    /**
283
     * Get character additional pictures.
284
     *
285
     * @param int|string $id id of the character
286
     *
287
     * @return array
288
     */
289
    private function getCharacterPicture($id)
290
    {
291
        return (new CharacterPeoplePicture('character', $id))->getAllInfo();
292
    }
293
294
    /**
295
     * Get people additional pictures.
296
     *
297
     * @param int|string $id id of the people
298
     *
299
     * @return array
300
     */
301
    private function getPeoplePicture($id)
302
    {
303
        return (new CharacterPeoplePicture('people', $id))->getAllInfo();
304
    }
305
306
    /**
307
     * Get anime additional review.
308
     *
309
     * @param int|string $id   id of the anime
310
     * @param int|string $page (Optional) Page number
311
     *
312
     * @return array
313
     */
314
    private function getAnimeReview($id, $page = 1)
315
    {
316
        return (new AnimeMangaReview('anime', $id, $page))->getAllInfo();
317
    }
318
319
    /**
320
     * Get manga additional review.
321
     *
322
     * @param int|string $id   id of the manga
323
     * @param int|string $page (Optional) Page number
324
     *
325
     * @return array
326
     */
327
    private function getMangaReview($id, $page = 1)
328
    {
329
        return (new AnimeMangaReview('manga', $id, $page))->getAllInfo();
330
    }
331
332
    /**
333
     * Get anime additional recommendation.
334
     *
335
     * @param int|string $id id of the anime
336
     *
337
     * @return array
338
     */
339
    private function getAnimeRecommendation($id)
340
    {
341
        return (new AnimeMangaRecommendation('anime', $id))->getAllInfo();
342
    }
343
344
    /**
345
     * Get manga additional recommendation.
346
     *
347
     * @param int|string $id id of the manga
348
     *
349
     * @return array
350
     */
351
    private function getMangaRecommendation($id)
352
    {
353
        return (new AnimeMangaRecommendation('manga', $id))->getAllInfo();
354
    }
355
356
    /**
357
     * Get all anime produced by the studio/producer.
358
     *
359
     * @param int|string $id   id of the studio/producer
360
     * @param int|string $page (Optional) Page number
361
     *
362
     * @return array
363
     */
364
    private function getStudioProducer($id, $page = 1)
365
    {
366
        return (new Producer('anime', 'producer', $id, $page))->getAllInfo();
367
    }
368
369
    /**
370
     * Get all manga serialized by the magazine.
371
     *
372
     * @param int|string $id   id of the magazine
373
     * @param int|string $page (Optional) Page number
374
     *
375
     * @return array
376
     */
377
    private function getMagazine($id, $page = 1)
378
    {
379
        return (new Producer('manga', 'producer', $id, $page))->getAllInfo();
380
    }
381
382
    /**
383
     * Get all anime or manga that has the genre.
384
     *
385
     * @param string     $type Either anime or manga
386
     * @param int|string $id   id of the genre
387
     * @param int|string $page (Optional) Page number
388
     *
389
     * @return array
390
     */
391
    private function getGenre($type, $id, $page = 1)
392
    {
393
        return (new Producer($type, 'genre', $id, $page))->getAllInfo();
394
    }
395
396
    /**
397
     * Get list of all anime genre.
398
     *
399
     * @return array
400
     */
401
    private function getAllAnimeGenre()
402
    {
403
        return (new AllGenre('anime'))->getAllInfo();
404
    }
405
406
    /**
407
     * Get list of all manga genre.
408
     *
409
     * @return array
410
     */
411
    private function getAllMangaGenre()
412
    {
413
        return (new AllGenre('manga'))->getAllInfo();
414
    }
415
416
    /**
417
     * Get list of all anime studio/producer.
418
     *
419
     * @return array
420
     */
421
    private function getAllStudioProducer()
422
    {
423
        return (new AllProducer('anime'))->getAllInfo();
424
    }
425
426
    /**
427
     * Get list of all manga magazine.
428
     *
429
     * @return array
430
     */
431
    private function getAllMagazine()
432
    {
433
        return (new AllProducer('manga'))->getAllInfo();
434
    }
435
436
    /**
437
     * Get list of all review.
438
     *
439
     * @param int|string $type Type of review
440
     * @param int|string $page (Optional) Page number
441
     *
442
     * @return array
443
     */
444
    private function getAllReview($type = 'anime', $page = 1)
445
    {
446
        return (new AllReview($type, $page))->getAllInfo();
447
    }
448
449
    /**
450
     * Get list of all recommendation.
451
     *
452
     * @param int|string $type Either anime or manga
453
     * @param int|string $page (Optional) Page number
454
     *
455
     * @return array
456
     */
457
    private function getAllRecommendation($type = 'anime', $page = 1)
458
    {
459
        return (new AllRecommendation($type, $page))->getAllInfo();
460
    }
461
462
    /**
463
     * Get anime search result.
464
     *
465
     * @param string     $query Search query
466
     * @param int|string $page  (Optional) Page number
467
     *
468
     * @return array
469
     */
470
    private function searchAnime($query, $page = 1)
471
    {
472
        return (new SearchAnimeManga('anime', $query, $page))->getAllInfo();
473
    }
474
475
    /**
476
     * Get manga search result.
477
     *
478
     * @param string     $query Search query
479
     * @param int|string $page  (Optional) Page number
480
     *
481
     * @return array
482
     */
483
    private function searchManga($query, $page = 1)
484
    {
485
        return (new SearchAnimeManga('manga', $query, $page))->getAllInfo();
486
    }
487
488
    /**
489
     * Get character search result.
490
     *
491
     * @param string     $query Search query
492
     * @param int|string $page  (Optional) Page number
493
     *
494
     * @return array
495
     */
496
    private function searchCharacter($query, $page = 1)
497
    {
498
        return (new SearchCharacterPeople('character', $query, $page))->getAllInfo();
499
    }
500
501
    /**
502
     * Get people search result.
503
     *
504
     * @param string     $query Search query
505
     * @param int|string $page  (Optional) Page number
506
     *
507
     * @return array
508
     */
509
    private function searchPeople($query, $page = 1)
510
    {
511
        return (new SearchCharacterPeople('people', $query, $page))->getAllInfo();
512
    }
513
514
    /**
515
     * Get user search result.
516
     *
517
     * @param string     $query Search query
518
     * @param int|string $page  (Optional) Page number
519
     *
520
     * @return array
521
     */
522
    private function searchUser($query, $page = 1)
523
    {
524
        return (new SearchUser($query, $page))->getAllInfo();
525
    }
526
527
    /**
528
     * Get seasonal anime.
529
     *
530
     * @param string|int|bool $year   (Optional) Season year
531
     * @param string|bool     $season (Optional) Season (summer,spring,fall,winter)
532
     *
533
     * @return array
534
     */
535
    private function getSeason($year = false, $season = false)
536
    {
537
        return (new Season($year, $season))->getAllInfo();
538
    }
539
540
    /**
541
     * Get top anime.
542
     *
543
     * @param string     $type (Optional) Type of anime
544
     * @param int|string $page (Optional) Page number
545
     *
546
     * @return array
547
     */
548
    private function getTopAnime($type = 0, $page = 1)
549
    {
550
        return (new Top('anime', $type, $page))->getAllInfo();
551
    }
552
553
    /**
554
     * Get top manga.
555
     *
556
     * @param string     $type (Optional) Type of manga
557
     * @param int|string $page (Optional) Page number
558
     *
559
     * @return array
560
     */
561
    private function getTopManga($type = 0, $page = 1)
562
    {
563
        return (new Top('manga', $type, $page))->getAllInfo();
564
    }
565
566
    /**
567
     * Get top character.
568
     *
569
     * @param int|string $page (Optional) Page number
570
     *
571
     * @return array
572
     */
573
    private function getTopCharacter($page = 1)
574
    {
575
        return (new TopCharacter($page))->getAllInfo();
576
    }
577
578
    /**
579
     * Get top people.
580
     *
581
     * @param int|string $page (Optional) Page number
582
     *
583
     * @return array
584
     */
585
    private function getTopPeople($page = 1)
586
    {
587
        return (new TopPeople($page))->getAllInfo();
588
    }
589
590
    /**
591
     * Get user info.
592
     *
593
     * @param string $user Username
594
     *
595
     * @return array
596
     */
597
    private function getUser($user)
598
    {
599
        return (new User($user))->getAllInfo();
600
    }
601
602
    /**
603
     * Get user friend list.
604
     *
605
     * @param string $user Username
606
     *
607
     * @return array
608
     */
609
    private function getUserFriend($user)
610
    {
611
        return (new Friend($user))->getAllInfo();
612
    }
613
614
    /**
615
     * Get user history.
616
     *
617
     * @param string      $user Username
618
     * @param string|bool $type (Optional) Either anime or manga
619
     *
620
     * @return array
621
     */
622
    private function getUserHistory($user, $type = false)
623
    {
624
        return (new History($user, $type))->getAllInfo();
625
    }
626
627
    /**
628
     * Get user list.
629
     *
630
     * @param string $user   Username
631
     * @param string $type   (Optional) Either anime or manga
632
     * @param string $status (Optional) Anime/manga status
633
     *
634
     * @return array
635
     */
636
    private function getUserList($user, $type = 'anime', $status = 7)
637
    {
638
        return (new UserList($user, $type, $status))->getAllInfo();
639
    }
640
641
    /**
642
     * Get user cover.
643
     *
644
     * @param string      $user  Username
645
     * @param string      $type  (Optional) Either anime or manga
646
     * @param string|bool $style (Optional) CSS style for the cover
647
     *
648
     * @return string
649
     */
650
    private function getUserCover($user, $type = 'anime', $style = false)
651
    {
652
        return (new UserCover($user, $type, $style))->getAllInfo();
653
    }
654
}
655