From 0b0ca2c44503d01800558d36da74af033d964389 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sun, 7 Apr 2019 02:39:16 +0200 Subject: [PATCH] Implemented UUID to Name history endpoint --- CHANGELOG.md | 1 + src/Api.php | 27 ++++++++++++++++++++++++++ src/Response/NameHistoryItem.php | 33 ++++++++++++++++++++++++++++++++ tests/ApiTest.php | 28 +++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 src/Response/NameHistoryItem.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 162bcbd..5ddd90d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - This CHANGELOG.md file. - `\Ely\Mojang\Api::setClient()` method to override default HTTP client. - [API Status](https://wiki.vg/Mojang_API#API_Status) endpoint. +- [UUID to Name history](https://wiki.vg/Mojang_API#UUID_-.3E_Name_history) endpoint. ### Changed - The constructor no longer has arguments. diff --git a/src/Api.php b/src/Api.php index c35b49b..99a1f80 100644 --- a/src/Api.php +++ b/src/Api.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace Ely\Mojang; +use DateTime; use Ely\Mojang\Middleware\ResponseConverterMiddleware; use Ely\Mojang\Middleware\RetryMiddleware; use GuzzleHttp\Client as GuzzleClient; @@ -70,6 +71,32 @@ class Api { return Response\ProfileInfo::createFromResponse($data); } + /** + * @param string $uuid + * + * @return \Ely\Mojang\Response\NameHistoryItem[] + * + * @throws GuzzleException + * + * @url https://wiki.vg/Mojang_API#UUID_-.3E_Name_history + */ + public function uuidToNameHistory(string $uuid): array { + $response = $this->getClient()->request('GET', "https://api.mojang.com/user/profiles/{$uuid}/names"); + $data = $this->decode($response->getBody()->getContents()); + + $result = []; + foreach ($data as $record) { + $date = null; + if (isset($record['changedToAt'])) { + $date = new DateTime('@' . ($record['changedToAt'] / 1000)); + } + + $result[] = new Response\NameHistoryItem($record['name'], $date); + } + + return $result; + } + /** * @param string $uuid * diff --git a/src/Response/NameHistoryItem.php b/src/Response/NameHistoryItem.php new file mode 100644 index 0000000..7d8b6ba --- /dev/null +++ b/src/Response/NameHistoryItem.php @@ -0,0 +1,33 @@ +name = $name; + $this->changedToAt = $changedToAt; + } + + public function getName(): string { + return $this->name; + } + + public function getChangedToAt(): ?DateTime { + return $this->changedToAt; + } + +} diff --git a/tests/ApiTest.php b/tests/ApiTest.php index afd2baf..f0724ec 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -8,6 +8,7 @@ use Ely\Mojang\Exception\NoContentException; use Ely\Mojang\Middleware\ResponseConverterMiddleware; use Ely\Mojang\Middleware\RetryMiddleware; use Ely\Mojang\Response\ApiStatus; +use Ely\Mojang\Response\NameHistoryItem; use Ely\Mojang\Response\Properties\TexturesProperty; use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; @@ -101,6 +102,33 @@ class ApiTest extends TestCase { $this->assertFalse($result->isDemo()); } + public function testUuidToNameHistory() { + $this->mockHandler->append($this->createResponse(200, [ + [ + 'name' => 'Gold', + ], + [ + 'name' => 'Diamond', + 'changedToAt' => 1414059749000, + ], + ])); + + $result = $this->api->uuidToNameHistory('86f6e3695b764412a29820cac1d4d0d6'); + + /** @var \Psr\Http\Message\RequestInterface $request */ + $request = $this->history[0]['request']; + $this->assertSame('https://api.mojang.com/user/profiles/86f6e3695b764412a29820cac1d4d0d6/names', (string)$request->getUri()); + + $this->assertCount(2, $result); + $this->assertContainsOnlyInstancesOf(NameHistoryItem::class, $result); + + $this->assertSame('Gold', $result[0]->getName()); + $this->assertNull($result[0]->getChangedToAt()); + + $this->assertSame('Diamond', $result[1]->getName()); + $this->assertSame('2014-10-23T10:22:29+00:00', $result[1]->getChangedToAt()->format(DATE_ATOM)); + } + public function testUsernameToUuidWithAtParam() { $this->mockHandler->append($this->createResponse(200, [ 'id' => '86f6e3695b764412a29820cac1d4d0d6',