Passed
Push — feature/super-model ( 13c8a0...a850c4 )
by axel
01:59
created

PeopleModel::getStaff()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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