|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class CoC_Location |
|
4
|
|
|
{ |
|
5
|
|
|
protected $api; |
|
6
|
|
|
protected $locationId; |
|
7
|
|
|
protected $location = NULL; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Constructor of CoC_Location |
|
11
|
|
|
* |
|
12
|
|
|
* @param $locationId |
|
13
|
|
|
*/ |
|
14
|
|
|
public function __construct($location) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->api = new ClashOfClans(); |
|
17
|
|
|
if(is_object($location)) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->location = $location; |
|
20
|
|
|
} |
|
21
|
|
|
else |
|
22
|
|
|
{ |
|
23
|
|
|
$this->locationId = $location; |
|
24
|
|
|
$this->getLocation(); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Gets an stdClass containing location-information |
|
30
|
|
|
* |
|
31
|
|
|
* @return stdClass, location |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function getLocation() |
|
34
|
|
|
{ |
|
35
|
|
|
if($this->location == NULL) |
|
36
|
|
|
{ |
|
37
|
|
|
foreach ($this->api->getLocationList()->items as $location) |
|
38
|
|
|
{ |
|
39
|
|
|
if ($location->id == $this->locationId) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->location = $location; |
|
42
|
|
|
return $this->location; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
return 0; |
|
46
|
|
|
} |
|
47
|
|
|
else |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->location; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Sets the location by providing it's exact name |
|
55
|
|
|
* |
|
56
|
|
|
* @param string, location name |
|
57
|
|
|
* @return bool, success or fail |
|
58
|
|
|
*/ |
|
59
|
|
View Code Duplication |
public function setLocationByName($name) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($this->api->getLocationList()->items as $location) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($location->name == $name) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->locationId = $location->id; |
|
66
|
|
|
$this->location = $location; |
|
67
|
|
|
return 1; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
return 0; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Sets the location by providing it's country code |
|
75
|
|
|
* |
|
76
|
|
|
* @param string |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
View Code Duplication |
public function setLocationByCode($cc) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
foreach ($this->api->getLocationList()->items as $location) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($location->countryCode == $cc) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->locationId = $location->id; |
|
86
|
|
|
$this->location = $location; |
|
87
|
|
|
return 1; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return 0; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Gets the location ID |
|
95
|
|
|
* |
|
96
|
|
|
* @return int |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getLocationId() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->getLocation()->id; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Gets the location name |
|
105
|
|
|
* |
|
106
|
|
|
* @return string, name |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getLocationName() |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->getLocation()->name; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Check whether the given location is a country or a region |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public function isCountry() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->getLocation()->isCountry; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Gets the country code for the given location |
|
125
|
|
|
* |
|
126
|
|
|
* @return string, country code |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getCountryCode() |
|
129
|
|
|
{ |
|
130
|
|
|
if($this->isCountry()) |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->getLocation()->countryCode; |
|
133
|
|
|
} |
|
134
|
|
|
else |
|
135
|
|
|
{ |
|
136
|
|
|
return "nA"; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
?> |
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.