9
9
*
10
10
*/
11
11
class Countries extends Model {
12
-
13
- /**
14
- * @var string
15
- * Path to the directory containing countries data.
16
- */
17
- protected $ countries ;
18
-
19
- /**
20
- * @var string
21
- * The table for the countries in the database, is "countries" by default.
22
- */
23
- protected $ table ;
24
-
12
+
13
+ /**
14
+ * @var array
15
+ */
16
+ protected $ countries = [];
17
+
18
+ /**
19
+ * @var string
20
+ * The table for the countries in the database, is "countries" by default.
21
+ */
22
+ protected $ table ;
23
+
25
24
/**
26
25
* Constructor.
27
26
*
28
27
* @return void
29
28
*/
30
29
public function __construct ()
31
30
{
32
- $ this ->table = \Config::get ('countries.table_name ' );
31
+ $ this ->table = \Config::get ('countries.table_name ' );
33
32
}
34
-
33
+
35
34
/**
36
35
* Get the countries from the JSON file, if it hasn't already been loaded.
37
36
*
@@ -40,77 +39,77 @@ public function __construct()
40
39
protected function getCountries ()
41
40
{
42
41
//Get the countries from the JSON file
43
- if (! $ this -> countries || sizeof ($ this ->countries ) == 0 ){
42
+ if (sizeof ($ this ->countries ) == 0 ) {
44
43
$ this ->countries = json_decode (file_get_contents (__DIR__ . '/Models/countries.json ' ), true );
45
44
}
46
-
45
+
47
46
//Return the countries
48
47
return $ this ->countries ;
49
48
}
50
-
51
- /**
52
- * Returns one country
53
- *
54
- * @param string $id The country id
49
+
50
+ /**
51
+ * Returns one country
55
52
*
56
- * @return array
57
- */
58
- public function getOne ($ id )
59
- {
53
+ * @param string $id The country id
54
+ *
55
+ * @return array
56
+ */
57
+ public function getOne ($ id )
58
+ {
60
59
$ countries = $ this ->getCountries ();
61
- return $ countries [$ id ];
62
- }
63
-
64
- /**
65
- * Returns a list of countries
66
- *
67
- * @param string sort
68
- *
69
- * @return array
70
- */
71
- public function getList ($ sort = null )
72
- {
73
- //Get the countries list
74
- $ countries = $ this ->getCountries ();
75
-
76
- //Sorting
77
- $ validSorts = [
78
- 'capital ' ,
79
- 'citizenship ' ,
80
- 'country-code ' ,
81
- 'currency ' ,
82
- 'currency_code ' ,
83
- 'currency_sub_unit ' ,
84
- 'full_name ' ,
85
- 'iso_3166_2 ' ,
86
- 'iso_3166_3 ' ,
87
- 'name ' ,
88
- 'region-code ' ,
89
- 'sub-region-code ' ,
90
- 'eea ' ,
91
- 'calling_code ' ,
92
- 'currency_symbol ' ,
93
- 'flag ' ,
60
+ return $ countries [$ id ];
61
+ }
62
+
63
+ /**
64
+ * Returns a list of countries
65
+ *
66
+ * @param string sort
67
+ *
68
+ * @return array
69
+ */
70
+ public function getList ($ sort = null )
71
+ {
72
+ //Get the countries list
73
+ $ countries = $ this ->getCountries ();
74
+
75
+ //Sorting
76
+ $ validSorts = [
77
+ 'capital ' ,
78
+ 'citizenship ' ,
79
+ 'country-code ' ,
80
+ 'currency ' ,
81
+ 'currency_code ' ,
82
+ 'currency_sub_unit ' ,
83
+ 'full_name ' ,
84
+ 'iso_3166_2 ' ,
85
+ 'iso_3166_3 ' ,
86
+ 'name ' ,
87
+ 'region-code ' ,
88
+ 'sub-region-code ' ,
89
+ 'eea ' ,
90
+ 'calling_code ' ,
91
+ 'currency_symbol ' ,
92
+ 'flag ' ,
94
93
];
95
-
96
- if (!is_null ($ sort ) && in_array ($ sort , $ validSorts )){
97
- uasort ($ countries , function ($ a , $ b ) use ($ sort ) {
98
- if (!isset ($ a [$ sort ]) && !isset ($ b [$ sort ])){
99
- return 0 ;
100
- } elseif (!isset ($ a [$ sort ])){
101
- return -1 ;
102
- } elseif (!isset ($ b [$ sort ])){
103
- return 1 ;
104
- } else {
105
- return strcasecmp ($ a [$ sort ], $ b [$ sort ]);
106
- }
107
- });
108
- }
109
-
110
- //Return the countries
111
- return $ countries ;
112
- }
113
-
94
+
95
+ if (!is_null ($ sort ) && in_array ($ sort , $ validSorts )){
96
+ uasort ($ countries , function ($ a , $ b ) use ($ sort ) {
97
+ if (!isset ($ a [$ sort ]) && !isset ($ b [$ sort ])){
98
+ return 0 ;
99
+ } elseif (!isset ($ a [$ sort ])){
100
+ return -1 ;
101
+ } elseif (!isset ($ b [$ sort ])){
102
+ return 1 ;
103
+ } else {
104
+ return strcasecmp ($ a [$ sort ], $ b [$ sort ]);
105
+ }
106
+ });
107
+ }
108
+
109
+ //Return the countries
110
+ return $ countries ;
111
+ }
112
+
114
113
/**
115
114
* Returns a list of countries suitable to use with a select element in Laravelcollective\html
116
115
* Will show the value and sort by the column specified in the display attribute
@@ -124,7 +123,7 @@ public function getListForSelect($display = 'name')
124
123
foreach ($ this ->getList ($ display ) as $ key => $ value ) {
125
124
$ countries [$ key ] = $ value [$ display ];
126
125
}
127
-
126
+
128
127
//return the array
129
128
return $ countries ;
130
129
}
0 commit comments