Issues (118)

src/Lib/ISOCodes.php (24 issues)

1
<?php
2
3
namespace Someshwer\WorldCountries\Lib;
4
5
use Illuminate\Encryption\Encrypter;
0 ignored issues
show
The type Illuminate\Encryption\Encrypter was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Someshwer\WorldCountries\Data\DataRepository;
7
use Someshwer\WorldCountries\Helpers\MyPaginate;
8
9
/**
10
 * Author: Someshwer Bandapally
11
 * Date: 14-07-2018.
12
 *
13
 * This class provides ISO codes data
14
 *
15
 * Class ISOCodes
16
 */
17
class ISOCodes extends TimeZones
18
{
19
    /**
20
     * @var DataRepository
21
     */
22
    private $data;
23
24
    /**
25
     * @var string
26
     */
27
    private $en_key = 'Someshwer1@2#BandapallySomeshwer';
28
29
    /**
30
     * @var string
31
     */
32
    private $cipher = 'AES-256-CBC';
33
34
    /**
35
     * ISOCodes constructor.
36
     *
37
     * @param DataRepository $dataRepository
38
     */
39
    public function __construct(DataRepository $dataRepository)
40
    {
41
        parent::__construct($dataRepository);
42
        $this->data = $dataRepository;
43
    }
44
45
    /**
46
     * Optimize ISO data.
47
     *
48
     * @param $all_iso_data
49
     *
50
     * @return string
51
     */
52
    private function optimizeISOData($all_iso_data)
53
    {
54
        $str_length = strlen($all_iso_data) - 4;
55
        $all_iso_trimmed_data = substr($all_iso_data, 0, 2).substr($all_iso_data, 3, $str_length);
56
        // $hash = new Encrypter($this->en_key, Config::get('app.cipher'));
57
        $hash = new Encrypter($this->en_key, $this->cipher);
58
        $all_iso = $hash->decrypt($all_iso_trimmed_data);
59
60
        return $all_iso;
61
    }
62
63
    /**
64
     * Get optimized ISO data.
65
     *
66
     * @return mixed
67
     */
68
    private function getOptimizedIsoData()
69
    {
70
        $all_countries_iso_data = $this->data->countriesISOData();
71
        $iso_data = $this->optimizeISOData($all_countries_iso_data);
72
73
        return $iso_codes = json_decode($iso_data, true);
0 ignored issues
show
The assignment to $iso_codes is dead and can be removed.
Loading history...
74
    }
75
76
    /**
77
     * Optimize ISO result.
78
     *
79
     * @param $iso_codes
80
     * @param $alpha_code
81
     *
82
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
0 ignored issues
show
The type Illuminate\Contracts\Routing\ResponseFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The type Symfony\Component\HttpFoundation\Response was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
83
     */
84
    private function optimizeIsoResult($iso_codes, $alpha_code)
85
    {
86
        $group_by = ctype_digit($alpha_code) ? 'country_numeric_code' : ((strlen($alpha_code) == 2) ? 'alpha_2' : 'alpha_3');
87
        $alpha_code = ctype_digit($alpha_code) ? (int) $alpha_code : strtoupper($alpha_code);
88
        $result = collect($iso_codes)->groupBy($group_by)->get($alpha_code);
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        $result = /** @scrutinizer ignore-call */ collect($iso_codes)->groupBy($group_by)->get($alpha_code);
Loading history...
89
        if ($result == null) {
90
            return $this->isoErrorResponse();
91
        } else {
92
            return response(['success' => true, 'iso_info' => $result->first()], 200);
0 ignored issues
show
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
            return /** @scrutinizer ignore-call */ response(['success' => true, 'iso_info' => $result->first()], 200);
Loading history...
93
        }
94
    }
95
96
    /**
97
     * Format regions.
98
     *
99
     * @param $iso_codes
100
     *
101
     * @return array
102
     */
103
    private function formatRegions($iso_codes)
104
    {
105
        $regions = collect($iso_codes)->groupBy('region')->all();
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        $regions = /** @scrutinizer ignore-call */ collect($iso_codes)->groupBy('region')->all();
Loading history...
106
        $new_regions = [];
107
        foreach ($regions as $key => $region) {
108
            if ($key == '') {
109
                $key = $region[0]['name'];
110
            }
111
            $new_regions[$key] = $region;
112
        }
113
114
        return $new_regions;
115
    }
116
117
    /**
118
     * Filter regions.
119
     *
120
     * @param $regions
121
     * @param $region
122
     *
123
     * @return static
124
     */
125
    private function filterRegions($regions, $region)
126
    {
127
        return collect($regions)->filter(function ($item, $key) use ($region) {
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
        return /** @scrutinizer ignore-call */ collect($regions)->filter(function ($item, $key) use ($region) {
Loading history...
128
            return ($region == null) ? true : (strpos(strtolower($key), strtolower($region)) === 0) ? true : false;
129
        });
130
    }
131
132
    /**
133
     * Get ISO codes. This method returns all ISO codes data.
134
     *
135
     * This method also supports pagination.
136
     * If pagination is enabled for states in config file
137
     * then the result contains paginated data otherwise all records
138
     * wil be directly returned.
139
     *
140
     * @param null $page_number
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $page_number is correct as it would always require null to be passed?
Loading history...
141
     *
142
     * @return mixed
143
     */
144
    public function isoCodes($page_number = null)
145
    {
146
        $page_number = ($page_number == null) ? 1 : $page_number;
0 ignored issues
show
The condition $page_number == null is always true.
Loading history...
147
        $iso_data = $this->getOptimizedIsoData();
148
        if (config('world.pagination.iso_codes') == false) {
0 ignored issues
show
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
        if (/** @scrutinizer ignore-call */ config('world.pagination.iso_codes') == false) {
Loading history...
149
            return $iso_data;
150
        }
151
        $per_page = config('world.pagination.iso_per_page');
152
        $ceil_val = ceil(count($iso_data) / $per_page);
153
        $request_url = request()->url();
0 ignored issues
show
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        $request_url = /** @scrutinizer ignore-call */ request()->url();
Loading history...
154
        $total_records = count($iso_data);
155
        $pagination_data = MyPaginate::getPagination(
156
            $request_url,
157
            $page_number,
158
            $per_page,
159
            $ceil_val,
160
            $total_records
161
        );
162
        $data = collect($iso_data)->forPage($page_number, $per_page)->values();
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
        $data = /** @scrutinizer ignore-call */ collect($iso_data)->forPage($page_number, $per_page)->values();
Loading history...
163
        $pagination_data['data'] = $data;
164
165
        return $pagination_data;
166
    }
167
168
    /**
169
     * Get regions.
170
     *
171
     * @return static
172
     */
173
    public function regions()
174
    {
175
        $iso_codes = $this->getOptimizedIsoData();
176
        $regions_data = $this->formatRegions($iso_codes);
177
        $regions = array_keys($regions_data);
178
179
        return collect($regions)->map(function ($item) {
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
        return /** @scrutinizer ignore-call */ collect($regions)->map(function ($item) {
Loading history...
180
            return ['key' => strtolower($item), 'name' => $item];
181
        });
182
    }
183
184
    /**
185
     * Search ISO codes by country name,
186
     * or by iso code, or by numeric country code.
187
     *
188
     * @param $key
189
     *
190
     * @return array
191
     */
192
    public function searchIsoCodes($key = null)
193
    {
194
        if ($key == null) {
195
            return [];
196
        }
197
        $iso_data = $this->getOptimizedIsoData();
198
199
        return array_values(array_filter($iso_data, function ($item) use ($key) {
200
            return str_contains(strtolower($item['name']), strtolower($key)) ||
0 ignored issues
show
The function str_contains was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
            return /** @scrutinizer ignore-call */ str_contains(strtolower($item['name']), strtolower($key)) ||
Loading history...
201
                str_contains(strtolower($item['alpha_2']), strtolower($key)) ||
202
                str_contains(strtolower($item['alpha_3']), strtolower($key)) ||
203
                str_contains(strtolower($item['country_numeric_code']), strtolower($key));
204
        }));
205
    }
206
207
    /**
208
     * Filter ISO info by country name.
209
     *
210
     * @param $iso_codes
211
     * @param $name
212
     *
213
     * @return static
214
     */
215
    private function filterIsoInfoByCountryName($iso_codes, $name)
216
    {
217
        return collect($iso_codes)->map(function ($item, $key) {
0 ignored issues
show
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

217
        return /** @scrutinizer ignore-call */ collect($iso_codes)->map(function ($item, $key) {
Loading history...
218
            $item['display_name'] = $item['name'];
219
            $item['name'] = strtolower(studly_case($item['name']));
0 ignored issues
show
The function studly_case was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

219
            $item['name'] = strtolower(/** @scrutinizer ignore-call */ studly_case($item['name']));
Loading history...
220
221
            return $item;
222
        })->groupBy('name')->filter(function ($item, $key) use ($name) {
223
            return strpos($key, $name) === 0;
224
        })->collapse();
225
    }
226
227
    /**
228
     * Get ISO info by country name.
229
     *
230
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
231
     *
232
     * @return array|ISOCodes
233
     */
234
    public function isoInfoByCountryName($name = null)
235
    {
236
        if ($name == null) {
0 ignored issues
show
The condition $name == null is always true.
Loading history...
237
            return [];
238
        }
239
        $iso_codes = $this->getOptimizedIsoData();
240
        $result = $this->filterIsoInfoByCountryName($iso_codes, $name);
241
        if ($result == null) {
242
            return [];
243
        }
244
245
        return $result;
246
    }
247
248
    /**
249
     * Get ISO info by code.
250
     *
251
     * @param null $code
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $code is correct as it would always require null to be passed?
Loading history...
252
     *
253
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
254
     */
255
    public function isoInfoByCode($code = null)
256
    {
257
        if ($code == null) {
0 ignored issues
show
The condition $code == null is always true.
Loading history...
258
            return $this->isoErrorResponse('NO_PARAM');
259
        }
260
        $iso_codes = $this->getOptimizedIsoData();
261
262
        return $this->optimizeIsoResult($iso_codes, $code);
263
    }
264
265
    /**
266
     * ISO codes by region.
267
     *
268
     * @param null $region
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $region is correct as it would always require null to be passed?
Loading history...
269
     *
270
     * @return ISOCodes
271
     */
272
    public function isoCodesByRegion($region = null)
273
    {
274
        $iso_codes = $this->getOptimizedIsoData();
275
        $regions = $this->formatRegions($iso_codes);
276
277
        return $this->filterRegions($regions, $region);
278
    }
279
280
    /**
281
     * Format ISO error response.
282
     *
283
     * @param null $param
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $param is correct as it would always require null to be passed?
Loading history...
284
     *
285
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
286
     */
287
    private function isoErrorResponse($param = null)
288
    {
289
        $status = 'INVALID_CODE';
290
        $message = 'Invalid ISO code!';
291
        if ($param == 'NO_PARAM') {
292
            $status = 'CODE_REQUIRED';
293
            $message = 'ISO code parameter is required';
294
        }
295
296
        return response(['error' => true, 'status' => $status, 'message' => $message], 422);
0 ignored issues
show
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

296
        return /** @scrutinizer ignore-call */ response(['error' => true, 'status' => $status, 'message' => $message], 422);
Loading history...
297
    }
298
}
299