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