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 |
|
self::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
|
|
|
} |
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 && $argumentCount< 2) |
66
|
2 |
|
return $this->callBase($arguments[0], $name); |
67
|
|
|
|
68
|
1 |
|
if ($argumentCount == 2) { |
69
|
1 |
|
list($id, $action) = $arguments; |
70
|
1 |
|
return $this->callAction($id, $name, $action); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($argumentCount == 4) { |
74
|
|
|
list($id, $action, $cache, $raw) = $arguments; |
75
|
|
|
return $this->find($id, $name, $action, $cache, $raw); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
throw new \InvalidArgumentException("Invalid arguments"); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $id |
83
|
|
|
* @param $system |
84
|
|
|
* @return bool|mixed |
85
|
|
|
*/ |
86
|
2 |
|
private function callBase($id, $system) |
87
|
|
|
{ |
88
|
2 |
|
return $this->find($id, $system, $this->systems[$system]['base_action']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $id |
93
|
|
|
* @param $system |
94
|
|
|
* @param bool $action |
95
|
|
|
* @return bool|mixed |
96
|
|
|
*/ |
97
|
1 |
|
private function callAction($id, $system, $action = false) |
98
|
|
|
{ |
99
|
1 |
|
if ($action === false) |
100
|
|
|
$action = $this->systems[$system]['base_action']; |
101
|
1 |
|
return $this->find($id, $system, $action); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $name |
106
|
|
|
* @param $arguments |
107
|
|
|
* @return bool|mixed |
108
|
|
|
* @throws \Exception |
109
|
|
|
*/ |
110
|
1 |
|
public static function __callStatic($name, $arguments) |
111
|
|
|
{ |
112
|
1 |
|
$starCitizens = new StarCitizens(); |
113
|
1 |
|
return $starCitizens->__call($name, $arguments); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Find an entity |
119
|
|
|
* |
120
|
|
|
* @param $id |
121
|
|
|
* @param $system |
122
|
|
|
* @param $profileType |
123
|
|
|
* @param bool $cache |
124
|
|
|
* @param bool $raw |
125
|
|
|
* @return bool|mixed |
126
|
|
|
*/ |
127
|
3 |
|
private function find($id, $system, $profileType, $cache = false, $raw = false) |
128
|
|
|
{ |
129
|
3 |
|
$response = json_decode( |
130
|
3 |
|
self::$client |
131
|
3 |
|
->getResult( |
132
|
3 |
|
$this->getParams($id, $system, $profileType, $cache) |
133
|
|
|
) |
134
|
3 |
|
->getBody() |
135
|
3 |
|
->getContents(), |
136
|
3 |
|
true |
137
|
|
|
); |
138
|
|
|
|
139
|
3 |
|
return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $id |
144
|
|
|
* @param $system |
145
|
|
|
* @param $profileType |
146
|
|
|
* @param $cache |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
3 |
|
private function getParams($id, $system, $profileType, $cache) |
150
|
|
|
{ |
151
|
3 |
|
$cache = ($cache === true) ? "cache" : "live"; |
152
|
|
|
|
153
|
|
|
return [ |
154
|
3 |
|
'api_source' => $cache, |
155
|
3 |
|
'system' => $system, |
156
|
3 |
|
'action' => $profileType, |
157
|
3 |
|
'target_id' => $id, |
158
|
3 |
|
'expedite' => '0', |
159
|
3 |
|
'format' => 'json', |
160
|
3 |
|
'start_page' => '1', |
161
|
3 |
|
'end_page' => '5' |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $response |
167
|
|
|
* @param $profileType |
168
|
|
|
* @param $raw |
169
|
|
|
* |
170
|
|
|
* @return bool|mixed |
171
|
|
|
*/ |
172
|
3 |
|
private function checkResponse($response, $profileType, $raw) |
173
|
|
|
{ |
174
|
3 |
|
if ($response['request_stats']['query_status'] == "success") { |
175
|
3 |
|
if ($raw === true) { |
176
|
|
|
return $response; |
177
|
|
|
} else { |
178
|
3 |
|
return $this->fillModel($profileType, $response['data']); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Setup the client |
187
|
|
|
*/ |
188
|
4 |
|
private static function setupClient() |
189
|
|
|
{ |
190
|
4 |
|
if (self::$client === false) { |
191
|
1 |
|
self::$client = new StarCitizensClient(); |
192
|
|
|
} |
193
|
4 |
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Fills our model in with the provided data |
197
|
|
|
* |
198
|
|
|
* @param $model |
199
|
|
|
* @param $fillData |
200
|
|
|
* |
201
|
|
|
* @return Model |
202
|
|
|
*/ |
203
|
3 |
|
private function fillModel($model, $fillData) |
204
|
|
|
{ |
205
|
3 |
|
if (is_array($model)) { |
206
|
1 |
|
list($className, $dataRoot, $idName) =$model; |
207
|
1 |
|
$object = new \ReflectionClass('StarCitizen\Models\Store'); |
208
|
1 |
|
return $object->newInstance($fillData, $className, $dataRoot, $idName); |
209
|
|
|
} else { |
210
|
2 |
|
$object = new \ReflectionClass('StarCitizen\Models' . $model); |
211
|
2 |
|
return $object->newInstance($fillData); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |