1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (c) Ne-Lexa |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @see https://github.com/Ne-Lexa/google-play-scraper |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Nelexa\GPlay\Scraper; |
15
|
|
|
|
16
|
|
|
use Nelexa\GPlay\HttpClient\ParseHandlerInterface; |
17
|
|
|
use Nelexa\GPlay\Model\GoogleImage; |
18
|
|
|
use Nelexa\GPlay\Model\Permission; |
19
|
|
|
use Psr\Http\Message\RequestInterface; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @internal |
24
|
|
|
*/ |
25
|
|
|
class PermissionScraper implements ParseHandlerInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param RequestInterface $request |
29
|
|
|
* @param ResponseInterface $response |
30
|
|
|
* @param array $options |
31
|
|
|
* |
32
|
|
|
* @return Permission[] |
33
|
|
|
*/ |
34
|
1 |
|
public function __invoke(RequestInterface $request, ResponseInterface $response, array &$options = []): array |
35
|
|
|
{ |
36
|
1 |
|
$contents = substr($response->getBody()->getContents(), 5); |
37
|
1 |
|
$json = \GuzzleHttp\json_decode($contents, true); |
|
|
|
|
38
|
1 |
|
$data = \GuzzleHttp\json_decode($json[0][2], true); |
|
|
|
|
39
|
|
|
|
40
|
1 |
|
$permissionMapFn = static function (array $v): string { |
41
|
1 |
|
return (string) $v[1]; |
42
|
|
|
}; |
43
|
|
|
|
44
|
1 |
|
$permissions = []; |
45
|
|
|
|
46
|
1 |
|
foreach (\array_slice($data, 0, 2) as $items) { |
47
|
1 |
|
if ($items === null) { |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
foreach ($items as $values) { |
52
|
1 |
|
if (empty($values)) { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
1 |
|
$permissionName = $values[0]; |
56
|
1 |
|
$permissions[$permissionName] = [ |
57
|
|
|
'name' => $permissionName, |
58
|
1 |
|
'icon' => new GoogleImage($values[1][3][2]), |
59
|
1 |
|
'permissions' => array_map($permissionMapFn, $values[2]), |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
if (isset($data[2])) { |
65
|
1 |
|
end($permissions); |
66
|
1 |
|
$lastKey = key($permissions); |
67
|
1 |
|
$permissions[$lastKey]['permissions'] = array_merge( |
68
|
1 |
|
array_map($permissionMapFn, $data[2]), |
69
|
1 |
|
$permissions[$lastKey]['permissions'] |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return array_map( |
74
|
1 |
|
static function (array $data) { |
75
|
1 |
|
return new Permission($data['name'], $data['icon'], $data['permissions']); |
76
|
|
|
}, |
77
|
|
|
$permissions |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.