|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Someshwer\WorldCountries\Lib; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Encryption\Encrypter; |
|
6
|
|
|
use Someshwer\WorldCountries\Data\DataRepository; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Author: Someshwer Bandapally |
|
11
|
|
|
* Date: 14-07-2018. |
|
12
|
|
|
* |
|
13
|
|
|
* This class gives all country names |
|
14
|
|
|
* |
|
15
|
|
|
* Class Countries |
|
16
|
|
|
*/ |
|
17
|
|
|
class Countries extends ISOCodes |
|
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
|
|
|
* Countries constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param DataRepository $dataRepository |
|
38
|
|
|
*/ |
|
39
|
39 |
|
public function __construct(DataRepository $dataRepository) |
|
40
|
|
|
{ |
|
41
|
39 |
|
parent::__construct($dataRepository); |
|
42
|
39 |
|
$this->data = $dataRepository; |
|
43
|
39 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Optimizing the country data. |
|
47
|
|
|
* |
|
48
|
|
|
* @param $all_countries_data |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
2 |
|
private function optimizeCountryData($all_countries_data) |
|
53
|
|
|
{ |
|
54
|
2 |
|
$str_length = strlen($all_countries_data) - 4; |
|
55
|
2 |
|
$all_countries_trimmed_data = substr($all_countries_data, 0, 2).substr($all_countries_data, 3, $str_length); |
|
56
|
|
|
// $hash = new Encrypter($this->en_key, Config::get('app.cipher')); |
|
57
|
2 |
|
$hash = new Encrypter($this->en_key, $this->cipher); |
|
58
|
2 |
|
$all_countries = $hash->decrypt($all_countries_trimmed_data); |
|
59
|
|
|
|
|
60
|
2 |
|
return $all_countries; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get all countries. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function countries() |
|
69
|
|
|
{ |
|
70
|
1 |
|
$all_countries_data = $this->data->countries(); |
|
71
|
1 |
|
$all_countries = $this->optimizeCountryData($all_countries_data); |
|
72
|
|
|
$countries = collect($all_countries)->map(function ($item) { |
|
73
|
|
|
// return Str::title(str_replace('-',' ', $item)); |
|
74
|
1 |
|
return Str::studly($item); |
|
75
|
1 |
|
}); |
|
76
|
|
|
|
|
77
|
1 |
|
return ['countries' => $countries]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Search country by search string. |
|
82
|
|
|
* |
|
83
|
|
|
* @param $search_string |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function searchCountry($search_string = null) |
|
88
|
|
|
{ |
|
89
|
1 |
|
$all_countries_data = $this->data->countries(); |
|
90
|
1 |
|
$all_countries = $this->optimizeCountryData($all_countries_data); |
|
91
|
1 |
|
if ($search_string == null) { |
|
92
|
|
|
return array_map(function ($it) { |
|
93
|
|
|
return Str::studly($it); |
|
94
|
|
|
}, $all_countries); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
$countries = array_map(function ($it) { |
|
97
|
1 |
|
return Str::studly($it); |
|
98
|
|
|
}, array_filter($all_countries, function ($item) use ($search_string) { |
|
|
|
|
|
|
99
|
1 |
|
return strpos($item, strtolower($search_string)) === 0; |
|
100
|
1 |
|
})); |
|
101
|
|
|
|
|
102
|
1 |
|
return ['countries' => array_values($countries)]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|