Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.

Commit 8dade17

Browse files
authored
Merge pull request #1 from Fortnite-API/creator-codes
Adds the creatorcodes endpoint
2 parents 90d66bc + 0dfa651 commit 8dade17

File tree

7 files changed

+374
-1
lines changed

7 files changed

+374
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ $api->news->...
4747

4848
// accesses the shop endpoint (https://fortnite-api.com/shop)
4949
$api->shop->...
50+
51+
// accesses the creatorcode endpoint (https://fortnite-api.com/creatorcode)
52+
$api->creatorCode->...
5053
```
5154

5255
```php
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace FortniteApi\Components\Endpoints;
4+
5+
use FortniteApi\Components\HttpClient;
6+
use FortniteApi\Components\Tasks\CreatorCodeArrayTask;
7+
use FortniteApi\Components\Tasks\CreatorCodeTask;
8+
use FortniteApi\FortniteApiError;
9+
10+
/**
11+
* Provides access to the /creatorcode endpoint.
12+
*/
13+
class CreatorCodeEndpoint
14+
{
15+
/**
16+
* Returns the creator code data for a given slug.
17+
*
18+
* @param string|array|mixed $slug
19+
* @return null|CreatorCode
20+
*/
21+
public function get($slug)
22+
{
23+
$promise = $this->getAsync($slug);
24+
25+
if ($promise == null) {
26+
return null;
27+
} else {
28+
return $promise->await();
29+
}
30+
}
31+
32+
/**
33+
* Returns the creator code data for a given slug.
34+
*
35+
* @param string|array|mixed $slug
36+
* @return null|CreatorCodeTask
37+
*/
38+
public function getAsync($slug)
39+
{
40+
FortniteApiError::clearLastError();
41+
42+
if (empty($slug)) {
43+
FortniteApiError::setLastError("Missing paramter 'slug'.");
44+
45+
return null;
46+
}
47+
48+
$path = "/creatorcode";
49+
50+
$query = [
51+
"slug" => $slug
52+
];
53+
54+
$promise = HttpClient::getInstance()->getAsync($path, [
55+
"query" => $query
56+
]);
57+
58+
return new CreatorCodeTask($promise);
59+
}
60+
61+
/**
62+
* Returns the first creator code matching the given slug.
63+
*
64+
* @param string|array|mixed $slug
65+
* @return null|CreatorCode
66+
*/
67+
public function search($slug)
68+
{
69+
$promise = $this->searchAsync($slug);
70+
71+
if ($promise == null) {
72+
return null;
73+
} else {
74+
return $promise->await();
75+
}
76+
}
77+
78+
/**
79+
* Returns the first creator code matching the given slug.
80+
*
81+
* @param string|array|mixed $slug
82+
* @return null|CreatorCodeTask
83+
*/
84+
public function searchAsync($slug)
85+
{
86+
FortniteApiError::clearLastError();
87+
88+
if (empty($slug)) {
89+
FortniteApiError::setLastError("Missing paramter 'slug'.");
90+
91+
return null;
92+
}
93+
94+
$path = "/creatorcode/search";
95+
96+
$query = [
97+
"slug" => $slug
98+
];
99+
100+
$promise = HttpClient::getInstance()->getAsync($path, [
101+
"query" => $query
102+
]);
103+
104+
return new CreatorCodeTask($promise);
105+
}
106+
107+
/**
108+
* Returns the all creator codes matching the given slug.
109+
*
110+
* @param string|array|mixed $slug
111+
* @return null|CreatorCode[]|array|mixed
112+
*/
113+
public function searchAll($slug)
114+
{
115+
$promise = $this->searchAllAsync($slug);
116+
117+
if ($promise == null) {
118+
return null;
119+
} else {
120+
return $promise->await();
121+
}
122+
}
123+
124+
/**
125+
* Returns the all creator codes matching the given slug.
126+
*
127+
* @param string|array|mixed $slug
128+
* @return null|CreatorCodeArrayTask
129+
*/
130+
public function searchAllAsync($slug)
131+
{
132+
FortniteApiError::clearLastError();
133+
134+
if (empty($slug)) {
135+
FortniteApiError::setLastError("Missing paramter 'slug'.");
136+
137+
return null;
138+
}
139+
140+
$path = "/creatorcode/search/all";
141+
142+
$query = [
143+
"slug" => $slug
144+
];
145+
146+
$promise = HttpClient::getInstance()->getAsync($path, [
147+
"query" => $query
148+
]);
149+
150+
return new CreatorCodeArrayTask($promise);
151+
}
152+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace FortniteApi\Components\Objects;
4+
5+
use Exception;
6+
use FortniteApi\Components\Objects\Reflection\Activator;
7+
8+
class CreatorCode
9+
{
10+
/**
11+
* Undocumented variable
12+
*
13+
* @var Activator
14+
*/
15+
private static $_activator;
16+
17+
/**
18+
* Undocumented variable
19+
*
20+
* @var string
21+
*/
22+
public $id;
23+
/**
24+
* Undocumented variable
25+
*
26+
* @var string
27+
*/
28+
public $slug;
29+
30+
/**
31+
* Undocumented variable
32+
*
33+
* @var string
34+
*/
35+
public $displayName;
36+
37+
/**
38+
* Undocumented variable
39+
*
40+
* @var null|string
41+
*/
42+
public $status;
43+
44+
/**
45+
* Undocumented variable
46+
*
47+
* @var bool
48+
*/
49+
public $verified;
50+
51+
public static function createObject($body)
52+
{
53+
return self::getActivator()->createObjectFromBody($body);
54+
}
55+
56+
public static function createObjectArray($body)
57+
{
58+
return self::getActivator()->createArrayFromBody($body);
59+
}
60+
61+
/**
62+
* Undocumented function
63+
*
64+
* @param CreatorCode $obj
65+
* @param array|mixed $body
66+
* @return bool
67+
*/
68+
private static function initializeObject(&$obj, &$body)
69+
{
70+
try {
71+
$obj->id = $body["id"];
72+
$obj->slug = $body["slug"];
73+
$obj->displayName = $body["displayName"];
74+
$obj->status = $body["status"];
75+
$obj->verified = $body["verified"];
76+
77+
return true;
78+
} catch (Exception $ex) {
79+
return false;
80+
}
81+
}
82+
83+
/**
84+
* Undocumented function
85+
*
86+
* @return Activator
87+
*/
88+
private static function getActivator()
89+
{
90+
if (empty(self::$_activator)) {
91+
self::$_activator = new Activator(function () {
92+
return new CreatorCode();
93+
}, function (&$obj, &$body) {
94+
return self::initializeObject($obj, $body);
95+
});
96+
}
97+
98+
return self::$_activator;
99+
}
100+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace FortniteApi\Components\Tasks;
4+
5+
use Exception;
6+
use FortniteApi\Components\HttpClient;
7+
use FortniteApi\Components\Objects\CreatorCode;
8+
use FortniteApi\FortniteApiError;
9+
use Psr\Http\Message\ResponseInterface as Response;
10+
11+
class CreatorCodeArrayTask extends Awaitable
12+
{
13+
/**
14+
* Awaits the response and returns the parsed body.
15+
*
16+
* @return null|CreatorCode[]|array
17+
*/
18+
public function await()
19+
{
20+
FortniteApiError::clearLastError();
21+
22+
try {
23+
/** @var Response $response */
24+
$response = parent::await();
25+
26+
if (empty($response)) {
27+
return null;
28+
}
29+
30+
$statusCode = $response->getStatusCode();
31+
32+
if (!HttpClient::isSuccess($statusCode)) {
33+
FortniteApiError::setLastError("Request failed.", $response);
34+
35+
return null;
36+
}
37+
38+
$body = $response->getBody();
39+
40+
if (empty($body)) {
41+
return CreatorCode::createObjectArray(null);
42+
}
43+
44+
$text = (string)$body;
45+
46+
return CreatorCode::createObjectArray($text);
47+
} catch (Exception $ex) {
48+
FortniteApiError::setLastError($ex->getMessage());
49+
50+
return null;
51+
}
52+
}
53+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace FortniteApi\Components\Tasks;
4+
5+
use Exception;
6+
use FortniteApi\Components\HttpClient;
7+
use FortniteApi\Components\Objects\CreatorCode;
8+
use FortniteApi\FortniteApiError;
9+
use Psr\Http\Message\ResponseInterface as Response;
10+
11+
class CreatorCodeTask extends Awaitable
12+
{
13+
/**
14+
* Awaits the response and returns the parsed body.
15+
*
16+
* @return null|CreatorCode
17+
*/
18+
public function await()
19+
{
20+
FortniteApiError::clearLastError();
21+
22+
try {
23+
/** @var Response $response */
24+
$response = parent::await();
25+
26+
if (empty($response)) {
27+
return null;
28+
}
29+
30+
$statusCode = $response->getStatusCode();
31+
32+
if (!HttpClient::isSuccess($statusCode)) {
33+
FortniteApiError::setLastError("Request failed.", $response);
34+
35+
return null;
36+
}
37+
38+
$body = $response->getBody();
39+
40+
if (empty($body)) {
41+
return CreatorCode::createObject(null);
42+
}
43+
44+
$text = (string)$body;
45+
46+
return CreatorCode::createObject($text);
47+
} catch (Exception $ex) {
48+
FortniteApiError::setLastError($ex->getMessage());
49+
50+
return null;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)