Implemented UUID to Name history endpoint

This commit is contained in:
ErickSkrauch 2019-04-07 02:39:16 +02:00
parent ddf3a07d1f
commit 0b0ca2c445
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
4 changed files with 89 additions and 0 deletions

View File

@ -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.

View File

@ -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
*

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Ely\Mojang\Response;
use DateTime;
class NameHistoryItem {
/**
* @var string
*/
private $name;
/**
* @var DateTime|null
*/
private $changedToAt;
public function __construct(string $name, ?DateTime $changedToAt) {
$this->name = $name;
$this->changedToAt = $changedToAt;
}
public function getName(): string {
return $this->name;
}
public function getChangedToAt(): ?DateTime {
return $this->changedToAt;
}
}

View File

@ -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',