1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Someshwer\WorldCountries\Lib; |
4
|
|
|
|
5
|
|
|
use Illuminate\Encryption\Encrypter; |
|
|
|
|
6
|
|
|
use Someshwer\WorldCountries\Data\DataRepository; |
7
|
|
|
use Someshwer\WorldCountries\Helpers\MyPaginate; |
8
|
|
|
|
9
|
|
|
class Cities |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var DataRepository |
13
|
|
|
*/ |
14
|
|
|
private $data; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $en_key = 'Someshwer1@2#BandapallySomeshwer'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $cipher = 'AES-256-CBC'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* States constructor. |
28
|
|
|
* |
29
|
|
|
* @param DataRepository $dataRepository |
30
|
|
|
*/ |
31
|
|
|
public function __construct(DataRepository $dataRepository) |
32
|
|
|
{ |
33
|
|
|
$this->data = $dataRepository; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Optimize all countries data. |
38
|
|
|
* |
39
|
|
|
* @param $all_countries_data |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
private function optimizeCountriesData($all_countries_data) |
44
|
|
|
{ |
45
|
|
|
$str_length = strlen($all_countries_data) - 4; |
46
|
|
|
$all_countries_trimmed_data = substr($all_countries_data, 0, 2).substr($all_countries_data, 3, $str_length); |
47
|
|
|
$hash = new Encrypter($this->en_key, $this->cipher); |
48
|
|
|
|
49
|
|
|
return $hash->decrypt($all_countries_trimmed_data); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Fetch optimized countries data. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
private function getOptimizedCountriesData() |
58
|
|
|
{ |
59
|
|
|
$all_countries_data = $this->data->countriesHelper(); |
60
|
|
|
$countries_data = $this->optimizeCountriesData($all_countries_data); |
61
|
|
|
// $std_codes_data = json_decode($std_codes_data, true); |
62
|
|
|
return $countries_data; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Optimize states data. |
67
|
|
|
* |
68
|
|
|
* @param $all_states_data |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
private function optimizeStatesData($all_states_data) |
73
|
|
|
{ |
74
|
|
|
$str_length = strlen($all_states_data) - 15; |
75
|
|
|
$all_states_trimmed_data = substr($all_states_data, 0, 14).substr($all_states_data, 15, $str_length); |
76
|
|
|
|
77
|
|
|
return unserialize($all_states_trimmed_data); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Fetch optimized states data. |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
private function getOptimizedStatesData() |
86
|
|
|
{ |
87
|
|
|
$all_states_data = $this->data->states(); |
88
|
|
|
$states_data = $this->optimizeStatesData($all_states_data); |
89
|
|
|
|
90
|
|
|
return $states_data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Optimize cities data. |
95
|
|
|
* |
96
|
|
|
* @param $all_cities_data |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
private function optimizeCitiesData($all_cities_data) |
101
|
|
|
{ |
102
|
|
|
$str_length = strlen($all_cities_data) - 15; |
103
|
|
|
$all_cities_trimmed_data = substr($all_cities_data, 0, 14).substr($all_cities_data, 15, $str_length); |
104
|
|
|
|
105
|
|
|
return unserialize($all_cities_trimmed_data); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Fetch optimized cities data. |
110
|
|
|
* |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
private function getOptimizedCitiesData() |
114
|
|
|
{ |
115
|
|
|
$all_cities_data = $this->data->cities(); |
116
|
|
|
$cities_data = $this->optimizeCitiesData($all_cities_data); |
117
|
|
|
|
118
|
|
|
return $cities_data; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Format cities data. |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
private function formatCitiesData() |
127
|
|
|
{ |
128
|
|
|
$countries = $this->getOptimizedCountriesData(); |
129
|
|
|
$states = $this->getOptimizedStatesData(); |
130
|
|
|
$cities = $this->getOptimizedCitiesData(); |
131
|
|
|
$grouped_states_collection = collect($states)->groupBy('id'); |
|
|
|
|
132
|
|
|
$grouped_countries_collection = collect($countries)->groupBy('id'); |
133
|
|
|
$result = []; |
134
|
|
|
foreach ($cities as $city) { |
135
|
|
|
$state = $grouped_states_collection->get($city['state_id']); |
136
|
|
|
$country = $grouped_countries_collection->get($state[0]['country_id']); |
137
|
|
|
$result[] = [ |
138
|
|
|
'city' => $city['city_name'], |
139
|
|
|
'state' => $state[0]['state_name'], |
140
|
|
|
'country' => $country[0]['country_name'], |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $result; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns all cities. |
149
|
|
|
* |
150
|
|
|
* If pagination is enabled for cities in config file |
151
|
|
|
* then the result contains paginated data otherwise all records wil be returned. |
152
|
|
|
* |
153
|
|
|
* @param null $page_number |
|
|
|
|
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
public function cities($page_number = null) |
158
|
|
|
{ |
159
|
|
|
$page_number = ($page_number == null) ? 1 : $page_number; |
|
|
|
|
160
|
|
|
$cities_data = $this->formatCitiesData(); |
161
|
|
|
if (config('world.pagination.cities') == false) { |
|
|
|
|
162
|
|
|
return $cities_data; |
163
|
|
|
} |
164
|
|
|
$per_page = config('world.pagination.cities_per_page'); |
165
|
|
|
$ceil_val = ceil(count($cities_data) / $per_page); |
166
|
|
|
$request_url = request()->url(); |
|
|
|
|
167
|
|
|
$total_records = count($cities_data); |
168
|
|
|
$pagination_data = MyPaginate::getPagination( |
169
|
|
|
$request_url, |
170
|
|
|
$page_number, |
171
|
|
|
$per_page, |
172
|
|
|
$ceil_val, |
173
|
|
|
$total_records |
174
|
|
|
); |
175
|
|
|
$data = collect($cities_data)->forPage($page_number, $per_page)->values(); |
|
|
|
|
176
|
|
|
$pagination_data['data'] = $data; |
177
|
|
|
|
178
|
|
|
return $pagination_data; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Search cities by any search string. |
183
|
|
|
* |
184
|
|
|
* @param null $search_key |
|
|
|
|
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function searchCities($search_key = null) |
189
|
|
|
{ |
190
|
|
|
if (is_null($search_key)) { |
|
|
|
|
191
|
|
|
return []; |
192
|
|
|
} |
193
|
|
|
$cities = $this->formatCitiesData(); |
194
|
|
|
|
195
|
|
|
return array_values(array_filter($cities, function ($item) use ($search_key) { |
196
|
|
|
return starts_with(strtolower($item['city']), strtolower($search_key)); |
|
|
|
|
197
|
|
|
})); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get all state names if you wish to |
202
|
|
|
* fetch cities by state name. |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
public function statesForCities() |
207
|
|
|
{ |
208
|
|
|
$states = $this->getOptimizedStatesData(); |
209
|
|
|
|
210
|
|
|
return array_map(function ($item) { |
211
|
|
|
return $item['state_name']; |
212
|
|
|
}, $states); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get all country names if you wish to |
217
|
|
|
* fetch cities by country name. |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
public function countriesForCities() |
222
|
|
|
{ |
223
|
|
|
$countries = $this->getOptimizedCountriesData(); |
224
|
|
|
|
225
|
|
|
return array_map(function ($item) { |
226
|
|
|
return $item['country_name']; |
227
|
|
|
}, $countries); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get cities by state name. |
232
|
|
|
* |
233
|
|
|
* @param null $state_name |
|
|
|
|
234
|
|
|
* |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
|
|
public function getCitiesByStateName($state_name = null) |
238
|
|
|
{ |
239
|
|
|
if (is_null($state_name)) { |
|
|
|
|
240
|
|
|
return []; |
241
|
|
|
} |
242
|
|
|
$cities = $this->formatCitiesData(); |
243
|
|
|
|
244
|
|
|
return array_values(array_filter($cities, function ($item) use ($state_name) { |
245
|
|
|
return strtolower($item['state']) == strtolower($state_name); |
246
|
|
|
})); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get cities by country name. |
251
|
|
|
* |
252
|
|
|
* @param null $country_name |
|
|
|
|
253
|
|
|
* |
254
|
|
|
* @return array |
255
|
|
|
*/ |
256
|
|
|
public function getCitiesByCountryName($country_name = null) |
257
|
|
|
{ |
258
|
|
|
if (is_null($country_name)) { |
|
|
|
|
259
|
|
|
return []; |
260
|
|
|
} |
261
|
|
|
$cities = $this->formatCitiesData(); |
262
|
|
|
|
263
|
|
|
return array_values(array_filter($cities, function ($item) use ($country_name) { |
264
|
|
|
return strtolower($item['country']) == strtolower($country_name); |
265
|
|
|
})); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths