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
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and some wrappers around it to improve the usability.
The generated code can be found in `src/Api` and `src/Model`. It is recommended to use the wrappers in
`src/Client` instead.

The wrappers like `Project`, `Version` or `User` extend the generated models, so all getters of the models
can be used directly, e.g. `$project->getTitle()`.
Methods of the wrappers that send a request to the API start with `fetch`, e.g. `$project->fetchMembers()`.

## Requirements
- PHP 8.4.1 or newer
- PHP extensions: curl, json, mbstring
Expand Down Expand Up @@ -88,7 +92,7 @@ $allProjects = $projects->getResultsFromFollowingPages();
Search results are `SearchProject` objects. They only contain the data returned by the search,
but you can fetch the full project or the author:
```php
$project = $projects[0]->getFullProject();
$project = $projects[0]->fetchFullProject();
$author = $projects[0]->fetchAuthor();
```

Expand Down Expand Up @@ -188,16 +192,16 @@ $versions = $project->fetchProjectVersions();
$versions = $project->fetchProjectVersions(["paper"], ["1.21"]);

// the members of the project team
$members = $project->getMembers();
$members = $project->fetchMembers();

// the dependencies of the project
// projects can depend on other projects or directly on specific versions
$dependencies = $project->getDependencies();
$dependencies = $project->fetchDependencies();
$dependencyProjects = $dependencies->getProjects();
$dependencyVersions = $dependencies->getVersions();

// the moderation thread of the project (requires authentication, not available for search results)
$thread = $project->getThread();
$thread = $project->fetchThread();
```

Note that `$project->getVersions()` is the getter of the generated model and only returns the version ids.
Expand All @@ -221,13 +225,13 @@ $versions = $modrinthClient->getVersions(["xzRGr4AC", "EwNN8uNA"]);

The `Version` wrapper provides methods to fetch related data:
```php
$project = $version->getProject();
$author = $version->getAuthor();
$project = $version->fetchProject();
$author = $version->fetchAuthor();

foreach ($version->getDependencies() as $dependency) {
// a dependency can point to a project, a specific version or both
$dependencyProject = $dependency->getProject();
$dependencyVersion = $dependency->getVersion();
$dependencyProject = $dependency->fetchProject();
$dependencyVersion = $dependency->fetchVersion();
}

// get the download url of the primary file
Expand Down Expand Up @@ -275,16 +279,16 @@ $users = $modrinthClient->getUsers(["MpxzqsyW", "Dc7EYhxG"]);
$user = $modrinthClient->getCurrentUser();

// get the projects of a user
$projects = $user->getProjects();
$projects = $user->fetchProjects();

// get the projects a user follows (requires authentication)
$projects = $user->getFollowedProjects();
$projects = $user->fetchFollowedProjects();

// get the notifications of a user (requires authentication)
$notifications = $user->getNotifications();
$notifications = $user->fetchNotifications();

