Skip to content

Commit dea7349

Browse files
committed
Use new list filters in ModSearchoptions, add new fields
1 parent cbafc6c commit dea7349

File tree

7 files changed

+322
-147
lines changed

7 files changed

+322
-147
lines changed

lib/Api/ModsApi.php

+57-66
Large diffs are not rendered by default.

lib/Client/Category.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ public function getMods(?ModSearchOptions $options = null): PaginatedModList
3434
{
3535
if ($options === null) {
3636
$options = new ModSearchOptions($this->category->getGameId());
37-
if ($this->category->getIsClass()) {
38-
$options->setClassId($this->category->getId());
39-
} else {
40-
$options->setCategoryId($this->category->getId());
41-
}
37+
}
38+
39+
if ($this->category->getIsClass()) {
40+
$options->setClassId($this->category->getId());
41+
} else {
42+
$options->setCategoryIds([$this->category->getId()]);
4243
}
4344

4445
return $this->client->searchMods($options);
@@ -53,4 +54,4 @@ public function getGame(): Game
5354
{
5455
return $this->client->getGame($this->category->getGameId());
5556
}
56-
}
57+
}

lib/Client/CurseForgeAPIClient.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,20 @@ public function searchMods(ModSearchOptions $options): PaginatedModList
236236
return new PaginatedModList($this, $this->mods->searchMods(
237237
$options->getGameId(),
238238
$options->getClassId(),
239-
$options->getCategoryId(),
240-
$options->getGameVersion(),
239+
null,
240+
null,
241241
$options->getSearchFilter(),
242242
$options->getSortField()?->value,
243243
$options->getSortOrder()?->value,
244-
$options->getModLoaderType()?->value,
244+
null,
245245
$options->getGameVersionTypeId(),
246246
$options->getAuthorId(),
247247
$options->getSlug(),
248+
$options->getEncodedCategoryIds(),
249+
$options->getEncodedGameVersions(),
250+
$options->getEncodedModLoaderTypes(),
251+
$options->getPrimaryAuthorId(),
252+
$options->getPremiumType(),
248253
$options->getOffset(),
249254
PaginatedModList::getAllowedPageSize($options->getOffset(), $options->getPageSize()),
250255
), $options);

lib/Client/Options/ModSearch/ModSearchOptions.php

+178-24
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,33 @@ class ModSearchOptions
99
/**
1010
* @param int $gameId
1111
* @param int|null $classId
12-
* @param int|null $categoryId
13-
* @param string|null $gameVersion
12+
* @param int[]|null $categoryIds
13+
* @param string[]|null $gameVersions
1414
* @param string|null $searchFilter Filter by free text search in the mod name and author
1515
* @param ModSearchSortField|null $sortField
1616
* @param SortOrder|null $sortOrder
17-
* @param ModLoaderType|null $modLoaderType Filter only mods associated to a given modloader (Forge, Fabric ...). Must be coupled with gameVersion.
17+
* @param ModLoaderType[]|null $modLoaderTypes Filter only mods associated to a given modloader (Forge, Fabric ...). Must be coupled with gameVersion.
1818
* @param int|null $gameVersionTypeId Filter only mods that contain files tagged with versions of the given gameVersionTypeId
19-
* @param int|null $authorId
19+
* @param int|null $authorId Filter only mods that the given authorId is a member of
20+
* @param int|null $primaryAuthorId Filter only mods that have the given author as primary author
21+
* @param PremiumType|null $premiumType Filter only mods that are Premium or not
2022
* @param string|null $slug Filter by slug (coupled with classId will result in a unique result).
2123
* @param int $offset
2224
* @param int $pageSize
2325
*/
2426
public function __construct(
2527
protected int $gameId,
2628
protected ?int $classId = null,
27-
protected ?int $categoryId = null,
28-
protected ?string $gameVersion = null,
29+
protected ?array $categoryIds = null,
30+
protected ?array $gameVersions = null,
2931
protected ?string $searchFilter = null,
3032
protected ?ModSearchSortField $sortField = null,
3133
protected ?SortOrder $sortOrder = null,
32-
protected ?ModLoaderType $modLoaderType = null,
34+
protected ?array $modLoaderTypes = null,
3335
protected ?int $gameVersionTypeId = null,
3436
protected ?int $authorId = null,
37+
protected ?int $primaryAuthorId = null,
38+
protected ?PremiumType $premiumType = null,
3539
protected ?string $slug = null,
3640
protected int $offset = 0,
3741
protected int $pageSize = PaginatedModList::MAX_PAGE_SIZE,
@@ -76,38 +80,111 @@ public function setClassId(?int $classId): static
7680
}
7781

7882
/**
79-
* @return int|null
83+
* @return int[]|null
84+
*/
85+
public function getCategoryIds(): ?array
86+
{
87+
return $this->categoryIds;
88+
}
89+
90+
/**
91+
* Get the categories encoded for the API request
92+
* @return string|null
8093
*/
81-
public function getCategoryId(): ?int
94+
public function getEncodedCategoryIds(): ?string
8295
{
83-
return $this->categoryId;
96+
if ($this->categoryIds === null) {
97+
return null;
98+
}
99+
100+
return json_encode($this->categoryIds);
101+
}
102+
103+
/**
104+
* Set the category IDs to filter the search results
105+
* @param int[]|null $categoryIds
106+
* @return $this
107+
*/
108+
public function setCategoryIds(?array $categoryIds): static
109+
{
110+
$this->categoryIds = $categoryIds;
111+
return $this;
112+
}
113+
114+
/**
115+
* Add a category ID to filter the search results
116+
* @param int $categoryId
117+
* @return $this
118+
*/
119+
public function addCategoryId(int $categoryId): static
120+
{
121+
$this->categoryIds ??= [];
122+
$this->categoryIds[] = $categoryId;
123+
return $this;
84124
}
85125

