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) |
13
|
|
|
* @method organisations($id, $profileType = false, $cache = false, $raw = false) |
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
|
3 |
|
public function __construct() |
37
|
|
|
{ |
38
|
3 |
|
static::setupClient(); |
|
|
|
|
39
|
3 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $name |
43
|
|
|
* @param $arguments |
44
|
|
|
* @return bool|mixed |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
3 |
|
function __call($name, $arguments) |
|
|
|
|
48
|
|
|
{ |
49
|
3 |
|
if (array_key_exists($name, $this->systems)) { |
50
|
2 |
|
$argumentCount = count($arguments); |
51
|
2 |
|
if ($argumentCount == 0) |
52
|
2 |
|
throw new \InvalidArgumentException("Requires an argument"); |
53
|
|
|
|
54
|
2 |
|
if ($argumentCount > 0 && $argumentCount< 2) |
55
|
2 |
|
return $this->find($arguments[0], $name, $this->systems[$name]['base_action']); |
56
|
|
|
|
57
|
1 |
|
if ($argumentCount == 2) { |
58
|
1 |
|
list($id, $profileType) = $arguments; |
59
|
1 |
|
if ($profileType == false) |
60
|
1 |
|
$profileType = $this->systems[$name]['base_action']; |
61
|
1 |
|
return $this->find($id, $name, $profileType); |
62
|
|
|
} |
63
|
|
|
if ($argumentCount == 4 ) { |
64
|
|
|
list($id, $profileType, $cache, $raw) = $arguments; |
65
|
|
|
return $this->find($id, $name, $profileType, $cache, $raw); |
66
|
|
|
} else { |
67
|
|
|
throw new \InvalidArgumentException("Invalid arguments"); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
throw new \Exception("Method not found"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Find an entity |
76
|
|
|
* |
77
|
|
|
* @param $id |
78
|
|
|
* @param $system |
79
|
|
|
* @param $profileType |
80
|
|
|
* @param bool $cache |
81
|
|
|
* @param bool $raw |
82
|
|
|
* @return bool|mixed |
83
|
|
|
*/ |
84
|
2 |
|
private function find($id, $system, $profileType, $cache = false, $raw = false) |
85
|
|
|
{ |
86
|
2 |
|
var_dump($this->getParams($id, $system, $profileType, $cache)); |
|
|
|
|
87
|
2 |
|
$response = json_decode( |
88
|
|
|
self::$client |
89
|
2 |
|
->getResult( |
90
|
2 |
|
$this->getParams($id, $system, $profileType, $cache) |
91
|
2 |
|
) |
92
|
2 |
|
->getBody() |
93
|
2 |
|
->getContents(), |
94
|
|
|
true |
95
|
2 |
|
); |
96
|
|
|
|
97
|
2 |
|
return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $id |
102
|
|
|
* @param $system |
103
|
|
|
* @param $profileType |
104
|
|
|
* @param $cache |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
2 |
|
private function getParams($id, $system, $profileType, $cache) |
108
|
|
|
{ |
109
|
2 |
|
$cache = ($cache === true) ? "cache" : "live"; |
110
|
|
|
|
111
|
|
|
return [ |
112
|
2 |
|
'api_source' => $cache, |
113
|
2 |
|
'system' => $system, |
114
|
2 |
|
'action' => $profileType, |
115
|
2 |
|
'target_id' => $id, |
116
|
2 |
|
'expedite' => '0', |
117
|
2 |
|
'format' => 'json', |
118
|
2 |
|
'start_page' => '1', |
119
|
|
|
'end_page' => '5' |
120
|
2 |
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $response |
125
|
|
|
* @param $profileType |
126
|
|
|
* @param $raw |
127
|
|
|
* |
128
|
|
|
* @return bool|mixed |
129
|
|
|
*/ |
130
|
2 |
|
private function checkResponse($response, $profileType, $raw) |
131
|
|
|
{ |
132
|
2 |
|
if ($response['request_stats']['query_status'] == "success") { |
133
|
2 |
|
if ($raw === true) { |
134
|
|
|
return $response; |
135
|
|
|
} else { |
136
|
2 |
|
return $this->fillModel($profileType, $response['data']); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Setup the client |
145
|
|
|
*/ |
146
|
3 |
|
private static function setupClient() |
147
|
|
|
{ |
148
|
3 |
|
if (static::$client === false) { |
|
|
|
|
149
|
1 |
|
static::$client = new StarCitizensClient(); |
|
|
|
|
150
|
1 |
|
} |
151
|
3 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Fills our model in with the provided data |
155
|
|
|
* |
156
|
|
|
* @param $model |
157
|
|
|
* @param $fillData |
158
|
|
|
* |
159
|
|
|
* @return Model |
160
|
|
|
*/ |
161
|
2 |
|
private function fillModel($model, $fillData) |
162
|
|
|
{ |
163
|
2 |
|
if (is_array($model)) { |
164
|
1 |
|
list($className, $dataRoot, $idName) =$model; |
165
|
1 |
|
$object = new \ReflectionClass('StarCitizen\Models\Store'); |
166
|
1 |
|
return $object->newInstance($fillData, $className, $dataRoot, $idName); |
167
|
|
|
} else { |
168
|
1 |
|
$object = new \ReflectionClass('StarCitizen\Models' . $model); |
169
|
1 |
|
return $object->newInstance($fillData); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
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.