// get the payout history of a user (requires authentication)
$history = $user->getPayoutHistory();
$history = $user->fetchPayoutHistory();
```

## Teams
Expand Down
5 changes: 3 additions & 2 deletions src/Client/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public function __construct(
}

/**
* Get the user that received the notification
* Fetch the user that received the notification
* This sends a request to the Modrinth API every time it is called.
* @return User
* @throws ApiException
*/
public function getUser(): User
public function fetchUser(): User
{
return $this->client->getUser($this->getUserId());
}
Expand Down
5 changes: 3 additions & 2 deletions src/Client/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ public function __construct(
}

/**
* Fetch the moderation thread of this project from the API (requires authentication)
* Fetch the moderation thread of this project (requires authentication)
* This sends a request to the Modrinth API every time it is called.
*
* Not part of {@link ProjectTrait} because search results do not carry a thread id.
*
* @return Thread
* @throws ApiException
*/
public function getThread(): Thread
public function fetchThread(): Thread
{
return $this->client->getThread($this->getThreadId());
}
Expand Down
13 changes: 8 additions & 5 deletions src/Client/ProjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ abstract protected function getId(): string;
abstract protected function getClient(): ModrinthAPIClient;

/**
* Get all dependencies of this project
* Fetch all dependencies of this project
* This sends a request to the Modrinth API every time it is called.
* @return ProjectDependencies
* @throws ApiException
*/
public function getDependencies(): ProjectDependencies
public function fetchDependencies(): ProjectDependencies
{
return $this->getClient()->getProjectDependencies($this->getId());
}

/**
* Get all versions of this project
* Fetch all versions of this project
* This sends a request to the Modrinth API every time it is called.
* @param string[]|null $loaders only show versions for any of these loaders
* @param string[]|null $gameVersions
* @param bool|null $featured
Expand All @@ -48,11 +50,12 @@ public function fetchProjectVersions(
}

/**
* Get the members of this project
* Fetch the members of this project
* This sends a request to the Modrinth API every time it is called.
* @return TeamMember[]
* @throws ApiException
*/
public function getMembers(): array
public function fetchMembers(): array
{
return $this->getClient()->getProjectMembers($this->getId());
}
Expand Down
8 changes: 5 additions & 3 deletions src/Client/SearchProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ public function __construct(
}

/**
* Fetch the full project from the API
* Fetch the full project
* This sends a request to the Modrinth API every time it is called.
* @return Project
* @throws ApiException
*/
public function getFullProject(): Project
public function fetchFullProject(): Project
{
return $this->client->getProject($this->getProjectId());
}

/**
* Fetch the author of this project from the API.
* Fetch the author of this project
* This sends a request to the Modrinth API every time it is called.
*
* Named differently from the inherited {@link ProjectResult::getAuthor()}, which
* returns the author's username without requesting anything.
Expand Down
5 changes: 3 additions & 2 deletions src/Client/TeamMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public function getUser(): User
}

/**
* Fetch all members of the team this member belongs to from the API
* Fetch all members of the team this member belongs to
* This sends a request to the Modrinth API every time it is called.
* @return TeamMember[]
* @throws ApiException
*/
public function getTeamMembers(): array
public function fetchTeamMembers(): array
{
return $this->client->getTeamMembers($this->getTeamId());
}
Expand Down
20 changes: 12 additions & 8 deletions src/Client/Threads/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public function getItemTypeEnum(): ReportItemType
}

/**
* Fetch the full project from the API if the report is a project report
* Fetch the reported project if the report is a project report
* This sends a request to the Modrinth API every time it is called.
* @return Project
* @throws ApiException
* @throws Exception
*/
public function getProject(): Project
public function fetchProject(): Project
{
if ($this->getItemTypeEnum() !== ReportItemType::PROJECT) {
throw new Exception("Report is not a project report");
Expand All @@ -44,12 +45,13 @@ public function getProject(): Project
}

/**
* Fetch the full user from the API if the report is a user report
* Fetch the reported user if the report is a user report
* This sends a request to the Modrinth API every time it is called.
* @return User
* @throws ApiException
* @throws Exception
*/
public function getUser(): User
public function fetchUser(): User
{
if ($this->getItemTypeEnum() !== ReportItemType::USER) {
throw new Exception("Report is not a user report");
Expand All @@ -59,12 +61,13 @@ public function getUser(): User
}

/**
* Fetch the full version from the API if the report is a version report
* Fetch the reported version if the report is a version report
* This sends a request to the Modrinth API every time it is called.
* @return Version
* @throws ApiException
* @throws Exception
*/
public function getVersion(): Version
public function fetchVersion(): Version
{
if ($this->getItemTypeEnum() !== ReportItemType::VERSION) {
throw new Exception("Report is not a version report");
Expand All @@ -74,11 +77,12 @@ public function getVersion(): Version
}

/**
* Fetch the thread of this report from the API (requires authentication)
* Fetch the thread of this report (requires authentication)
* This sends a request to the Modrinth API every time it is called.
* @return Thread
* @throws ApiException
*/
public function getThread(): Thread
public function fetchThread(): Thread
{
return $this->client->getThread($this->getThreadId());
}
Expand Down
12 changes: 8 additions & 4 deletions src/Client/Threads/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public function __construct(
}

/**
* Fetch the full project from the API if the thread has a project id
* Fetch the project of this thread
* This sends a request to the Modrinth API every time it is called.
* Returns null if the thread has no project id.
* @return Project|null
* @throws ApiException
*/
public function getProject(): ?Project
public function fetchProject(): ?Project
{
if ($id = $this->getProjectId()) {
return $this->client->getProject($id);
Expand All @@ -33,11 +35,13 @@ public function getProject(): ?Project
}

/**
* Fetch the full report from the API if the thread has a report id
* Fetch the report of this thread
* This sends a request to the Modrinth API every time it is called.
* Returns null if the thread has no report id.
* @return Report|null
* @throws ApiException
*/
public function getReport(): ?Report
public function fetchReport(): ?Report
{
if ($id = $this->getReportId()) {
return $this->client->getReport($id);
Expand Down
5 changes: 3 additions & 2 deletions src/Client/Threads/ThreadMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ public function getThread(): Thread
}

/**
* Fetch the user that wrote this message from the API.
* Fetch the user that wrote this message
* This sends a request to the Modrinth API every time it is called.
* Returns null if the message has no author, e.g. for system messages.
* @return User|null
* @throws ApiException
*/
public function getAuthor(): ?User
public function fetchAuthor(): ?User
{
if ($this->getAuthorId() === null) {
return null;
Expand Down
20 changes: 12 additions & 8 deletions src/Client/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,45 @@ public function __construct(
}

/**
* Get the user's projects
* Fetch the user's projects
* This sends a request to the Modrinth API every time it is called.
* @return Project[]
* @throws ApiException
*/
public function getProjects(): array
public function fetchProjects(): array
{
return $this->client->getUserProjects($this->getId());
}

/**
* Get the user's notifications (requires authentication)
* Fetch the user's notifications (requires authentication)
* This sends a request to the Modrinth API every time it is called.
* @return Notification[]
* @throws ApiException
*/
public function getNotifications(): array
public function fetchNotifications(): array
{
return $this->client->getUserNotifications($this->getId());
}

/**
* Get the user projects this user follows (requires authentication)
* Fetch the projects this user follows (requires authentication)
* This sends a request to the Modrinth API every time it is called.
* @return Project[]
* @throws ApiException
*/
public function getFollowedProjects(): array
public function fetchFollowedProjects(): array
{
return $this->client->getFollowedProjects($this->getId());
}

/**
* Get the user's payout history (requires authentication)
* Fetch the user's payout history (requires authentication)
* This sends a request to the Modrinth API every time it is called.
* @return UserPayoutHistory
* @throws ApiException
*/
public function getPayoutHistory(): UserPayoutHistory
public function fetchPayoutHistory(): UserPayoutHistory
{
return $this->client->getPayoutHistory($this->getId());
}
Expand Down
10 changes: 6 additions & 4 deletions src/Client/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ public function __construct(
}

/**
* Fetch the full project from the API
* Fetch the project of this version
* This sends a request to the Modrinth API every time it is called.
* @return Project
* @throws ApiException
*/
public function getProject(): Project
public function fetchProject(): Project
{
return $this->client->getProject($this->getProjectId());
}

/**
* Fetch the user that published this version from the API
* Fetch the user that published this version
* This sends a request to the Modrinth API every time it is called.
* @return User
* @throws ApiException
*/
public function getAuthor(): User
public function fetchAuthor(): User
{
return $this->client->getUser($this->getAuthorId());
}
Expand Down
Loading
Loading