PeopleModel   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 312
Duplicated Lines 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 105
c 2
b 2
f 0
dl 0
loc 312
rs 9.84
wmc 32

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setBiodata() 0 12 2
A getId() 0 3 1
A getName() 0 3 1
A getAnimeTitle() 0 3 1
A getVa() 0 33 4
A getAnimeId() 0 6 1
A getAnimeImage() 0 5 1
A getImage() 0 5 2
A getAnimeRole() 0 7 2
A getAllInfo() 0 19 1
A getStaff() 0 25 4
A __construct() 0 10 2
A __call() 0 7 2
B getBiodata() 0 28 7
A getMore() 0 6 1
1
<?php
2
3
namespace MalScraper\Model\General;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * PeopleModel class.
10
 */
11
class PeopleModel extends MainModel
12
{
13
    /**
14
     * Id of the people.
15
     *
16
     * @var string|int
17
     */
18
    private $_id;
19
20
    /**
21
     * Biodata area.
22
     *
23
     * @var string
24
     */
25
    private $_biodata;
26
27
    /**
28
     * Default constructor.
29
     *
30
     * @param string|int $id
31
     * @param string     $parserArea
32
     *
33
     * @return void
34
     */
35
    public function __construct($id, $parserArea = '#contentWrapper')
36
    {
37
        $this->_id = $id;
38
        $this->_url = $this->_myAnimeListUrl.'/people/'.$id;
39
        $this->_parserArea = $parserArea;
40
41
        parent::errorCheck($this);
42
43
        if (!$this->_error) {
44
            $this->setBiodata();
45
        }
46
    }
47
48
    /**
49
     * Default call.
50
     *
51
     * @param string $method
52
     * @param array  $arguments
53
     *
54
     * @return array|string|int
55
     */
56
    public function __call($method, $arguments)
57
    {
58
        if ($this->_error) {
59
            return $this->_error;
60
        }
61
62
        return call_user_func_array([$this, $method], $arguments);
63
    }
64
65
    /**
66
     * Get people id.
67
     *
68
     * @return string
69
     */
70
    private function getId()
71
    {
72
        return $this->_id;
73
    }
74
75
    /**
76
     * Get people name.
77
     *
78
     * @return string
79
     */
80
    private function getName()
81
    {
82
        return $this->_parser->find('h1', 0)->plaintext;
83
    }
84
85
    /**
86
     * Get people image.
87
     *
88
     * @return string|bool
89
     */
90
    private function getImage()
91
    {
92
        $image = $this->_parser->find('#content table tr', 0)->find('td', 0)->find('img', 0);
93
94
        return $image ? $image->src : '';
95
    }
96
97
    /**
98
     * Set people biodata.
99
     *
100
     * @return void
101
     */
102
    private function setBiodata()
103
    {
104
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0);
105
        $biodata = $html->innertext;
106
        $useless_biodata = '';
107
        $useless_area = $html->find('div', 0);
108
        for ($i = 0; $i < 4; $i++) {
109
            $useless_biodata .= $useless_area->outertext;
110
            $useless_area = $useless_area->next_sibling();
111
        }
112
        $biodata = str_replace($useless_biodata, '', $biodata);
113
        $this->_biodata = preg_replace("/([\s])+/", ' ', $biodata);
114
    }
115
116
    /**
117
     * Get people biodata.
118
     *
119
     * @param string $type Biodata type
120
     *
121
     * @return string|array
122
     */
123
    private function getBiodata($type)
124
    {
125
        if ($type == 'Website') {
126
            preg_match('/('.$type.":<\/span> <a)[^<]*/", $this->_biodata, $biodata);
127
            if ($biodata) {
128
                preg_match('/".+"/', $biodata[0], $biodata);
129
                if ($biodata[0] != '"http://"') {
130
                    return str_replace('"', '', $biodata[0]);
131
                }
132
            }
133
        }
134
135
        preg_match('/('.$type.":<\/span>)[^<]*/", $this->_biodata, $biodata);
136
137
        if ($biodata) {
138
            $biodata = strip_tags($biodata[0]);
139
            $biodata = explode(': ', $biodata);
140
            $biodata = trim($biodata[1]);
141
142
            if ($type == 'Alternate names') {
143
                return explode(', ', $biodata);
144
            }
145
146
            if ($type == 'Member Favorites') {
147
                return str_replace(',', '', $biodata);
148
            }
149
150
            return $biodata;
151
        }
152
    }
153
154
    /**
155
     * Get people more information.
156
     *
157
     * @return string
158
     */
159
    private function getMore()
160
    {
161
        $more = $this->_parser->find('#content table tr', 0)->find('td', 0);
162
        $more = $more->find('div[class^=people-informantion-more]', 0)->plaintext;
163
164
        return preg_replace('/\n[^\S\n]*/', "\n", $more);
165
    }
166
167
    /**
168
     * Get people voice actor list.
169
     *
170
     * @return array
171
     */
172
    private function getVa()
173
    {
174
        $va = [];
175
        $va_index = 0;
176
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
177
        $va_area = $html->find('.normal_header', 0)->next_sibling();
178
        if ($va_area->tag == 'table') {
179
            if ($va_area->find('tr')) {
180
                foreach ($va_area->find('tr') as $each_va) {
181
182
                    // anime
183
                    $anime_image_area = $each_va->find('td', 0);
184
                    $anime_area = $each_va->find('td', 1);
185
186
                    $va[$va_index]['anime']['image'] = $this->getAnimeImage($anime_image_area);
187
                    $va[$va_index]['anime']['id'] = $this->getAnimeId($anime_area);
188
                    $va[$va_index]['anime']['title'] = $this->getAnimeTitle($anime_area);
189
190
                    // character
191
                    $character_image_area = $each_va->find('td', 3);
192
                    $character_area = $each_va->find('td', 2);
193
194
                    $va[$va_index]['character']['image'] = $this->getAnimeImage($character_image_area);
195
                    $va[$va_index]['character']['id'] = $this->getAnimeId($character_area);
196
                    $va[$va_index]['character']['name'] = $this->getAnimeTitle($character_area);
197
                    $va[$va_index]['character']['role'] = $this->getAnimeRole($character_area);
198
199
                    $va_index++;
200
                }
201
            }
202
        }
203
204
        return $va;
205
    }
206
207
    /**
208
     * Get anime id.
209
     *
210
     * @param \simplehtmldom_1_5\simple_html_dom $anime_area
211
     *
212
     * @return string
213
     */
214
    private function getAnimeId($anime_area)
215
    {
216
        $anime_id = $anime_area->find('a', 0)->href;
217
        $parsed_anime_id = explode('/', $anime_id);
218
219
        return $parsed_anime_id[4];
220
    }
221
222
    /**
223
     * Get anime title.
224
     *
225
     * @param \simplehtmldom_1_5\simple_html_dom $anime_area
226
     *
227
     * @return string
228
     */
229
    private function getAnimeTitle($anime_area)
230
    {
231
        return $anime_area->find('a', 0)->plaintext;
232
    }
233
234
    /**
235
     * Get anime image.
236
     *
237
     * @param \simplehtmldom_1_5\simple_html_dom $anime_image_area
238
     *
239
     * @return string
240
     */
241
    private function getAnimeImage($anime_image_area)
242
    {
243
        $anime_image_area = $anime_image_area->find('img', 0)->getAttribute('data-src');
244
245
        return Helper::imageUrlCleaner($anime_image_area);
246
    }
247
248
    /**
249
     * Get anime role.
250
     *
251
     * @param \simplehtmldom_1_5\simple_html_dom $anime_area
252
     * @param bool                               $staff      (Optional)
253
     *
254
     * @return string
255
     */
256
    private function getAnimeRole($anime_area, $staff = false)
257
    {
258
        if ($staff) {
259
            return $anime_area->find('small', 0)->plaintext;
260
        }
261
262
        return $anime_area->find('div', 0)->plaintext;
263
    }
264
265
    /**
266
     * Get people staff list.
267
     *
268
     * @param bool $staff (Optional)
269
     *
270
     * @return array
271
     */
272
    private function getStaff($manga = false)
273
    {
274
        $staff = [];
275
        $staff_index = 0;
276
        $html = $this->_parser->find('#content table tr', 0)->find('td', 0)->next_sibling();
277
        if ($manga) {
278
            $staff_area = $html->find('.normal_header', 2)->next_sibling();
279
        } else {
280
            $staff_area = $html->find('.normal_header', 1)->next_sibling();
281
        }
282
        if ($staff_area->tag == 'table') {
283
            foreach ($staff_area->find('tr') as $each_staff) {
284
                $anime_image_area = $each_staff->find('td', 0);
285
                $staff_area = $each_staff->find('td', 1);
286
287
                $staff[$staff_index]['image'] = $this->getAnimeImage($anime_image_area);
288
                $staff[$staff_index]['id'] = $this->getAnimeId($staff_area);
289
                $staff[$staff_index]['title'] = $this->getAnimeTitle($staff_area);
290
                $staff[$staff_index]['role'] = $this->getAnimeRole($staff_area, true);
291
292
                $staff_index++;
293
            }
294
        }
295
296
        return $staff;
297
    }
298
299
    /**
300
     * Get people all information.
301
     *
302
     * @return array
303
     */
304
    private function getAllInfo()
305
    {
306
        $data = [
307
            'id'               => $this->getId(),
308
            'name'             => $this->getName(),
309
            'image'            => $this->getImage(),
310
            'given_name'       => $this->getBiodata('Given name'),
311
            'family_name'      => $this->getBiodata('Family name'),
312
            'alternative_name' => $this->getBiodata('Alternate names'),
313
            'birthday'         => $this->getBiodata('Birthday'),
314
            'website'          => $this->getBiodata('Website'),
315
            'favorite'         => $this->getBiodata('Member Favorites'),
316
            'more'             => $this->getMore(),
317
            'va'               => $this->getVa(),
318
            'staff'            => $this->getStaff(),
319
            'published_manga'  => $this->getStaff(true),
320
        ];
321
322
        return $data;
323
    }
324
}
325