86126
/**
87-
* @param int|null $categoryId
127+
* Remove a category ID from the filter
128+
* @param int $categoryId
88129
* @return $this
89130
*/
90-
public function setCategoryId(?int $categoryId): static
131+
public function removeCategoryId(int $categoryId): static
91132
{
92-
$this->categoryId = $categoryId;
133+
$this->categoryIds = array_filter($this->categoryIds, fn($value) => $value !== $categoryId);
93134
return $this;
94135
}
95136

96137
/**
138+
* @return string[]|null
139+
*/
140+
public function getGameVersions(): ?array
141+
{
142+
return $this->gameVersions;
143+
}
144+
145+
/**
146+
* Get the game versions encoded for the API request
97147
* @return string|null
98148
*/
99-
public function getGameVersion(): ?string
149+
public function getEncodedGameVersions(): ?string
100150
{
101-
return $this->gameVersion;
151+
if ($this->gameVersions === null) {
152+
return null;
153+
}
154+
155+
return json_encode($this->gameVersions);
102156
}
103157

104158
/**
105-
* @param string|null $gameVersion
159+
* @param string[]|null $gameVersions
106160
* @return $this
107161
*/
108-
public function setGameVersion(?string $gameVersion): static
162+
public function setGameVersions(?array $gameVersions): static
109163
{
110-
$this->gameVersion = $gameVersion;
164+
$this->gameVersions = $gameVersions;
165+
return $this;
166+
}
167+
168+
/**
169+
* Add a game version to the filter
170+
* @param string $gameVersion
171+
* @return $this
172+
*/
173+
public function addGameVersion(string $gameVersion): static
174+
{
175+
$this->gameVersions ??= [];
176+
$this->gameVersions[] = $gameVersion;
177+
return $this;
178+
}
179+
180+
/**
181+
* Remove a game version from the filter
182+
* @param string $gameVersion
183+
* @return $this
184+
*/
185+
public function removeGameVersion(string $gameVersion): static
186+
{
187+
$this->gameVersions = array_filter($this->gameVersions, fn($value) => $value !== $gameVersion);
111188
return $this;
112189
}
113190

@@ -166,20 +243,61 @@ public function setSortOrder(?SortOrder $sortOrder): static
166243
}
167244

168245
/**
169-
* @return ModLoaderType|null
246+
* @return ModLoaderType[]|null
170247
*/
171-
public function getModLoaderType(): ?ModLoaderType
248+
public function getModLoaderTypes(): ?array
172249
{
173-
return $this->modLoaderType;
250+
return $this->modLoaderTypes;
174251
}
175252

176253
/**
177-
* @param ModLoaderType|null $modLoaderType
254+
* @return string|null
255+
*/
256+
public function getEncodedModLoaderTypes(): ?string
257+
{
258+
if ($this->modLoaderTypes === null) {
259+
return null;
260+
}
261+
262+
$result = [];
263+
264+
foreach ($this->modLoaderTypes as $modLoaderType) {
265+
$result[] = $modLoaderType->value;
266+
}
267+
268+
return json_encode($result);
269+
}
270+
271+
/**
272+
* @param ModLoaderType[]|null $modLoaderTypes
178273
* @return $this
179274
*/
180-
public function setModLoaderType(?ModLoaderType $modLoaderType): static
275+
public function setModLoaderTypes(?array $modLoaderTypes): static
181276
{
182-
$this->modLoaderType = $modLoaderType;
277+
$this->modLoaderTypes = $modLoaderTypes;
278+
return $this;
279+
}
280+
281+
/**
282+
* Add a mod loader type to the filter
283+
* @param ModLoaderType $modLoaderType
284+
* @return $this
285+
*/
286+
public function addModLoaderType(ModLoaderType $modLoaderType): static
287+
{
288+
$this->modLoaderTypes ??= [];
289+
$this->modLoaderTypes[] = $modLoaderType;
290+
return $this;
291+
}
292+
293+
/**
294+
* Remove a mod loader type from the filter
295+
* @param ModLoaderType $modLoaderType
296+
* @return $this
297+
*/
298+
public function removeModLoaderType(ModLoaderType $modLoaderType): static
299+
{
300+
$this->modLoaderTypes = array_filter($this->modLoaderTypes, fn($value) => $value !== $modLoaderType);
183301
return $this;
184302
}
185303

@@ -219,6 +337,42 @@ public function setAuthorId(?int $authorId): static
219337
return $this;
220338
}
221339

340+
/**
341+
* @return int|null
342+
*/
343+
public function getPrimaryAuthorId(): ?int
344+
{
345+
return $this->primaryAuthorId;
346+
}
347+
348+
/**
349+
* @param int|null $primaryAuthorId
350+
* @return $this
351+
*/
352+
public function setPrimaryAuthorId(?int $primaryAuthorId): static
353+
{
354+
$this->primaryAuthorId = $primaryAuthorId;
355+
return $this;
356+
}
357+
358+
/**
359+
* @return PremiumType|null
360+
*/
361+
public function getPremiumType(): ?PremiumType
362+
{
363+
return $this->premiumType;
364+
}
365+
366+
/**
367+
* @param PremiumType|null $premiumType
368+
* @return $this
369+
*/
370+
public function setPremiumType(?PremiumType $premiumType): static
371+
{
372+
$this->premiumType = $premiumType;
373+
return $this;
374+
}
375+
222376
/**
223377
* @return string|null
224378
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Aternos\CurseForgeApi\Client\Options\ModSearch;
4+
5+
enum PremiumType: int
6+
{
7+
case All = 0;
8+
case Premium = 1;
9+
case Free = 2;
10+
}

0 commit comments

Comments
 (0)