diff --git a/class/OsuAPI.php b/class/OsuAPI.php index 22f0433..cd4ac8d 100644 --- a/class/OsuAPI.php +++ b/class/OsuAPI.php @@ -1,49 +1,75 @@ apiKey = $apiKey; } - /** * Gets user information for a specific game mode * - * @param string $username [The player's username] - * @param [string] $mode [The game mode] - * $return array | false + * @param string $username The player's username + * @param string $mode The game mode + * @return array|bool */ public function getUserForMode($username, $mode = "osu") { - if (in_array($mode, $this->modes)) { - $query = [ - "k" => $this->apiKey, - "u" => $username, - "m" => $mode, - ]; - - $request = file_get_contents(self::API_URL . "get_user?" . http_build_query($query)); - - if ($request) { - $apiResult = json_decode($request); - - if (isset($apiResult[0])) { - return $apiResult[0]; - } + if (in_array($mode, static::$modes)) { + $request = $this->request('get_user', ['u' => $username, 'm' => $mode]); + if ($request && isset($result[0])) { + return $result[0]; } - return false; } } -} \ No newline at end of file + + /** + * Request from the osu!API + * + * @param string $url The resource to fetch + * @param array $params A list of arguments to give for the resource + */ + public function request($url, $params = []) + { + $params = array_merge(["k" => $this->apiKey], $params); + $url = static::API_URL . $url . '?' . http_build_query($params); + + return $this->decode(file_get_contents($url)); + } + + /** + * Decode a response from the API + * + * @param string $content The response from the API + * @return array|null The decoded JSON object, or null. + */ + protected function decode($content) + { + // todo: error handling here + if (strlen($content) > 0 && $content) { + return json_decode($content, true); + } + + return null; + } +}