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

Commit

Permalink
Merge pull request #1 from Fortnite-API/creator-codes
Browse files Browse the repository at this point in the history
Adds the creatorcodes endpoint
  • Loading branch information
michel-pi authored Dec 3, 2019
2 parents 90d66bc + 0dfa651 commit 8dade17
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ $api->news->...

// accesses the shop endpoint (https://fortnite-api.com/shop)
$api->shop->...

// accesses the creatorcode endpoint (https://fortnite-api.com/creatorcode)
$api->creatorCode->...
```

```php
Expand Down
152 changes: 152 additions & 0 deletions src/Components/Endpoints/CreatorCodeEndpoint.php
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);
}
}
100 changes: 100 additions & 0 deletions src/Components/Objects/CreatorCode.php
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;
}
}
53 changes: 53 additions & 0 deletions src/Components/Tasks/CreatorCodeArrayTask.php
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;
}
}
}
53 changes: 53 additions & 0 deletions src/Components/Tasks/CreatorCodeTask.php
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;
}
}
}
Loading

0 comments on commit 8dade17

Please sign in to comment.