Passed
Push — feature/super-model ( 24c950...13c8a0 )
by axel
02:30
created

CharacterStaffModel::getCharacterVaRole()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MalScraper\Model\Additional;
4
5
use MalScraper\Helper\Helper;
6
use MalScraper\Model\MainModel;
7
8
/**
9
 * CharacterStaffModel class.
10
 */
11
class CharacterStaffModel 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 = '.js-scrollfix-bottom-rel')
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
        return call_user_func_array([$this, $method], $arguments);
59
    }
60
61
    /**
62
     * Get type (anime or manga).
63
     *
64
     * @return string
65
     */
66
    private function getType()
67
    {
68
        return $this->_type;
69
    }
70
71
    /**
72
     * Get anime/manga id.
73
     *
74
     * @return string
75
     */
76
    private function getId()
77
    {
78
    	return $this->_id;
79
    }
80
81
    /**
82
     * Get anime/manga character list.
83
     *
84
     * @return array
85
     */
86
    private function getCharacter()
87
    {
88
        $character = [];
89
        $character_index = 0;
90
        $char_table = $this->_parser->find('h2', 0);
91
        if ($char_table->next_sibling()->tag == 'table') {
92
            $char_table = $char_table->next_sibling();
93
            while (true) {
94
                $char_name_area = $char_table->find('td', 1);
95
96
                $character[$character_index]['image'] = self::getCharacterImage($char_table);
97
                $character[$character_index]['id'] = self::getCharacterId($char_name_area);
98
                $character[$character_index]['name'] = self::getCharacterName($char_name_area);
99
                $character[$character_index]['role'] = self::getCharacterRole($char_name_area);
100
101
                // va name + role
102
                $va = [];
103
                $va_index = 0;
104
                $char_va_area = $char_table->find('td', 2);
105
                if ($char_va_area) {
106
                    $char_va_area = $char_va_area->find('table', 0);
107
                    foreach ($char_va_area->find('tr') as $each_va) {
108
                        $va_name_area = $each_va->find('td', 0);
109
110
                        $va[$va_index]['id'] = self::getCharacterVaId($va_name_area);
111
                        $va[$va_index]['name'] = self::getCharacterVaName($va_name_area);
112
                        $va[$va_index]['role'] = self::getCharacterVaRole($va_name_area);
113
                        $va[$va_index]['image'] = self::getCharacterVaImage($each_va);
114
115
                        $va_index++;
116
                    }
117
                    $character[$character_index]['va'] = $va;
118
                }
119
120
                $char_table = $char_table->next_sibling();
121
                if ($char_table->tag == 'br' || $char_table->tag == 'a' || $char_table->tag == 'h2' || $char_table->tag == 'div')
122
                    break;
123
                $character_index++;
124
            }
125
        }
126
        return $character;
127
    }
128
129
    /**
130
     * Get anime/manga character image.
131
     *
132
     * @param \simplehtmldom_1_5\simple_html_dom $char_table
133
     *
134
     * @return string
135
     */
136
    static private function getCharacterImage($char_table)
137
    {
138
        $char_image = $char_table->find('td .picSurround img', 0)->getAttribute('data-src');
139
        return Helper::imageUrlCleaner($char_image);
140
    }
141
142
    /**
143
     * Get anime/manga character id.
144
     *
145
     * @param \simplehtmldom_1_5\simple_html_dom $char_name_area
146
     *
147
     * @return string
148
     */
149
    static private function getCharacterId($char_name_area)
150
    {
151
        $char_id = $char_name_area->find('a', 0)->href;
152
        $char_id = explode('/', $char_id);
153
        return $char_id[4];
154
    }
155
156
    /**
157
     * Get anime/manga character name.
158
     *
159
     * @param \simplehtmldom_1_5\simple_html_dom $char_name_area
160
     *
161
     * @return string
162
     */
163
    static private function getCharacterName($char_name_area)
164
    {
165
        return $char_name_area->find('a', 0)->plaintext;
166
    }
167
168
    /**
169
     * Get anime/manga character role.
170
     *
171
     * @param \simplehtmldom_1_5\simple_html_dom $char_name_area
172
     *
173
     * @return string
174
     */
175
    static private function getCharacterRole($char_name_area)
176
    {
177
        return $char_name_area->find('small', 0)->plaintext;
178
    }
179
180
    /**
181
     * Get anime/manga character va id.
182
     *
183
     * @param \simplehtmldom_1_5\simple_html_dom $va_name_area
184
     *
185
     * @return string
186
     */
187
    static private function getCharacterVaId($va_name_area)
188
    {
189
        $va_id = $va_name_area->find('a', 0)->href;
190
        $va_id = explode('/', $va_id);
191
        return $va_id[4];
192
    }
193
194
    /**
195
     * Get anime/manga character va name.
196
     *
197
     * @param \simplehtmldom_1_5\simple_html_dom $va_name_area
198
     *
199
     * @return string
200
     */
201
    static private function getCharacterVaName($va_name_area)
202
    {
203
        return $va_name_area->find('a', 0)->plaintext;
204
    }
205
206
    /**
207
     * Get anime/manga character va image.
208
     *
209
     * @param \simplehtmldom_1_5\simple_html_dom $each_va
210
     *
211
     * @return string
212
     */
213
    static private function getCharacterVaImage($each_va)
214
    {
215
        $va_image = $each_va->find('td', 1)->find('img', 0)->getAttribute('data-src');
216
        return Helper::imageUrlCleaner($va_image);
217
    }
218
219
    /**
220
     * Get anime/manga character va role.
221
     *
222
     * @param \simplehtmldom_1_5\simple_html_dom $va_name_area
223
     *
224
     * @return string
225
     */
226
    static private function getCharacterVaRole($va_name_area)
227
    {
228
        return $va_name_area->find('small', 0)->plaintext;
229
    }
230
231
    /**
232
     * Get anime/manga staff list.
233
     *
234
     * @return array
235
     */
236
    private function getStaff()
237
    {
238
        $staff = [];
239
        $staff_index = 0;
240
        $staff_table = $this->_parser->find('h2', 1);
241
        if ($staff_table) {
242
            if ($staff_table->next_sibling()->tag == 'table') {
243
                $staff_table = $staff_table->next_sibling();
244
                while (true) {
245
                    $staff_name_area = $staff_table->find('td', 1);
246
247
                    $staff[$staff_index]['image'] = self::getStaffImage($staff_table);
248
                    $staff[$staff_index]['id'] = self::getStaffId($staff_name_area);
249
                    $staff[$staff_index]['name'] = self::getStaffName($staff_name_area);
250
                    $staff[$staff_index]['role'] = self::getStaffRole($staff_name_area);
251
252
                    $staff_table = $staff_table->next_sibling();
253
                    if (!$staff_table)
254
                        break;
255
                    $staff_index++;
256
                }
257
            }
258
        }
259
        return $staff;
260
    }
261
262
    /**
263
     * Get anime/manga staff image.
264
     *
265
     * @param \simplehtmldom_1_5\simple_html_dom $staff_table
266
     *
267
     * @return string
268
     */
269
    static private function getStaffImage($staff_table)
270
    {
271
        $staff_image = $staff_table->find('td .picSurround img', 0)->getAttribute('data-src');
272
        return Helper::imageUrlCleaner($staff_image);
273
    }
274
275
    /**
276
     * Get anime/manga staff id.
277
     *
278
     * @param \simplehtmldom_1_5\simple_html_dom $staff_name_area
279
     *
280
     * @return string
281
     */
282
    static private function getStaffId($staff_name_area)
283
    {
284
        $staff_id = $staff_name_area->find('a', 0)->href;
285
        $staff_id = explode('/', $staff_id);
286
        return $staff_id[4];
287
    }
288
289
    /**
290
     * Get anime/manga staff name.
291
     *
292
     * @param \simplehtmldom_1_5\simple_html_dom $staff_name_area
293
     *
294
     * @return string
295
     */
296
    static private function getStaffName($staff_name_area)
297
    {
298
        return $staff_name_area->find('a', 0)->plaintext;
299
    }
300
301
    /**
302
     * Get anime/manga staff role.
303
     *
304
     * @param \simplehtmldom_1_5\simple_html_dom $staff_name_area
305
     *
306
     * @return string
307
     */
308
    static private function getStaffRole($staff_name_area)
309
    {
310
        return $staff_name_area->find('small', 0)->plaintext;
311
    }
312
313
    /**
314
     * Get anime/manga character + staff complete list.
315
     *
316
     * @return array
317
     */
318
    private function getAllInfo()
319
    {
320
        $data = [
321
            'character' => self::getCharacter(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\Additio...ffModel::getCharacter() 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
            'character' => self::/** @scrutinizer ignore-call */ getCharacter(),
Loading history...
322
            'staff'     => self::getStaff(),
0 ignored issues
show
Bug Best Practice introduced by
The method MalScraper\Model\Additio...rStaffModel::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

322
            'staff'     => self::/** @scrutinizer ignore-call */ getStaff(),
Loading history...
323
        ];
324
325
        return $data;
326
    }
327
}