This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Fortnite-API/creator-codes
Adds the creatorcodes endpoint
- Loading branch information
Showing
7 changed files
with
374 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
namespace FortniteApi\Components\Endpoints; | ||
|
||
use FortniteApi\Components\HttpClient; | ||
use FortniteApi\Components\Tasks\CreatorCodeArrayTask; | ||
use FortniteApi\Components\Tasks\CreatorCodeTask; | ||
use FortniteApi\FortniteApiError; | ||
|
||
/** | ||
* Provides access to the /creatorcode endpoint. | ||
*/ | ||
class CreatorCodeEndpoint | ||
{ | ||
/** | ||
* Returns the creator code data for a given slug. | ||
* | ||
* @param string|array|mixed $slug | ||
* @return null|CreatorCode | ||
*/ | ||
public function get($slug) | ||
{ | ||
$promise = $this->getAsync($slug); | ||
|
||
if ($promise == null) { | ||
return null; | ||
} else { | ||
return $promise->await(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the creator code data for a given slug. | ||
* | ||
* @param string|array|mixed $slug | ||
* @return null|CreatorCodeTask | ||
*/ | ||
public function getAsync($slug) | ||
{ | ||
FortniteApiError::clearLastError(); | ||
|
||
if (empty($slug)) { | ||
FortniteApiError::setLastError("Missing paramter 'slug'."); | ||
|
||
return null; | ||
} | ||
|
||
$path = "/creatorcode"; | ||
|
||
$query = [ | ||
"slug" => $slug | ||
]; | ||
|
||
$promise = HttpClient::getInstance()->getAsync($path, [ | ||
"query" => $query | ||
]); | ||
|
||
return new CreatorCodeTask($promise); | ||
} | ||
|
||
/** | ||
* Returns the first creator code matching the given slug. | ||
* | ||
* @param string|array|mixed $slug | ||
* @return null|CreatorCode | ||
*/ | ||
public function search($slug) | ||
{ | ||
$promise = $this->searchAsync($slug); | ||
|
||
if ($promise == null) { | ||
return null; | ||
} else { | ||
return $promise->await(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the first creator code matching the given slug. | ||
* | ||
* @param string|array|mixed $slug | ||
* @return null|CreatorCodeTask | ||
*/ | ||
public function searchAsync($slug) | ||
{ | ||
FortniteApiError::clearLastError(); | ||
|
||
if (empty($slug)) { | ||
FortniteApiError::setLastError("Missing paramter 'slug'."); | ||
|
||
return null; | ||
} | ||
|
||
$path = "/creatorcode/search"; | ||
|
||
$query = [ | ||
"slug" => $slug | ||
]; | ||
|
||
$promise = HttpClient::getInstance()->getAsync($path, [ | ||
"query" => $query | ||
]); | ||
|
||
return new CreatorCodeTask($promise); | ||
} | ||
|
||
/** | ||
* Returns the all creator codes matching the given slug. | ||
* | ||
* @param string|array|mixed $slug | ||
* @return null|CreatorCode[]|array|mixed | ||
*/ | ||
public function searchAll($slug) | ||
{ | ||
$promise = $this->searchAllAsync($slug); | ||
|
||
if ($promise == null) { | ||
return null; | ||
} else { | ||
return $promise->await(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the all creator codes matching the given slug. | ||
* | ||
* @param string|array|mixed $slug | ||
* @return null|CreatorCodeArrayTask | ||
*/ | ||
public function searchAllAsync($slug) | ||
{ | ||
FortniteApiError::clearLastError(); | ||
|
||
if (empty($slug)) { | ||
FortniteApiError::setLastError("Missing paramter 'slug'."); | ||
|
||
return null; | ||
} | ||
|
||
$path = "/creatorcode/search/all"; | ||
|
||
$query = [ | ||
"slug" => $slug | ||
]; | ||
|
||
$promise = HttpClient::getInstance()->getAsync($path, [ | ||
"query" => $query | ||
]); | ||
|
||
return new CreatorCodeArrayTask($promise); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace FortniteApi\Components\Objects; | ||
|
||
use Exception; | ||
use FortniteApi\Components\Objects\Reflection\Activator; | ||
|
||
class CreatorCode | ||
{ | ||
/** | ||
* Undocumented variable | ||
* | ||
* @var Activator | ||
*/ | ||
private static $_activator; | ||
|
||
/** | ||
* Undocumented variable | ||
* | ||
* @var string | ||
*/ | ||
public $id; | ||
/** | ||
* Undocumented variable | ||
* | ||
* @var string | ||
*/ | ||
public $slug; | ||
|
||
/** | ||
* Undocumented variable | ||
* | ||
* @var string | ||
*/ | ||
public $displayName; | ||
|
||
/** | ||
* Undocumented variable | ||
* | ||
* @var null|string | ||
*/ | ||
public $status; | ||
|
||
/** | ||
* Undocumented variable | ||
* | ||
* @var bool | ||
*/ | ||
public $verified; | ||
|
||
public static function createObject($body) | ||
{ | ||
return self::getActivator()->createObjectFromBody($body); | ||
} | ||
|
||
public static function createObjectArray($body) | ||
{ | ||
return self::getActivator()->createArrayFromBody($body); | ||
} | ||
|
||
/** | ||
* Undocumented function | ||
* | ||
* @param CreatorCode $obj | ||
* @param array|mixed $body | ||
* @return bool | ||
*/ | ||
private static function initializeObject(&$obj, &$body) | ||
{ | ||
try { | ||
$obj->id = $body["id"]; | ||
$obj->slug = $body["slug"]; | ||
$obj->displayName = $body["displayName"]; | ||
$obj->status = $body["status"]; | ||
$obj->verified = $body["verified"]; | ||
|
||
return true; | ||
} catch (Exception $ex) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Undocumented function | ||
* | ||
* @return Activator | ||
*/ | ||
private static function getActivator() | ||
{ | ||
if (empty(self::$_activator)) { | ||
self::$_activator = new Activator(function () { | ||
return new CreatorCode(); | ||
}, function (&$obj, &$body) { | ||
return self::initializeObject($obj, $body); | ||
}); | ||
} | ||
|
||
return self::$_activator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace FortniteApi\Components\Tasks; | ||
|
||
use Exception; | ||
use FortniteApi\Components\HttpClient; | ||
use FortniteApi\Components\Objects\CreatorCode; | ||
use FortniteApi\FortniteApiError; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
|
||
class CreatorCodeArrayTask extends Awaitable | ||
{ | ||
/** | ||
* Awaits the response and returns the parsed body. | ||
* | ||
* @return null|CreatorCode[]|array | ||
*/ | ||
public function await() | ||
{ | ||
FortniteApiError::clearLastError(); | ||
|
||
try { | ||
/** @var Response $response */ | ||
$response = parent::await(); | ||
|
||
if (empty($response)) { | ||
return null; | ||
} | ||
|
||
$statusCode = $response->getStatusCode(); | ||
|
||
if (!HttpClient::isSuccess($statusCode)) { | ||
FortniteApiError::setLastError("Request failed.", $response); | ||
|
||
return null; | ||
} | ||
|
||
$body = $response->getBody(); | ||
|
||
if (empty($body)) { | ||
return CreatorCode::createObjectArray(null); | ||
} | ||
|
||
$text = (string)$body; | ||
|
||
return CreatorCode::createObjectArray($text); | ||
} catch (Exception $ex) { | ||
FortniteApiError::setLastError($ex->getMessage()); | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace FortniteApi\Components\Tasks; | ||
|
||
use Exception; | ||
use FortniteApi\Components\HttpClient; | ||
use FortniteApi\Components\Objects\CreatorCode; | ||
use FortniteApi\FortniteApiError; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
|
||
class CreatorCodeTask extends Awaitable | ||
{ | ||
/** | ||
* Awaits the response and returns the parsed body. | ||
* | ||
* @return null|CreatorCode | ||
*/ | ||
public function await() | ||
{ | ||
FortniteApiError::clearLastError(); | ||
|
||
try { | ||
/** @var Response $response */ | ||
$response = parent::await(); | ||
|
||
if (empty($response)) { | ||
return null; | ||
} | ||
|
||
$statusCode = $response->getStatusCode(); | ||
|
||
if (!HttpClient::isSuccess($statusCode)) { | ||
FortniteApiError::setLastError("Request failed.", $response); | ||
|
||
return null; | ||
} | ||
|
||
$body = $response->getBody(); | ||
|
||
if (empty($body)) { | ||
return CreatorCode::createObject(null); | ||
} | ||
|
||
$text = (string)$body; | ||
|
||
return CreatorCode::createObject($text); | ||
} catch (Exception $ex) { | ||
FortniteApiError::setLastError($ex->getMessage()); | ||
|
||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.