1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StarCitizen; |
4
|
|
|
|
5
|
|
|
use StarCitizen\Models\Model; |
6
|
|
|
use StarCitizen\Client\StarCitizensClient; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class StarCitizens |
10
|
|
|
* @package StarCitizen |
11
|
|
|
* |
12
|
|
|
* @method accounts($id, $profileType = false, $cache = false, $raw = false) Get accounts system results |
13
|
|
|
* @method organisations($id, $profileType = false, $cache = false, $raw = false) Get org system results |
14
|
|
|
*/ |
15
|
|
|
final class StarCitizens |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var bool|StarCitizensClient |
19
|
|
|
*/ |
20
|
|
|
private static $client = false; |
21
|
|
|
|
22
|
|
|
private $systems = [ |
23
|
|
|
"accounts" => [ |
24
|
|
|
"base_action" => "full_profile", |
25
|
|
|
"actions" => [ |
26
|
|
|
"full_profile" => '\Profile', |
27
|
|
|
"threads" => ['\Thread', '', 'thread_id'], |
28
|
|
|
"posts" => ['\Post', 'post', 'post_id'], |
29
|
|
|
] |
30
|
|
|
] |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* StarCitizens constructor. |
35
|
|
|
*/ |
36
|
4 |
|
public function __construct() |
37
|
|
|
{ |
38
|
4 |
|
static::setupClient(); |
|
|
|
|
39
|
4 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $name |
43
|
|
|
* @param $arguments |
44
|
|
|
* @return bool|mixed |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
4 |
|
public function __call($name, $arguments) |
48
|
|
|
{ |
49
|
4 |
|
if (array_key_exists($name, $this->systems)) { |
50
|
3 |
|
$this->doCall($name, $arguments); |
51
|
3 |
|
} |
52
|
|
|
|
53
|
4 |
|
throw new \Exception("Method not found"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $name |
58
|
|
|
* @param array $arguments |
59
|
|
|
* @return bool|mixed |
60
|
|
|
*/ |
61
|
3 |
|
private function doCall($name, array $arguments) |
62
|
|
|
{ |
63
|
3 |
|
$argumentCount = count($arguments); |
64
|
|
|
|
65
|
3 |
|
if ($argumentCount == 0) |
66
|
3 |
|
throw new \InvalidArgumentException("Requires an argument"); |
67
|
|
|
|
68
|
3 |
|
if ($argumentCount > 0 && $argumentCount< 2) |
69
|
3 |
|
return $this->find($arguments[0], $name, $this->systems[$name]['base_action']); |
70
|
|
|
|
71
|
1 |
|
if ($argumentCount == 2) { |
72
|
1 |
|
list($id, $profileType) = $arguments; |
73
|
1 |
|
if ($profileType == false) |
74
|
1 |
|
$profileType = $this->systems[$name]['base_action']; |
75
|
1 |
|
return $this->find($id, $name, $profileType); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($argumentCount == 4 ) { |
79
|
|
|
list($id, $profileType, $cache, $raw) = $arguments; |
80
|
|
|
return $this->find($id, $name, $profileType, $cache, $raw); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new \InvalidArgumentException("Invalid arguments"); |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $name |
89
|
|
|
* @param $arguments |
90
|
|
|
* @return bool|mixed |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
1 |
|
public static function __callStatic($name, $arguments) |
94
|
|
|
{ |
95
|
1 |
|
$starCitizens = new StarCitizens(); |
96
|
1 |
|
return $starCitizens->__call($name, $arguments); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Find an entity |
102
|
|
|
* |
103
|
|
|
* @param $id |
104
|
|
|
* @param $system |
105
|
|
|
* @param $profileType |
106
|
|
|
* @param bool $cache |
107
|
|
|
* @param bool $raw |
108
|
|
|
* @return bool|mixed |
109
|
|
|
*/ |
110
|
3 |
|
private function find($id, $system, $profileType, $cache = false, $raw = false) |
111
|
|
|
{ |
112
|
3 |
|
$response = json_decode( |
113
|
|
|
self::$client |
114
|
3 |
|
->getResult( |
115
|
3 |
|
$this->getParams($id, $system, $profileType, $cache) |
116
|
3 |
|
) |
117
|
3 |
|
->getBody() |
118
|
3 |
|
->getContents(), |
119
|
|
|
true |
120
|
3 |
|
); |
121
|
|
|
|
122
|
3 |
|
return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $id |
127
|
|
|
* @param $system |
128
|
|
|
* @param $profileType |
129
|
|
|
* @param $cache |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
3 |
|
private function getParams($id, $system, $profileType, $cache) |
133
|
|
|
{ |
134
|
3 |
|
$cache = ($cache === true) ? "cache" : "live"; |
135
|
|
|
|
136
|
|
|
return [ |
137
|
3 |
|
'api_source' => $cache, |
138
|
3 |
|
'system' => $system, |
139
|
3 |
|
'action' => $profileType, |
140
|
3 |
|
'target_id' => $id, |
141
|
3 |
|
'expedite' => '0', |
142
|
3 |
|
'format' => 'json', |
143
|
3 |
|
'start_page' => '1', |
144
|
|
|
'end_page' => '5' |
145
|
3 |
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $response |
150
|
|
|
* @param $profileType |
151
|
|
|
* @param $raw |
152
|
|
|
* |
153
|
|
|
* @return bool|mixed |
154
|
|
|
*/ |
155
|
3 |
|
private function checkResponse($response, $profileType, $raw) |
156
|
|
|
{ |
157
|
3 |
|
if ($response['request_stats']['query_status'] == "success") { |
158
|
3 |
|
if ($raw === true) { |
159
|
|
|
return $response; |
160
|
|
|
} else { |
161
|
3 |
|
return $this->fillModel($profileType, $response['data']); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Setup the client |
170
|
|
|
*/ |
171
|
4 |
|
private static function setupClient() |
172
|
|
|
{ |
173
|
4 |
|
if (static::$client === false) { |
|
|
|
|
174
|
1 |
|
static::$client = new StarCitizensClient(); |
|
|
|
|
175
|
1 |
|
} |
176
|
4 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Fills our model in with the provided data |
180
|
|
|
* |
181
|
|
|
* @param $model |
182
|
|
|
* @param $fillData |
183
|
|
|
* |
184
|
|
|
* @return Model |
185
|
|
|
*/ |
186
|
3 |
|
private function fillModel($model, $fillData) |
187
|
|
|
{ |
188
|
3 |
|
if (is_array($model)) { |
189
|
1 |
|
list($className, $dataRoot, $idName) =$model; |
190
|
1 |
|
$object = new \ReflectionClass('StarCitizen\Models\Store'); |
191
|
1 |
|
return $object->newInstance($fillData, $className, $dataRoot, $idName); |
192
|
|
|
} else { |
193
|
2 |
|
$object = new \ReflectionClass('StarCitizen\Models' . $model); |
194
|
2 |
|
return $object->newInstance($fillData); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.