|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace agoalofalife\database\seeds; |
|
4
|
|
|
use \Illuminate\Database\Capsule\Manager as Capsule; |
|
5
|
|
|
use Illuminate\Database\Seeder; |
|
6
|
|
|
|
|
7
|
|
|
class CitiesTableSeeder extends Seeder |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Return id which will found |
|
11
|
|
|
* @param $regions |
|
12
|
|
|
* @param $what |
|
13
|
|
|
* @return null | int |
|
14
|
|
|
*/ |
|
15
|
|
|
public function findRegionId($regions, $what) |
|
16
|
|
|
{ |
|
17
|
|
|
return isset($regions[trim($what)]) ? $regions[trim($what)] : null; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Auto generated seed file. |
|
22
|
|
|
* |
|
23
|
|
|
* @return void |
|
24
|
|
|
*/ |
|
25
|
|
|
public function run() |
|
26
|
|
|
{ |
|
27
|
|
|
ini_set('memory_limit', '256M'); |
|
28
|
|
|
Capsule::transaction(function () { |
|
29
|
|
|
$response = []; |
|
30
|
|
|
$offset = 0; |
|
31
|
|
|
$arrCities = []; |
|
32
|
|
|
$regions = Capsule::table(config('geography.nameTable.regions'))->get()->pluck('id', 'title')->toArray(); |
|
33
|
|
|
$country = Capsule::table(config('geography.nameTable.country'))->get(); |
|
34
|
|
|
|
|
35
|
|
|
foreach ($country as $concreteCountry) |
|
36
|
|
|
{ |
|
37
|
|
|
do { |
|
38
|
|
|
$body = file_get_contents( |
|
39
|
|
|
'https://api.vk.com/method/database.getCities?v=5.5&country_id=' |
|
40
|
|
|
. $concreteCountry->id |
|
41
|
|
|
. '&need_all=1&count=1000&offset=' |
|
42
|
|
|
. $offset . '&access_token=' . config('geography.access_token')); |
|
43
|
|
|
|
|
44
|
|
|
$responseArray = json_decode($body, true); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($responseArray['response']['items'] as $city) |
|
47
|
|
|
{ |
|
48
|
|
|
$arrCities[] = [ |
|
49
|
|
|
'id' => $city['id'], |
|
50
|
|
|
'title' => $city['title'], |
|
51
|
|
|
'area' => isset($city['area']) ? $city['area'] : '', |
|
52
|
|
|
'region_id' => isset($city['region']) ? $this->findRegionId($regions, $city['region']) : null |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$response = array_merge($response, $responseArray['response']['items']); |
|
57
|
|
|
$offset += 1000; |
|
58
|
|
|
|
|
59
|
|
|
Capsule::table('cities')->insert($arrCities); |
|
60
|
|
|
$arrCities = []; |
|
61
|
|
|
} while( ( count($responseArray['response']['items']) > 0) ); |
|
62
|
|
|
} |
|
63
|
|
|
}); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|