-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathWorld.php
56 lines (48 loc) · 1.3 KB
/
World.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace Khsing\World;
use Khsing\World\Models\Continent;
use Khsing\World\Models\Country;
use Khsing\World\Models\Division;
/**
* World
*/
class World
{
public static function Continents()
{
return Continent::orderBy('name', 'asc')->get();
}
public static function Countries()
{
return Country::orderBy('name', 'asc')->get();
}
public static function getContinentByCode($code)
{
return Continent::getByCode($code);
}
public static function getCountryByCode($code)
{
return Country::getByCode($code);
}
public static function getByCode($code)
{
$code = strtolower($code);
if (strpos($code, '-')) {
list($country_code, $code) = explode('-', $code);
$country = self::getCountryByCode($country_code);
} else {
return self::getCountryByCode($code);
}
if ($country->has_division) {
return Division::where([
['country_id', $country->id],
['code', $code],
])->first();
}
return City::where([
['country_id', $country->id],
['code', $code],
])->first();
throw new \Khsing\World\Exceptions\InvalidCodeException("Code is invalid");
}
}