2016-03-22 02:52:17 +05:30
|
|
|
<?php
|
|
|
|
namespace common\components\Mojang;
|
|
|
|
|
|
|
|
use common\components\Mojang\exceptions\MojangApiException;
|
|
|
|
use common\components\Mojang\exceptions\NoContentException;
|
|
|
|
use common\components\Mojang\response\UsernameToUUIDResponse;
|
|
|
|
use GuzzleHttp\Client as GuzzleClient;
|
|
|
|
|
|
|
|
class Api {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $username
|
|
|
|
* @param int $atTime
|
|
|
|
*
|
|
|
|
* @return UsernameToUUIDResponse
|
|
|
|
* @throws MojangApiException
|
|
|
|
* @throws NoContentException
|
2016-04-24 00:14:10 +05:30
|
|
|
* @url http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time
|
2016-03-22 02:52:17 +05:30
|
|
|
*/
|
|
|
|
public function usernameToUUID($username, $atTime = null) {
|
2016-07-21 21:42:55 +05:30
|
|
|
$query = [];
|
2016-03-22 02:52:17 +05:30
|
|
|
if ($atTime !== null) {
|
2016-07-21 21:42:55 +05:30
|
|
|
$query['atTime'] = $atTime;
|
2016-03-22 02:52:17 +05:30
|
|
|
}
|
|
|
|
|
2016-07-21 21:42:55 +05:30
|
|
|
$response = $this->createClient()->get($this->buildUsernameToUUIDRoute($username), $query);
|
2016-03-22 02:52:17 +05:30
|
|
|
if ($response->getStatusCode() === 204) {
|
|
|
|
throw new NoContentException('Username not found');
|
2016-04-24 00:14:10 +05:30
|
|
|
} elseif ($response->getStatusCode() !== 200) {
|
2016-03-22 02:52:17 +05:30
|
|
|
throw new MojangApiException('Unexpected request result');
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:42:55 +05:30
|
|
|
$data = json_decode($response->getBody(), true);
|
2016-03-22 02:52:17 +05:30
|
|
|
$responseObj = new UsernameToUUIDResponse();
|
|
|
|
$responseObj->id = $data['id'];
|
|
|
|
$responseObj->name = $data['name'];
|
|
|
|
$responseObj->legacy = isset($data['legacy']);
|
|
|
|
$responseObj->demo = isset($data['demo']);
|
|
|
|
|
|
|
|
return $responseObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createClient() {
|
|
|
|
return new GuzzleClient();
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:42:55 +05:30
|
|
|
protected function buildUsernameToUUIDRoute($username) {
|
|
|
|
return 'https://api.mojang.com/users/profiles/minecraft/' . $username;
|
|
|
|
}
|
|
|
|
|
2016-03-22 02:52:17 +05:30
|
|
|
}
|