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 organizations($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
|
|
|
"organizations" => [ |
32
|
|
|
"base_action" => "single_organization", |
33
|
|
|
"actions" => [ |
34
|
|
|
"single_organization" => '\Organisation', |
35
|
|
|
"organization_members" => ['\OrgMember', '', 'handle'] |
36
|
|
|
] |
37
|
|
|
], |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* StarCitizens constructor. |
42
|
|
|
*/ |
43
|
20 |
|
public function __construct() |
44
|
|
|
{ |
45
|
20 |
|
self::setupClient(); |
46
|
20 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $name |
50
|
|
|
* @param $arguments |
51
|
|
|
* @return bool|mixed |
52
|
|
|
* @throws \Exception |
53
|
|
|
*/ |
54
|
20 |
|
public function __call($name, $arguments) |
55
|
|
|
{ |
56
|
20 |
|
if (array_key_exists($name, $this->systems)) { |
57
|
19 |
|
return $this->doCall($name, $arguments); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
throw new \Exception("Method {$name} not found"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $system |
65
|
|
|
* @param array $arguments |
66
|
|
|
* @return bool|mixed |
67
|
|
|
*/ |
68
|
19 |
|
private function doCall($system, array $arguments = []) |
69
|
|
|
{ |
70
|
19 |
|
$id = ''; |
71
|
19 |
|
$cache = false; |
72
|
19 |
|
$raw = false; |
73
|
19 |
|
$action = ""; |
74
|
|
|
|
75
|
19 |
|
$fixedArguments = $this->standardFindArguments($arguments); |
76
|
19 |
|
extract($fixedArguments, EXTR_OVERWRITE); |
77
|
19 |
|
$action = ($action == "") ? $this->systems[$system]['base_action'] : $action; |
78
|
19 |
|
return $this->find($id, $system, $action, $raw, $cache); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $arguments |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
19 |
|
private function standardFindArguments(array $arguments) |
86
|
|
|
{ |
87
|
|
|
|
88
|
|
|
$defaults = [ |
89
|
19 |
|
'id' => '', |
90
|
19 |
|
'action' => '', |
91
|
19 |
|
'raw' => false, |
92
|
19 |
|
'cache' => false, |
93
|
19 |
|
]; |
94
|
|
|
|
95
|
19 |
|
$varNames = array_keys($defaults); |
96
|
|
|
|
97
|
19 |
|
for ($argumentCount = 0; $argumentCount < 4; $argumentCount++) { |
98
|
19 |
|
if (array_key_exists($argumentCount, $arguments)) { |
99
|
19 |
|
$defaults[$varNames[$argumentCount]] = $arguments[$argumentCount]; |
100
|
19 |
|
} |
101
|
19 |
|
} |
102
|
|
|
|
103
|
19 |
|
return $defaults; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $name |
108
|
|
|
* @param $arguments |
109
|
|
|
* @return bool|mixed |
110
|
|
|
* @throws \Exception |
111
|
|
|
*/ |
112
|
1 |
|
public static function __callStatic($name, $arguments) |
113
|
|
|
{ |
114
|
1 |
|
$starCitizens = new StarCitizens(); |
115
|
1 |
|
return $starCitizens->__call($name, $arguments); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Find an entity |
121
|
|
|
* |
122
|
|
|
* @param $id |
123
|
|
|
* @param $system |
124
|
|
|
* @param $profileType |
125
|
|
|
* @param bool $raw |
126
|
|
|
* @param bool $cache |
127
|
|
|
* @return bool|mixed |
128
|
|
|
*/ |
129
|
19 |
|
private function find($id, $system, $profileType, $raw = false, $cache = false) |
130
|
|
|
{ |
131
|
19 |
|
$response = json_decode( |
132
|
|
|
self::$client |
133
|
19 |
|
->getResult( |
134
|
19 |
|
$this->getParams($id, $system, $profileType, $cache) |
135
|
19 |
|
) |
136
|
19 |
|
->getBody() |
137
|
19 |
|
->getContents(), |
138
|
|
|
true |
139
|
19 |
|
); |
140
|
|
|
|
141
|
19 |
|
return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $id |
146
|
|
|
* @param $system |
147
|
|
|
* @param $profileType |
148
|
|
|
* @param $cache |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
19 |
|
private function getParams($id, $system, $profileType, $cache) |
152
|
|
|
{ |
153
|
19 |
|
$cache = ($cache === true) ? "cache" : "live"; |
154
|
|
|
|
155
|
|
|
return [ |
156
|
19 |
|
'api_source' => $cache, |
157
|
19 |
|
'system' => $system, |
158
|
19 |
|
'action' => $profileType, |
159
|
19 |
|
'target_id' => $id, |
160
|
19 |
|
'expedite' => '0', |
161
|
19 |
|
'format' => 'json', |
162
|
19 |
|
'start_page' => '1', |
163
|
|
|
'end_page' => '5' |
164
|
19 |
|
]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param $response |
169
|
|
|
* @param $profileType |
170
|
|
|
* @param $raw |
171
|
|
|
* |
172
|
|
|
* @return bool|mixed |
173
|
|
|
*/ |
174
|
19 |
|
private function checkResponse($response, $profileType, $raw) |
175
|
|
|
{ |
176
|
19 |
|
if ($response['request_stats']['query_status'] == "success") { |
177
|
19 |
|
if ($raw === true) { |
178
|
1 |
|
return $response; |
179
|
|
|
} else { |
180
|
19 |
|
return $this->fillModel($profileType, $response['data']); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
3 |
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Setup the client |
189
|
|
|
*/ |
190
|
20 |
|
private static function setupClient() |
191
|
|
|
{ |
192
|
20 |
|
if (self::$client === false) { |
193
|
1 |
|
self::$client = new StarCitizensClient(); |
194
|
1 |
|
} |
195
|
20 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Fills our model in with the provided data |
199
|
|
|
* |
200
|
|
|
* @param $model |
201
|
|
|
* @param $fillData |
202
|
|
|
* |
203
|
|
|
* @return Model |
204
|
|
|
*/ |
205
|
19 |
|
private function fillModel($model, $fillData) |
206
|
|
|
{ |
207
|
19 |
|
if (is_array($model)) { |
208
|
15 |
|
list($className, $dataRoot, $idName) =$model; |
209
|
15 |
|
$object = new \ReflectionClass('StarCitizen\Models\Store'); |
210
|
15 |
|
return $object->newInstance($fillData, $className, $dataRoot, $idName); |
211
|
|
|
} else { |
212
|
8 |
|
$object = new \ReflectionClass('StarCitizen\Models' . $model); |
213
|
8 |
|
return $object->newInstance($fillData); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |