|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Ne-Lexa |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* |
|
9
|
|
|
* @see https://github.com/Ne-Lexa/google-play-scraper |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Nelexa\GPlay\Scraper; |
|
13
|
|
|
|
|
14
|
|
|
use Nelexa\GPlay\Model\GoogleImage; |
|
15
|
|
|
use Nelexa\GPlay\Model\Permission; |
|
16
|
|
|
use Nelexa\HttpClient\ResponseHandlerInterface; |
|
17
|
|
|
use Psr\Http\Message\RequestInterface; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @internal |
|
22
|
|
|
*/ |
|
23
|
|
|
class PermissionScraper implements ResponseHandlerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param RequestInterface $request |
|
27
|
|
|
* @param ResponseInterface $response |
|
28
|
|
|
* |
|
29
|
|
|
* @return Permission[] |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function __invoke(RequestInterface $request, ResponseInterface $response) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$contents = substr($response->getBody()->getContents(), 5); |
|
34
|
1 |
|
$json = \GuzzleHttp\json_decode($contents, true); |
|
35
|
1 |
|
$data = \GuzzleHttp\json_decode($json[0][2], true); |
|
36
|
|
|
|
|
37
|
|
|
$permissionMapFn = static function (array $v): string { |
|
38
|
1 |
|
return (string) $v[1]; |
|
39
|
1 |
|
}; |
|
40
|
|
|
|
|
41
|
1 |
|
$permissions = []; |
|
42
|
|
|
|
|
43
|
1 |
|
foreach (\array_slice($data, 0, 2) as $items) { |
|
44
|
1 |
|
if ($items === null) { |
|
45
|
|
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
foreach ($items as $values) { |
|
49
|
1 |
|
$permissionName = $values[0]; |
|
50
|
1 |
|
$permissions[$permissionName] = [ |
|
51
|
1 |
|
'name' => $permissionName, |
|
52
|
1 |
|
'icon' => new GoogleImage($values[1][3][2]), |
|
53
|
1 |
|
'permissions' => array_map($permissionMapFn, $values[2]), |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
if (isset($data[2])) { |
|
59
|
1 |
|
end($permissions); |
|
60
|
1 |
|
$lastKey = key($permissions); |
|
61
|
1 |
|
$permissions[$lastKey]['permissions'] = array_merge( |
|
62
|
1 |
|
array_map($permissionMapFn, $data[2]), |
|
63
|
1 |
|
$permissions[$lastKey]['permissions'] |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return array_map( |
|
68
|
|
|
static function (array $data) { |
|
69
|
1 |
|
return new Permission($data['name'], $data['icon'], $data['permissions']); |
|
70
|
1 |
|
}, |
|
71
|
1 |
|
$permissions |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|