Passed
Branch feature/super-model (24c950)
by axel
02:55
created

PeopleModel::getMore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model;
4
5
use MalScraper\Helper\Helper;
6
7
/**
8
 * PeopleModel class.
9
 */
10
class PeopleModel extends MainModel
11
{
12
    /**
13
     * Id of the people.
14
     *
15
     * @var string|int
16
     */
17
	private $_id;
18
19
    /**
20
     * Biodata area.
21
     *
22
     * @var string
23
     */
24
    private $_biodata;
25
26
    /**
27
     * Default constructor.
28
     *
29
     * @param string|int $id
30
     * @param string $parserArea
31
     *
32
     * @return void
33
     */
34
	public function __construct($id, $parserArea = '#contentWrapper')
35
    {
36
    	$this->_id = $id;
37
        $this->_url = $this->_myAnimeListUrl.'/people/'.$id;
38
    	$this->_parserArea = $parserArea;
39
40
        parent::errorCheck($this);
41
42
        if (!$this->_error)
43
            self::setBiodata();
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\PeopleModel::setBiodata() is not static, but was called statically. ( Ignorable by Annotation )

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

43
            self::/** @scrutinizer ignore-call */ 
44
                  setBiodata();
Loading history...
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
        return call_user_func_array([$this, $method], $arguments);
59
    }
60
61
    /**
62
     * Get people id.
63
     *
64
     * @return string
65
     */
66
    private function getId()
67
    {
68
        return $this->_id;
69
    }
70
71
    /**
72
     * Get people name.
73
     *
74
     * @return string
75
     */
76
    private function getName()
77
    {
78
        return $this->_parser->find('h1', 0)->plaintext;
79
    }
80
81
    /**
82
     * Get people image.
83
     *
84
     * @return string
85
     */
86
    private function getImage()
87
    {
88
        $image = $this->_parser->find('#content table tr', 0)->find('td', 0)->find('img', 0);
89
        return $image ? $image->src : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $image ? $image->src : '' also could return the type boolean which is incompatible with the documented return type string.
Loading history...
90
    }
91
92
    /**
93
     * Set people biodata.
94
     *
95
     * @return void
96
     */
97
    private function setBiodata()
98
    {
99
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0);
100
        $biodata = $html->innertext;
101
        $useless_biodata = '';
102
        $useless_area = $html->find('div', 0);
103
        for ($i = 0; $i < 4; $i++) {
104
            $useless_biodata .= $useless_area->outertext;
105
            $useless_area = $useless_area->next_sibling();
106
        }
107
        $biodata = str_replace($useless_biodata, '', $biodata);
108
        $this->_biodata = preg_replace("/([\s])+/", ' ', $biodata);
109
    }
110
111
    /**
112
     * Get people biodata.
113
     *
114
     * @param string $type Biodata type
115
     *
116
     * @return string|array
117
     */
118
    private function getBiodata($type)
119
    {
120
        switch ($type) {
121
            case 'given_name':
122
                preg_match("/(Given name:<\/span>)[^<]*/", $this->_biodata, $biodata);
123
                break;
124
            case 'family_name':
125
                preg_match("/(Family name:<\/span>)[^<]*/", $this->_biodata, $biodata);
126
                break;
127
            case 'alternative_name':
128
                preg_match("/(Alternate names:<\/span>)[^<]*/", $this->_biodata, $biodata);
129
                break;
130
            case 'birthday':
131
                preg_match("/(Birthday:<\/span>)([^<])*/", $this->_biodata, $biodata);
132
                break;
133
            case 'website':
134
                preg_match("/(Website:<\/span> <a)([^<])*/", $this->_biodata, $biodata);
135
                break;
136
            case 'favorite':
137
                preg_match("/(Member Favorites:<\/span>)([^<])*/", $this->_biodata, $biodata);
138
                break;
139
            default:
140
                return null;
141
        }
142
143
        if ($biodata) {
144
            if ($type != 'website') {
145
                $biodata = strip_tags($biodata[0]);
146
                $biodata = explode(': ', $biodata);
147
                $biodata = trim($biodata[1]);
148
            }
149
150
            if ($type == 'given_name' || $type == 'family_name' || $type == 'birthday')
151
                return $biodata;
152
153
            if ($type == 'alternative_name')
154
                return explode(', ', $biodata);
155
156
            if ($type == 'favorite')
157
                return str_replace(',', '', $biodata);
158
159
            if ($type == 'website') {
160
                preg_match('/".+"/', $biodata[0], $biodata);
161
                if ($biodata[0] != '"http://"')
162
                    return str_replace('"', '', $biodata[0]);
163
            }
164
        }
165
        return null;
166
    }
