Passed
Push — develop ( c893b5...77095f )
by axel
03:30
created

InfoModel::getCleanerInfo2()   B

Complexity

Conditions 9
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 3
nop 3
dl 0
loc 23
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model\General;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * InfoModel class.
10
 */
11
class InfoModel extends MainModel
12
{
13
    /**
14
     * Type of info. Either anime or manga.
15
     *
16
     * @var string
17
     */
18
    private $_type;
19
20
    /**
21
     * Id of the anime or manga.
22
     *
23
     * @var string|int
24
     */
25
    private $_id;
26
27
    /**
28
     * Default constructor.
29
     *
30
     * @param string     $type
31
     * @param string|int $id
32
     * @param string     $parserArea
33
     *
34
     * @return void
35
     */
36
    public function __construct($type, $id, $parserArea = '#content')
37
    {
38
        $this->_type = $type;
39
        $this->_id = $id;
40
        $this->_url = $this->_myAnimeListUrl.'/'.$type.'/'.$id;
41
        $this->_parserArea = $parserArea;
42
43
        parent::errorCheck($this);
44
    }
45
46
    /**
47
     * Default call.
48
     *
49
     * @param string $method
50
     * @param array  $arguments
51
     *
52
     * @return array|string|int
53
     */
54
    public function __call($method, $arguments)
55
    {
56
        if ($this->_error) {
57
            return $this->_error;
58
        }
59
60
        return call_user_func_array([$this, $method], $arguments);
61
    }
62
63
    /**
64
     * Get anime/manga id.
65
     *
66
     * @return string
67
     */
68
    private function getId()
69
    {
70
        return $this->_id;
71
    }
72
73
    /**
74
     * Get anime/manga cover.
75
     *
76
     * @return string|bool
77
     */
78
    private function getCover()
79
    {
80
        $anime_cover = $this->_parser->find('img.ac', 0);
81
82
        return $anime_cover ? $anime_cover->src : '';
83
    }
84
85
    /**
86
     * Get anime/manga title.
87
     *
88
     * @return string|bool
89
     */
90
    private function getTitle()
91
    {
92
        $anime_cover = $this->_parser->find('img.ac', 0);
93
94
        return $anime_cover ? $anime_cover->alt : '';
95
    }
96
97
    /**
98
     * Get anime/manga alternative title.
99
     *
100
     * @return array
101
     */
102
    private function getTitle2()
103
    {
104
        $title2 = [];
105
106
        $anime_info = $this->_parser->find('.js-scrollfix-bottom', 0);
107
108
        preg_match('/(English:<\/span>)([^<]*)/', $anime_info->innertext, $english);
109
        $title2['english'] = trim($english ? $english[2] : '');
110
111
        preg_match('/(Synonyms:<\/span>)([^<]*)/', $anime_info->innertext, $synonym);
112
        $title2['synonym'] = trim($synonym ? $synonym[2] : '');
113
114
        preg_match('/(Japanese:<\/span>)([^<]*)/', $anime_info->innertext, $japanese);
115
        $title2['japanese'] = trim($japanese ? $japanese[2] : '');
116
117
        return $title2;
118
    }
119
120
    /**
121
     * Get anime/manga synopsis.
122
     *
123
     * @return string
124
     */
125
    private function getSynopsis()
126
    {
127
        $synopsis = $this->_parser->find('span[itemprop=description]', 0);
128
        if ($synopsis) {
129
            $synopsis = $synopsis->plaintext;
130
131
            return trim(preg_replace('/\n[^\S\n]*/', "\n", $synopsis));
132
        } else {
133
            return;
134
        }
135
    }
136
137
    /**
138
     * Get anime/manga score.
139
     *
140
     * @return string
141
     */
142
    private function getScore()
143
    {
144
        $score = $this->_parser->find('div[class="fl-l score"]', 0)->plaintext;
145
        $score = trim($score);
146
147
        return $score != 'N/A' ? $score : null;
148
    }
149
150
    /**
151
     * Get number of user who give score.
152
     *
153
     * @return string
154
     */
155
    private function getVoter()
156
    {
157
        $voter = $this->_parser->find('div[class="fl-l score"]', 0)->getAttribute('data-user');
158
159
        return trim(str_replace(['users', 'user', ','], '', $voter));
160
    }
161
162
    /**
163
     * Get anime/manga rank.
164
     *
165
     * @return string
166
     */
167
    private function getRank()
168
    {
169
        $rank = $this->_parser->find('span[class="numbers ranked"] strong', 0)->plaintext;
170
        $rank = $rank != 'N/A' ? $rank : '';
171
172
        return str_replace('#', '', $rank);
173
    }
174
175
    /**
176
     * Get anime/manga popularity.
177
     *
178
     * @return string
179
     */
180
    private function getPopularity()
181
    {
182
        $popularity = $this->_parser->find('span[class="numbers popularity"] strong', 0)->plaintext;
183
184
        return str_replace('#', '', $popularity);
185
    }
186
187
    /**
188
     * Get number of user who watch/read the anime/manga.
189
     *
190
     * @return string
191
     */
192
    private function getMembers()
193
    {
194
        $member = $this->_parser->find('span[class="numbers members"] strong', 0)->plaintext;
195
196
        return str_replace(',', '', $member);
197
    }
198
199
    /**
200
     * Get number of user who favorite the anime/manga.
201
     *
202
     * @return string
203
     */
204
    private function getFavorite()
205
    {
206
        $favorite = $this->_parser->find('div[data-id=info2]', 0)->next_sibling()->next_sibling()->next_sibling();
207
        $favorite_title = $favorite->find('span', 0)->plaintext;
208
        $favorite = $favorite->plaintext;
209
        $favorite = trim(str_replace($favorite_title, '', $favorite));
210
        $favorite = str_replace(',', '', $favorite);
211
212
        return preg_replace("/([\s])+/", ' ', $favorite);
213
    }
214
215
    /**
216
     * Get anime/manga detail info.
217
     *
218
     * @return array
219
     */
220
    private function getOtherInfo()
221
    {
222
        $info = [];
223
224
        $anime_info = $this->_parser->find('.js-scrollfix-bottom', 0);
225
        $other_info = (count($anime_info->find('h2')) > 2) ? $anime_info->find('h2', 1) : $anime_info->find('h2', 0);
0 ignored issues
show
Bug introduced by
It seems like $anime_info->find('h2') can also be of type simplehtmldom_1_5\simple_html_dom_node; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

225
        $other_info = (count(/** @scrutinizer ignore-type */ $anime_info->find('h2')) > 2) ? $anime_info->find('h2', 1) : $anime_info->find('h2', 0);
Loading history...
226
        $next_info = $other_info->next_sibling();
0 ignored issues
show
Bug introduced by
The method next_sibling() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

226
        /** @scrutinizer ignore-call */ 
227
        $next_info = $other_info->next_sibling();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
227
        while (true) {
228
            $info_type = $next_info->find('span', 0)->plaintext;
229
230
            $clean_info_type = strtolower(str_replace(': ', '', $info_type));
231
            $clean_info_value = $this->getCleanInfo($info_type, $next_info);
232
            $clean_info_value = $this->getCleanerInfo1($clean_info_type, $clean_info_value);
233
            $clean_info_value = $this->getCleanerInfo2($next_info, $clean_info_type, $clean_info_value);
234
235
            $info[$clean_info_type] = $clean_info_value;
236
237
            $next_info = $next_info->next_sibling();
238
            if ($next_info->tag == 'h2' || $next_info->tag == 'br') {
239
                break;
240
            }
241
        }
242
243
        return $info;
244
    }
245
246
    /**
247
     * Get clean other info.
248
     *
249
     * @param string                             $info_type
250
     * @param \simplehtmldom_1_5\simple_html_dom $next_info
251
     *
252
     * @return string
253
     */
254
    private function getCleanInfo($info_type, $next_info)
255
    {
256
        $info_value = $next_info->plaintext;
257
        $clean_info_value = trim(str_replace($info_type, '', $info_value));
258
        $clean_info_value = preg_replace("/([\s])+/", ' ', $clean_info_value);
259
260
        return str_replace([', add some', '?', 'Not yet aired', 'Unknown'], '', $clean_info_value);
261
    }
262
263
    /**
264
     * Get cleaner other info.
265
     *
266
     * @param string $clean_info_type
267
     * @param string $clean_info_value
268
     *
269
     * @return string|array
270
     */
271
    private function getCleanerInfo1($clean_info_type, $clean_info_value)
272
    {
273
        if ($clean_info_type == 'published' || $clean_info_type == 'aired') {
274
            $start_air = $end_air = '';
275
            if ($clean_info_value != 'Not available') {
276
                $parsed_airing = explode(' to ', $clean_info_value);
277
                $start_air = ($parsed_airing[0] != '?') ? $parsed_airing[0] : '';
278
                if (count($parsed_airing) > 1) {
279
                    $end_air = ($parsed_airing[1] != '?') ? $parsed_airing[1] : '';
280
                }
281
            }
282
283
            $clean_info_value = [];
284
            $clean_info_value['start'] = $start_air;
285
            $clean_info_value['end'] = $end_air;
286
        }
287
288
        return $clean_info_value;
289
    }
290
291
    /**
292
     * Get cleaner other info.
293
     *
294
     * @param \simplehtmldom_1_5\simple_html_dom $next_info
295
     * @param string                             $clean_info_type
296
     * @param string|array                       $clean_info_value
297
     *
298
     * @return string|array
299
     */
300
    private function getCleanerInfo2($next_info, $clean_info_type, $clean_info_value)
301
    {
302
        if ($clean_info_type == 'producers'
303
            || $clean_info_type == 'licensors'
304
            || $clean_info_type == 'studios'
305
            || $clean_info_type == 'genres'
306
            || $clean_info_type == 'authors'
307
        ) {
308
            $info_temp = [];
309
            $info_temp_index = 0;
310
            if ($clean_info_value != 'None found') {
311
                foreach ($next_info->find('a') as $each_info) {
312
                    $temp_id = explode('/', $each_info->href);
313
                    $info_temp[$info_temp_index]['id'] = $clean_info_type == 'authors' ? $temp_id[2] : $temp_id[3];
314
                    $info_temp[$info_temp_index]['name'] = $each_info->plaintext;
315
                    $info_temp_index++;
316
                }
317
            }
318
319
            return $info_temp;
320
        }
321
322
        return $clean_info_value;
323
    }
324
325
    /**
326
     * Get anime/manga relation.
327
     *
328
     * @return array
329
     */
330
    private function getRelated()
331
    {
332
        $related = [];
333
        $related_area = $this->_parser->find('.anime_detail_related_anime', 0);
334
        if ($related_area) {
335
            foreach ($related_area->find('tr') as $rel) {
336
                $rel_type = $rel->find('td', 0)->plaintext;
337
                $rel_type = trim(strtolower(str_replace(':', '', $rel_type)));
338
339
                $each_rel = [];
340
                $each_rel_index = 0;
341
                $rel_anime = $rel->find('td', 1);
342
                foreach ($rel_anime->find('a') as $r) {
343
                    $each_rel[$each_rel_index] = $this->getRelatedDetail($r);
344
                    $each_rel_index++;
345
                }
346
347
                $related[$rel_type] = $each_rel;
348
            }
349
        }
350
351
        return $related;
352
    }
353
354
    /**
355
     * Get related detail.
356
     *
357
     * @param \simplehtmldom_1_5\simple_html_dom $r
358
     *
359
     * @return array
360
     */
361
    private function getRelatedDetail($r)
362
    {
363
        $related = [];
364
        $rel_anime_link = $r->href;
365
        $separated_anime_link = explode('/', $rel_anime_link);
366
367
        $related['id'] = $separated_anime_link[2];
368
        $related['title'] = $r->plaintext;
369
        $related['type'] = $separated_anime_link[1];
370
371
        return $related;
372
    }
373
374
    /**
375
     * Get anime/manga character and its va.
376
     *
377
     * @return array
378
     */
379
    private function getCharacter()
380
    {
381
        $character = [];
382
        $char_index = 0;
383
        $character_area = $this->_parser->find('div[class^=detail-characters-list]', 0);
384
        if ($character_area) {
385
            $character_list = [
386
                $character_area->find('div[class*=fl-l]', 0),
387
                $character_area->find('div[class*=fl-r]', 0),
388
            ];
389
            foreach ($character_list as $character_side) {
390
                if ($character_side) {
391
                    foreach ($character_side->find('table[width=100%]') as $each_char) {
392
                        $char = $each_char->find('tr td', 1);
393
                        $va = $each_char->find('table td', 0);
394
395
                        $character[$char_index]['id'] = $this->getStaffId($char);
396
                        $character[$char_index]['name'] = $this->getStaffName($char);
397
                        $character[$char_index]['role'] = $this->getStaffRole($char);
398
                        $character[$char_index]['image'] = $this->getStaffImage($each_char);
399
400
                        $character[$char_index]['va_id'] = $character[$char_index]['va_name'] = '';
401
                        $character[$char_index]['va_role'] = $character[$char_index]['va_image'] = '';
402
403
                        if ($va) {
404
                            $character[$char_index]['va_id'] = $this->getStaffId($va);
405
                            $character[$char_index]['va_name'] = $this->getStaffName($va, true);
406
                            $character[$char_index]['va_role'] = $this->getStaffRole($va);
407
                            $character[$char_index]['va_image'] = $this->getStaffImage($each_char, true);
408
                        }
409
410
                        $char_index++;
411
                    }
412
                }
413
            }
414
        }
415
416
        return $character;
417
    }
418
419
    /**
420
     * Get anime/manga staff involved.
421
     *
422
     * @return array
423
     */
424
    private function getStaff()
425
    {
426
        $staff = [];
427
        $staff_index = 0;
428
        $staff_area = $this->_parser->find('div[class^=detail-characters-list]', 1);
429
        if ($staff_area) {
430
            $staff_list = [
431
                $staff_area->find('div[class*=fl-l]', 0),
432
                $staff_area->find('div[class*=fl-r]', 0),
433
            ];
434
            foreach ($staff_list as $staff_side) {
435
                if ($staff_side) {
436
                    foreach ($staff_side->find('table[width=100%]') as $each_staff) {
437
                        $st = $each_staff->find('tr td', 1);
438
439
                        $staff[$staff_index]['id'] = $this->getStaffId($st);
440
                        $staff[$staff_index]['name'] = $this->getStaffName($st);
441
                        $staff[$staff_index]['role'] = $this->getStaffRole($st);
442
                        $staff[$staff_index]['image'] = $this->getStaffImage($each_staff);
443
444
                        $staff_index++;
445
                    }
446
                }
447
            }
448
        }
449
450
        return $staff;
451
    }
452
453
    /**
454
     * Get staff id.
455
     *
456
     * @param \simplehtmldom_1_5\simple_html_dom $st
457
     *
458
     * @return string
459
     */
460
    private function getStaffId($st)
461
    {
462
        $staff_id = $st->find('a', 0)->href;
463
        $staff_id = explode('/', $staff_id);
464
465
        return $staff_id[4];
466
    }
467
468
    /**
469
     * Get staff name.
470
     *
471
     * @param \simplehtmldom_1_5\simple_html_dom $st
472
     * @param bool                               $va (Optional)
473
     *
474
     * @return string
475
     */
476
    private function getStaffName($st, $va = false)
477
    {
478
        if ($va) {
479
            return $st->find('a', 0)->plaintext;
480
        }
481
482
        return trim(preg_replace('/\s+/', ' ', $st->find('a', 0)->plaintext));
483
    }
484
485
    /**
486
     * Get staff role.
487
     *
488
     * @param \simplehtmldom_1_5\simple_html_dom $st
489
     *
490
     * @return string
491
     */
492
    private function getStaffRole($st)
493
    {
494
        return trim($st->find('small', 0)->plaintext);
495
    }
496
497
    /**
498
     * Get staff image.
499
     *
500
     * @param \simplehtmldom_1_5\simple_html_dom $each_staff
501
     * @param bool                               $va         (Optional)
502
     *
503
     * @return string
504
     */
505
    private function getStaffImage($each_staff, $va = false)
506
    {
507
        if ($va) {
508
            $staff_image = $each_staff->find('table td', 1)->find('img', 0)->getAttribute('data-src');
509
        } else {
510
            $staff_image = $each_staff->find('tr td', 0)->find('img', 0)->getAttribute('data-src');
511
        }
512
513
        return Helper::imageUrlCleaner($staff_image);
514
    }
515
516
    /**
517
     * Get anime/manga opening and ending song.
518
     *
519
     * @return array
520
     */
521
    private function getSong()
522
    {
523
        $song = [];
524
        $song_area = $this->_parser->find('div[class*="theme-songs opnening"]', 0);
525
        if ($song_area) {
526
            foreach ($song_area->find('span.theme-song') as $each_song) {
527
                $each_song = trim(preg_replace('/#\d*:\s/', '', $each_song->plaintext));
528
                $song['opening'][] = $each_song;
529
            }
530
        }
531
532
        $song_area = $this->_parser->find('div[class*="theme-songs ending"]', 0);
533
        if ($song_area) {
534
            foreach ($song_area->find('span.theme-song') as $each_song) {
535
                $each_song = trim(preg_replace('/#\d*:\s/', '', $each_song->plaintext));
536
                $song['closing'][] = $each_song;
537
            }
538
        }
539
540
        return $song;
541
    }
542
543
    /**
544
     * Get anime/manga all information.
545
     *
546
     * @return array
547
     */
548
    private function getAllInfo()
549
    {
550
        $data = [
551
            'id'        => $this->getId(),
552
            'cover'     => $this->getCover(),
553
            'title'     => $this->getTitle(),
554
            'title2'    => $this->getTitle2(),
555
            'synopsis'  => $this->getSynopsis(),
556
            'score'     => $this->getScore(),
557
            'voter'     => $this->getVoter(),
558
            'rank'      => $this->getRank(),
559
            'popularity'=> $this->getPopularity(),
560
            'members'   => $this->getMembers(),
561
            'favorite'  => $this->getFavorite(),
562
        ];
563
564
        $data = array_merge($data, $this->getOtherInfo());
565
566
        $data2 = [
567
            'related'   => $this->getRelated(),
568
            'character' => $this->getCharacter(),
569
            'staff'     => $this->getStaff(),
570
            'song'      => $this->getSong(),
571
        ];
572
573
        $data = array_merge($data, $data2);
574
575
        return $data;
576
    }
577
}
578