mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Implemented "ely/mojang-api"
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
<?php
|
||||
namespace common\components\Mojang;
|
||||
|
||||
use common\components\Mojang\exceptions\MojangApiException;
|
||||
use common\components\Mojang\exceptions\NoContentException;
|
||||
use common\components\Mojang\response\UsernameToUUIDResponse;
|
||||
use Yii;
|
||||
|
||||
class Api {
|
||||
|
||||
/**
|
||||
* @param string $username
|
||||
* @param int $atTime
|
||||
*
|
||||
* @return UsernameToUUIDResponse
|
||||
* @throws MojangApiException
|
||||
* @throws NoContentException|\GuzzleHttp\Exception\RequestException
|
||||
* @url http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time
|
||||
*/
|
||||
public function usernameToUUID($username, $atTime = null) {
|
||||
$query = [];
|
||||
if ($atTime !== null) {
|
||||
$query['atTime'] = $atTime;
|
||||
}
|
||||
|
||||
$response = $this->getClient()->get($this->buildUsernameToUUIDRoute($username), $query);
|
||||
if ($response->getStatusCode() === 204) {
|
||||
throw new NoContentException('Username not found');
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new MojangApiException('Unexpected request result');
|
||||
}
|
||||
|
||||
$data = json_decode($response->getBody(), true);
|
||||
$responseObj = new UsernameToUUIDResponse();
|
||||
$responseObj->id = $data['id'];
|
||||
$responseObj->name = $data['name'];
|
||||
$responseObj->legacy = isset($data['legacy']);
|
||||
$responseObj->demo = isset($data['demo']);
|
||||
|
||||
return $responseObj;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \GuzzleHttp\Client
|
||||
*/
|
||||
protected function getClient() {
|
||||
return Yii::$app->guzzle;
|
||||
}
|
||||
|
||||
protected function buildUsernameToUUIDRoute($username) {
|
||||
return 'https://api.mojang.com/users/profiles/minecraft/' . $username;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
namespace common\components\Mojang\exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class MojangApiException extends Exception {
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
<?php
|
||||
namespace common\components\Mojang\exceptions;
|
||||
|
||||
class NoContentException extends MojangApiException {
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
namespace common\components\Mojang\response;
|
||||
|
||||
/**
|
||||
* http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time
|
||||
*/
|
||||
class UsernameToUUIDResponse {
|
||||
|
||||
/**
|
||||
* @var string uuid пользователя без разделения на дефисы
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* @var string ник пользователя в настоящем времени
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var bool если имеет значение true, то значит аккаунт не мигрирован в Mojang аккаунт
|
||||
*/
|
||||
public $legacy = false;
|
||||
|
||||
/**
|
||||
* @var bool будет иметь значение true, если аккаунт находится в демо-режиме (не приобретена лицензия)
|
||||
*/
|
||||
public $demo = false;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user