Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ use Aternos\SpigotApi\Client\SortDirection;
$projects = $spigotClient->listProjects(1);

foreach ($projects as $project) {
// wrappers expose underlying model via getData()
echo $project->getData()->getTitle() . PHP_EOL;
// wrappers extend the generated models, so all getters can be used directly
echo $project->getTitle() . PHP_EOL;
}

$projects = $projects->getNextPage();

foreach ($projects as $project) {
echo $project->getData()->getTitle() . PHP_EOL;
echo $project->getTitle() . PHP_EOL;
}

// list the most downloaded projects
Expand All @@ -118,14 +118,14 @@ use Aternos\SpigotApi\Client\SortDirection;
$projects = $spigotClient->listProjectsForCategory(Category::SPIGOT, 1);

foreach ($projects as $project) {
// wrappers expose underlying model via getData()
echo $project->getData()->getTitle() . PHP_EOL;
// wrappers extend the generated models, so all getters can be used directly
echo $project->getTitle() . PHP_EOL;
}

$projects = $projects->getNextPage();

foreach ($projects as $project) {
echo $project->getData()->getTitle() . PHP_EOL;
echo $project->getTitle() . PHP_EOL;
}

// list the most recently updated projects in a category
Expand All @@ -145,10 +145,13 @@ To get a single project (resource) by its numeric id, use `getProject()`.
// get a specific project (by numeric id)
$project = $spigotClient->getProject(2);

// get data from the wrapper
$id = $project->getData()->getId();
$title = $project->getData()->getTitle();
$description = $project->getData()->getDescription();
// the wrapper extends the generated model, so all getters of the model can be used directly
$id = $project->getId();
$title = $project->getTitle();
$description = $project->getDescription();

// the category as an Aternos\SpigotApi\Client\Category enum
$category = $project->getCategoryType();
```

### More information
Expand All @@ -167,10 +170,13 @@ $versions = $project->fetchVersions();
// fetch the newest versions first
$versions = $project->fetchVersions(1, VersionOrder::POST_DATE, SortDirection::DESC);

// get the author
$author = $project->getAuthor();
// fetch the full author from the API
$author = $project->fetchAuthor();
```

`$project->getAuthor()` and `$project->getCategory()` are the getters of the generated model.
They only contain the id and username of the author and the id, title and description of the category, and do not send a request.

## Authors

### getAuthor()
Expand All @@ -185,12 +191,12 @@ To get a single author by its numeric id, use `getAuthor()`.
$author = $spigotClient->getAuthor(1);

// get data from the wrapper
$id = $author->getData()->getId();
$username = $author->getData()->getUsername();
$resources = $author->getData()->getResourceCount();
$identities = $author->getData()->getIdentities();
$avatarUrl = $author->getData()->getAvatar();
$activity = $author->getData()->getLastActivity();
$id = $author->getId();
$username = $author->getUsername();
$resources = $author->getResourceCount();
$identities = $author->getIdentities();
$avatarUrl = $author->getAvatar();
$activity = $author->getLastActivity();
```

### findAuthor()
Expand All @@ -205,12 +211,12 @@ To find an author by username, use `findAuthor()`.
$author = $spigotClient->findAuthor("md_5");

// get data from the wrapper
$id = $author->getData()->getId();
$username = $author->getData()->getUsername();
$resources = $author->getData()->getResourceCount();
$identities = $author->getData()->getIdentities();
$avatarUrl = $author->getData()->getAvatar();
$activity = $author->getData()->getLastActivity();
$id = $author->getId();
$username = $author->getUsername();
$resources = $author->getResourceCount();
$identities = $author->getIdentities();
$avatarUrl = $author->getAvatar();
$activity = $author->getLastActivity();
```

### More information
Expand All @@ -237,7 +243,7 @@ To list all projects for an author (paginated), use `getAuthorProjects()`.
// list all projects from Luck
$projects = $spigotClient->getAuthorProjects(100356);
foreach ($projects as $project) {
echo $project->getData()->getTitle() . PHP_EOL;
echo $project->getTitle() . PHP_EOL;
}
```

Expand All @@ -260,7 +266,7 @@ use Aternos\SpigotApi\Client\VersionOrder;

$versions = $spigotClient->getProjectVersions(2);
foreach ($versions as $version) {
echo $version->getData()->getTitle() . PHP_EOL;
echo $version->getTitle() . PHP_EOL;
}

// list the newest versions first
Expand All @@ -277,8 +283,8 @@ To fetch a specific update by its id, use `getVersion()`.

```php
$version = $spigotClient->getVersion(352711);
echo $version->getData()->getTitle() . PHP_EOL;
echo $version->getData()->getMessage() . PHP_EOL;
echo $version->getTitle() . PHP_EOL;
echo $version->getMessage() . PHP_EOL;
```

### More information
Expand Down
14 changes: 5 additions & 9 deletions src/Client/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@

use Aternos\SpigotApi\ApiException;
use Aternos\SpigotApi\Client\List\PaginatedAuthorProjectList;
use Aternos\SpigotApi\Model\Author as SpigotAuthor;
use Aternos\SpigotApi\Model\Author as AuthorModel;

class Author
class Author extends AuthorModel
{

public function __construct(
protected SpigotAPIClient $client,
protected SpigotAuthor $data
AuthorModel $data
)
{
}

public function getData(): SpigotAuthor
{
return $this->data;
parent::__construct($data->container);
}

/**
Expand All @@ -28,7 +24,7 @@ public function getData(): SpigotAuthor
*/
public function getId(): int
{
return intval($this->data->getId());
return intval(parent::getId());
}

/**
Expand Down
25 changes: 12 additions & 13 deletions src/Client/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
use Aternos\SpigotApi\Client\List\PaginatedVersionList;
use Aternos\SpigotApi\Model\Resource;

class Project
class Project extends Resource
{

public function __construct(
protected SpigotAPIClient $client,
protected Resource $data
Resource $data
)
{
}

public function getData(): Resource
{
return $this->data;
parent::__construct($data->container);
}

/**
Expand All @@ -28,27 +24,30 @@ public function getData(): Resource
*/
public function getId(): int
{
return intval($this->data->getId());
return intval(parent::getId());
}

/**
* Get the category of the project.
*
* @return Category
*/
public function getCategory(): Category
public function getCategoryType(): Category
{
return Category::from($this->getData()->getCategory()->getId());
return Category::from($this->getCategory()->getId());
}

/**
* Get the author of the project.
* Fetch the author of this project from the API.
*
* This sends a request to the SpigotMC API every time it is called.
*
* @return Author
* @throws ApiException
*/
public function getAuthor(): Author
public function fetchAuthor(): Author
{
return $this->client->getAuthor($this->data->getAuthor()->getId());
return $this->client->getAuthor($this->getAuthor()->getId());
}

/**
Expand Down
14 changes: 5 additions & 9 deletions src/Client/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
use Aternos\SpigotApi\ApiException;
use Aternos\SpigotApi\Model\ResourceUpdate;

class Version
class Version extends ResourceUpdate
{

public function __construct(
protected SpigotAPIClient $client,
protected ResourceUpdate $data
ResourceUpdate $data
)
{
}

public function getData(): ResourceUpdate
{
return $this->data;
parent::__construct($data->container);
}

/**
Expand All @@ -27,7 +23,7 @@ public function getData(): ResourceUpdate
*/
public function getId(): int
{
return intval($this->data->getId());
return intval(parent::getId());
}

/**
Expand All @@ -37,7 +33,7 @@ public function getId(): int
*/
public function getProjectId(): int
{
return intval($this->getData()->getResourceId());
return intval($this->getResourceId());
}

/**
Expand Down
28 changes: 13 additions & 15 deletions tests/Integration/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public function testListProjectsOrderedByDownloadCount(): void
// collect the download counts of all projects from both pages
$downloadCounts = [];
foreach ($firstPage as $project) {
$downloadCounts[] = $project->getData()->getStats()->getDownloads();
$downloadCounts[] = $project->getStats()->getDownloads();
}
foreach ($secondPage as $project) {
$downloadCounts[] = $project->getData()->getStats()->getDownloads();
$downloadCounts[] = $project->getStats()->getDownloads();
}
$this->assertNotEmpty($downloadCounts);

Expand All @@ -47,10 +47,10 @@ public function testGetProjectVersionsOrderedByPostDate(): void
// collect the post dates of all versions from both pages
$postDates = [];
foreach ($firstPage as $version) {
$postDates[] = $version->getData()->getPostDate();
$postDates[] = $version->getPostDate();
}
foreach ($secondPage as $version) {
$postDates[] = $version->getData()->getPostDate();
$postDates[] = $version->getPostDate();
}
$this->assertNotEmpty($postDates);

Expand Down Expand Up @@ -267,9 +267,9 @@ public function testGetProject(): void
{
$project = $this->apiClient->getProject(2);
$this->assertNotNull($project);
$this->assertEquals("HubKick", $project->getData()->getTitle());
$this->assertEquals("Send players to lobby on kick. /lobby / hub", $project->getData()->getTag());
$this->assertEquals(1364368470, $project->getData()->getFirstRelease());
$this->assertEquals("HubKick", $project->getTitle());
$this->assertEquals("Send players to lobby on kick. /lobby / hub", $project->getTag());
$this->assertEquals(1364368470, $project->getFirstRelease());
}

/**
Expand Down Expand Up @@ -297,9 +297,9 @@ public function testGetVersion(): void
// PlaceholderAPI by HelpChat
$version = $this->apiClient->getVersion(352711);
$this->assertNotNull($version);
$this->assertEquals("2.10.9 back to normal", $version->getData()->getTitle());
$this->assertEquals(352711, $version->getData()->getId());
$this->assertEquals(6245, $version->getData()->getResourceId());
$this->assertEquals("2.10.9 back to normal", $version->getTitle());
$this->assertEquals(352711, $version->getId());
$this->assertEquals(6245, $version->getResourceId());
}

/**
Expand All @@ -309,8 +309,7 @@ public function testGetAuthor(): void
{
$author = $this->apiClient->getAuthor(100356);
$this->assertNotNull($author);
$this->assertNotNull($author->getData());
$this->assertEquals("Luck", $author->getData()->getUsername());
$this->assertEquals("Luck", $author->getUsername());
}

/**
Expand All @@ -320,9 +319,8 @@ public function testFindAuthor(): void
{
$author = $this->apiClient->findAuthor("Luck");
$this->assertNotNull($author);
$this->assertNotNull($author->getData());
$this->assertEquals("Luck", $author->getData()->getUsername());
$this->assertEquals(100356, $author->getData()->getId());
$this->assertEquals("Luck", $author->getUsername());
$this->assertEquals(100356, $author->getId());
}

}
22 changes: 11 additions & 11 deletions tests/Integration/Client/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@ public function setUp(): void
$this->project = $this->apiClient->getProject(57242);
}

public function testGetCategory(): void
public function testGetCategoryType(): void
{
$resourceCategory = $this->project->getData()->getCategory();
$resourceCategory = $this->project->getCategory();
$this->assertEquals(15, $resourceCategory->getId());
$this->assertEquals("Tools and Utilities", $resourceCategory->getTitle());
$this->assertEquals("", $resourceCategory->getDescription());

$category = $this->project->getCategory();
$category = $this->project->getCategoryType();
$this->assertEquals(Category::SPIGOT_TOOLS_AND_UTILITIES, $category);
}

/**
* @throws ApiException
*/
public function testGetAuthor(): void
public function testFetchAuthor(): void
{
// Luck
$author = $this->project->getAuthor();
$author = $this->project->fetchAuthor();
$this->assertNotNull($author);
$this->assertValidAuthor($author->getData());
$this->assertValidAuthor($author);

$this->assertEquals(100356, $author->getData()->getId());
$this->assertEquals("Luck", $author->getData()->getUsername());
$this->assertGreaterThan(1, $author->getData()->getResourceCount());
$this->assertStringStartsWith("https://www.spigotmc.org/data/avatars", $author->getData()->getAvatar());
$this->assertEquals(100356, $author->getId());
$this->assertEquals("Luck", $author->getUsername());
$this->assertGreaterThan(1, $author->getResourceCount());
$this->assertStringStartsWith("https://www.spigotmc.org/data/avatars", $author->getAvatar());
// 1759349337 -> Wed Oct 01 2025
$this->assertGreaterThanOrEqual(1759349337, $author->getData()->getLastActivity());
$this->assertGreaterThanOrEqual(1759349337, $author->getLastActivity());
}

/**
Expand Down
Loading
Loading