167
168
    /**
169
     * Get people more information.
170
     *
171
     * @return string
172
     */
173
    private function getMore()
174
    {
175
        $more = $this->_parser->find('#content table tr', 0)->find('td', 0);
176
        $more = $more->find('div[class^=people-informantion-more]', 0)->plaintext;
177
        return preg_replace('/\n[^\S\n]*/', "\n", $more);
178
    }
179
180
    /**
181
     * Get people voice actor list.
182
     *
183
     * @return array
184
     */
185
    private function getVa()
186
    {
187
        $va = [];
188
        $va_index = 0;
189
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
190
        $va_area = $html->find('.normal_header', 0)->next_sibling();
191
        if ($va_area->tag == 'table') {
192
            if ($va_area->find('tr')) {
193
                foreach ($va_area->find('tr') as $each_va) {
194
                    // anime image
195
                    $anime_image = $each_va->find('td', 0)->find('img', 0)->getAttribute('data-src');
196
                    $va[$va_index]['anime']['image'] = Helper::imageUrlCleaner($anime_image);
197
198
                    $anime_area = $each_va->find('td', 1);
199
200
                    // anime id
201
                    $anime_id = $anime_area->find('a', 0)->href;
202
                    $parsed_anime_id = explode('/', $anime_id);
203
                    $anime_id = $parsed_anime_id[4];
204
                    $va[$va_index]['anime']['id'] = $anime_id;
205
206
                    // anime title
207
                    $anime_title = $anime_area->find('a', 0)->plaintext;
208
                    $va[$va_index]['anime']['title'] = $anime_title;
209
210
                    // character image
211
                    $character_image = $each_va->find('td', 3)->find('img', 0)->getAttribute('data-src');
212
                    $va[$va_index]['character']['image'] = Helper::imageUrlCleaner($character_image);
213
214
                    $character_area = $each_va->find('td', 2);
215
216
                    // character id
217
                    $character_id = $character_area->find('a', 0)->href;
218
                    $parsed_character_id = explode('/', $character_id);
219
                    $character_id = $parsed_character_id[4];
220
                    $va[$va_index]['character']['id'] = $character_id;
221
222
                    // character name
223
                    $character_name = $character_area->find('a', 0)->plaintext;
224
                    $va[$va_index]['character']['name'] = $character_name;
225
226
                    // character role
227
                    $character_role = $character_area->find('div', 0)->plaintext;
228
                    $va[$va_index]['character']['role'] = $character_role;
229
230
                    $va_index++;
231
                }
232
            }
233
        }
234
        return $va;
235
    }
236
237
    /**
238
     * Get people staff list.
239
     *
240
     * @return array
241
     */
242
    private function getStaff()
243
    {
244
        $staff = [];
245
        $staff_index = 0;
246
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
247
        $staff_area = $html->find('.normal_header', 1)->next_sibling();
248
        if ($staff_area->tag == 'table') {
249
            foreach ($staff_area->find('tr') as $each_staff) {
250
                $anime_image = $each_staff->find('td', 0)->find('img', 0)->getAttribute('data-src');
251
                $staff[$staff_index]['image'] = Helper::imageUrlCleaner($anime_image);
252
253
                $each_staff = $each_staff->find('td', 1);
254
255
                // anime id
256
                $anime_id = $each_staff->find('a', 0)->href;
257
                $parsed_anime_id = explode('/', $anime_id);
258
                $anime_id = $parsed_anime_id[4];
259
                $staff[$staff_index]['id'] = $anime_id;
260
261
                // anime title
262
                $anime_title = $each_staff->find('a', 0)->plaintext;
263
                $staff[$staff_index]['title'] = $anime_title;
264
265
                // role
266
                $role = $each_staff->find('small', 0)->plaintext;
267
                $staff[$staff_index]['role'] = $role;
268
269
                $staff_index++;
270
            }
271
        }
272
        return $staff;
273
    }
