mirror of
https://github.com/elyby/mojang-api.git
synced 2024-12-22 08:09:48 +05:30
Implemented UUID to Name history endpoint
This commit is contained in:
parent
ddf3a07d1f
commit
0b0ca2c445
@ -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.
|
||||
|
27
src/Api.php
27
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
|
||||
*
|
||||
|
33
src/Response/NameHistoryItem.php
Normal file
33
src/Response/NameHistoryItem.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user