1
|
|
|
<?php |
2
|
|
|
namespace Bantenprov\WilayahIndonesia\Http\Controllers; |
3
|
|
|
|
4
|
|
|
/* Require */ |
5
|
|
|
use App\Http\Controllers\Controller; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
/* Models */ |
9
|
|
|
use Laravolt\Indonesia\Indonesia; |
10
|
|
|
use Laravolt\Indonesia\Models\Province; |
11
|
|
|
use Laravolt\Indonesia\Models\City; |
12
|
|
|
use Laravolt\Indonesia\Models\District; |
13
|
|
|
use Laravolt\Indonesia\Models\Village; |
14
|
|
|
|
15
|
|
|
/* Etc */ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The DistrictController class. |
19
|
|
|
* |
20
|
|
|
* @package Bantenprov\WilayahIndonesia |
21
|
|
|
* @author bantenprov <[email protected]> |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
class DistrictController extends Controller |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
protected $indonesia; |
26
|
|
|
protected $province; |
27
|
|
|
protected $city; |
28
|
|
|
protected $district; |
29
|
|
|
protected $village; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new controller instance. |
33
|
|
|
* |
34
|
|
|
* @return void |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->indonesia = new Indonesia; |
39
|
|
|
$this->province = new Province; |
40
|
|
|
$this->city = new City; |
41
|
|
|
$this->district = new District; |
42
|
|
|
$this->village = new Village; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Display a listing of the resource. |
47
|
|
|
* |
48
|
|
|
* @return \Illuminate\Http\Response |
49
|
|
|
*/ |
50
|
|
|
public function get() |
51
|
|
|
{ |
52
|
|
|
$districts = $this->indonesia->allDistricts(); |
53
|
|
|
|
54
|
|
|
foreach($districts as $district){ |
55
|
|
|
array_set($district, 'label', $district->name); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$response['districts'] = $districts; |
|
|
|
|
59
|
|
|
$response['message'] = 'Success'; |
60
|
|
|
$response['error'] = false; |
61
|
|
|
$response['status'] = true; |
62
|
|
|
|
63
|
|
|
return response()->json($response); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Display a listing of the resource. |
68
|
|
|
* |
69
|
|
|
* @return \Illuminate\Http\Response |
70
|
|
|
*/ |
71
|
|
|
public function getByCity($id) |
72
|
|
|
{ |
73
|
|
|
$districts = $this->indonesia->findCity($id, ['districts']); |
74
|
|
|
|
75
|
|
|
if (isset($districts->districts)) { |
76
|
|
|
$districts = $districts->districts; |
77
|
|
|
} else { |
78
|
|
|
$districts = $this->district->getAttributes(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach($districts as $district){ |
82
|
|
|
array_set($district, 'label', $district->name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$response['districts'] = $districts; |
|
|
|
|
86
|
|
|
$response['message'] = 'Success'; |
87
|
|
|
$response['error'] = false; |
88
|
|
|
$response['status'] = true; |
89
|
|
|
|
90
|
|
|
return response()->json($response); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Display the specified resource. |
95
|
|
|
* |
96
|
|
|
* @param \App\District $district |
|
|
|
|
97
|
|
|
* @return \Illuminate\Http\Response |
98
|
|
|
*/ |
99
|
|
|
public function show($id) |
100
|
|
|
{ |
101
|
|
|
$district = $this->indonesia->findDistrict($id); |
102
|
|
|
|
103
|
|
|
array_set($district, 'label', $district->name); |
104
|
|
|
|
105
|
|
|
$response['district'] = $district; |
|
|
|
|
106
|
|
|
$response['message'] = 'Success'; |
107
|
|
|
$response['error'] = false; |
108
|
|
|
$response['status'] = true; |
109
|
|
|
|
110
|
|
|
return response()->json($response); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.