274
275
    /**
276
     * Get people published manga list.
277
     *
278
     * @return array
279
     */
280
    private function getManga()
281
    {
282
        $published_manga = [];
283
        $manga_index = 0;
284
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
285
        $manga_area = $html->find('.normal_header', 2)->next_sibling();
286
        if ($manga_area->tag == 'table') {
287
            foreach ($manga_area->find('tr') as $each_manga) {
288
                $manga_image = $each_manga->find('td', 0)->find('img', 0)->getAttribute('data-src');
289
                $published_manga[$manga_index]['image'] = Helper::imageUrlCleaner($manga_image);
290
291
                $each_manga = $each_manga->find('td', 1);
292
293
                // manga id
294
                $manga_id = $each_manga->find('a', 0)->href;
295
                $parsed_manga_id = explode('/', $manga_id);
296
                $manga_id = $parsed_manga_id[4];
297
                $published_manga[$manga_index]['id'] = $manga_id;
298
299
                // manga title
300
                $manga_title = $each_manga->find('a', 0)->plaintext;
301
                $published_manga[$manga_index]['title'] = $manga_title;
302
303
                // role
304
                $role = $each_manga->find('small', 0)->plaintext;
305
                $published_manga[$manga_index]['role'] = $role;
306
307
                $manga_index++;
308
            }
309
        }
310
        return $published_manga;
311
    }
312
313
    /**
314
     * Get people all information.
315
     *
316
     * @return array
317
     */
318
    private function getAllInfo()
319
    {
320
        $data = [
321
            'id'               => self::getId(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\PeopleModel::getId() is not static, but was called statically. ( Ignorable by Annotation )

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

321
            'id'               => self::/** @scrutinizer ignore-call */ getId(),
Loading history...
322
            'name'             => self::getName(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\PeopleModel::getName() is not static, but was called statically. ( Ignorable by Annotation )

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

322
            'name'             => self::/** @scrutinizer ignore-call */ getName(),
Loading history...
323
            'image'            => self::getImage(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\PeopleModel::getImage() is not static, but was called statically. ( Ignorable by Annotation )

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

323
            'image'            => self::/** @scrutinizer ignore-call */ getImage(),
Loading history...
324
            'given_name'       => self::getBiodata('given_name'),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\PeopleModel::getBiodata() is not static, but was called statically. ( Ignorable by Annotation )

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

324
            'given_name'       => self::/** @scrutinizer ignore-call */ getBiodata('given_name'),
Loading history...
325
            'family_name'      => self::getBiodata('family_name'),
326
            'alternative_name' => self::getBiodata('alternative_name'),
327
            'birthday'         => self::getBiodata('birthday'),
328
            'website'          => self::getBiodata('website'),
329
            'favorite'         => self::getBiodata('favorite'),
330
            'more'             => self::getMore(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\PeopleModel::getMore() is not static, but was called statically. ( Ignorable by Annotation )

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

330
            'more'             => self::/** @scrutinizer ignore-call */ getMore(),
Loading history...
331
            'va'               => self::getVa(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\PeopleModel::getVa() is not static, but was called statically. ( Ignorable by Annotation )

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

331
            'va'               => self::/** @scrutinizer ignore-call */ getVa(),
Loading history...
332
            'staff'            => self::getStaff(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\PeopleModel::getStaff() is not static, but was called statically. ( Ignorable by Annotation )

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

332
            'staff'            => self::/** @scrutinizer ignore-call */ getStaff(),
Loading history...
333
            'published_manga'  => self::getManga(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\PeopleModel::getManga() is not static, but was called statically. ( Ignorable by Annotation )

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

333
            'published_manga'  => self::/** @scrutinizer ignore-call */ getManga(),
Loading history...
334
        ];
335
336
        return $data;
337
    }
338
}