From 8b06adb7e8affa6704c32fc48ca0b5fd0f5d230a Mon Sep 17 00:00:00 2001 From: ErickSkrauch <erickskrauch@yandex.ru> Date: Tue, 22 Mar 2016 00:22:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=81=D1=82=D0=B5=D1=86=D0=BA=D1=83=D1=8E=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=91=D1=80=D1=82=D0=BA=D1=83=20=D0=BD=D0=B0=D0=B4?= =?UTF-8?q?=20Mojang=20API=20(=D0=BF=D0=BE=D0=BA=D0=B0=20=D1=82=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=B0=20uuid=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=BD=D0=B8=D0=BA=D1=83).=20=D0=97=D0=B0=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D1=81=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=20=D0=BC=D0=B8?= =?UTF-8?q?=D0=BD=D0=BE=D1=80=D0=BD=D1=8B=D0=B5=20=D0=B2=D0=B5=D1=80=D1=81?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D0=B1=D0=B8=D0=B1=D0=BB?= =?UTF-8?q?=D0=B8=D0=BE=D1=82=D0=B5=D0=BA=20Composer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/components/Mojang/Api.php | 49 +++++++++++++++++++ .../Mojang/exceptions/MojangApiException.php | 8 +++ .../Mojang/exceptions/NoContentException.php | 6 +++ .../response/UsernameToUUIDResponse.php | 29 +++++++++++ composer.json | 7 +-- 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 common/components/Mojang/Api.php create mode 100644 common/components/Mojang/exceptions/MojangApiException.php create mode 100644 common/components/Mojang/exceptions/NoContentException.php create mode 100644 common/components/Mojang/response/UsernameToUUIDResponse.php diff --git a/common/components/Mojang/Api.php b/common/components/Mojang/Api.php new file mode 100644 index 0000000..70e253d --- /dev/null +++ b/common/components/Mojang/Api.php @@ -0,0 +1,49 @@ +<?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 + */ + public function usernameToUUID($username, $atTime = null) { + $client = $this->createClient(); + $request = $client->createRequest('GET', 'https://api.mojang.com/users/profiles/minecraft/' . $username); + $queryParams = []; + if ($atTime !== null) { + $queryParams['atTime'] = $atTime; + } + + $request->setQuery($queryParams); + $response = $client->send($request); + if ($response->getStatusCode() === 204) { + throw new NoContentException('Username not found'); + } elseif ($response->getStatusCode()) { + throw new MojangApiException('Unexpected request result'); + } + + $data = $response->json(['object' => false]); + $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(); + } + +} diff --git a/common/components/Mojang/exceptions/MojangApiException.php b/common/components/Mojang/exceptions/MojangApiException.php new file mode 100644 index 0000000..954f76d --- /dev/null +++ b/common/components/Mojang/exceptions/MojangApiException.php @@ -0,0 +1,8 @@ +<?php +namespace common\components\Mojang\exceptions; + +use Exception; + +class MojangApiException extends Exception { + +} diff --git a/common/components/Mojang/exceptions/NoContentException.php b/common/components/Mojang/exceptions/NoContentException.php new file mode 100644 index 0000000..6fb08d3 --- /dev/null +++ b/common/components/Mojang/exceptions/NoContentException.php @@ -0,0 +1,6 @@ +<?php +namespace common\components\Mojang\exceptions; + +class NoContentException extends MojangApiException { + +} diff --git a/common/components/Mojang/response/UsernameToUUIDResponse.php b/common/components/Mojang/response/UsernameToUUIDResponse.php new file mode 100644 index 0000000..de7dc9a --- /dev/null +++ b/common/components/Mojang/response/UsernameToUUIDResponse.php @@ -0,0 +1,29 @@ +<?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; + +} diff --git a/composer.json b/composer.json index 1efe3e9..5b5ab15 100644 --- a/composer.json +++ b/composer.json @@ -15,13 +15,14 @@ "minimum-stability": "stable", "require": { "php": ">=5.6.0", - "yiisoft/yii2": ">=2.0.6", + "yiisoft/yii2": "~2.0.6", "yiisoft/yii2-bootstrap": "*", "yiisoft/yii2-swiftmailer": "*", - "ramsey/uuid": "^3.1", + "ramsey/uuid": "~3.1", "league/oauth2-server": "~4.1.5", "yiisoft/yii2-redis": "~2.0.0", - "damirka/yii2-jwt": "dev-master#be29a5b5d7e7d146c13d4374788e02c54cc04138" + "damirka/yii2-jwt": "dev-master#be29a5b5d7e7d146c13d4374788e02c54cc04138", + "guzzlehttp/guzzle": "~5.3.0" }, "require-dev": { "yiisoft/yii2-codeception": "*",