Passed
Push — develop ( f20136...b10922 )
by axel
02:15
created

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