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.
- Loading branch information
Showing
1 changed file
with
58 additions
and
61 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,76 @@ | ||
<?php | ||
|
||
namespace FortniteApi\Components\Objects\Reflection; | ||
namespace MichelPi\FortniteApi\Components\Objects; | ||
|
||
use FortniteApi\Components\JsonSerializer; | ||
use Exception; | ||
use MichelPi\FortniteApi\Components\Objects\Reflection\Activator; | ||
|
||
class Activator | ||
class Variant | ||
{ | ||
private $activator; | ||
private $initializer; | ||
|
||
public function __construct($activator, $initializer) | ||
/** | ||
* Undocumented variable | ||
* | ||
* @var Activator | ||
*/ | ||
private static $_activator; | ||
|
||
/** | ||
* Undocumented variable | ||
* | ||
* @var null|string | ||
*/ | ||
public $type; | ||
/** | ||
* Undocumented variable | ||
* | ||
* @var null|Option[]|array | ||
*/ | ||
public $options; | ||
|
||
public static function createObject($body) | ||
{ | ||
$this->activator = $activator; | ||
$this->initializer = $initializer; | ||
return self::getActivator()->createObjectFromBody($body); | ||
} | ||
|
||
public function createObjectFromBody($body) | ||
public static function createObjectArray($body) | ||
{ | ||
if (empty($body)) { | ||
return null; | ||
} | ||
|
||
if (is_string($body)) { | ||
$body = JsonSerializer::deserialize($body); | ||
|
||
if ($body === false) { | ||
return null; | ||
} | ||
} | ||
|
||
if (array_key_exists("status", $body) && array_key_exists("data", $body)) { | ||
$body = $body["data"]; | ||
} | ||
|
||
$obj = call_user_func($this->activator); | ||
|
||
if (call_user_func_array($this->initializer, array(&$obj, &$body))) { | ||
return $obj; | ||
} else { | ||
return null; | ||
} | ||
return self::getActivator()->createArrayFromBody($body); | ||
} | ||
|
||
public function createArrayFromBody($body) | ||
/** | ||
* Undocumented function | ||
* | ||
* @param Variant $obj | ||
* @param array|mixed $body | ||
* @return bool | ||
*/ | ||
private static function initializeObject(&$obj, &$body) | ||
{ | ||
if (empty($body)) { | ||
return null; | ||
try { | ||
$obj->type = $body["type"]; | ||
$obj->options = Option::createObjectArray($body["options"]); | ||
|
||
return true; | ||
} catch (Exception $ex) { | ||
return false; | ||
} | ||
} | ||
|
||
if (is_string($body)) { | ||
$body = JsonSerializer::deserialize($body); | ||
|
||
if ($body === false) { | ||
return null; | ||
} | ||
} | ||
|
||
if (array_key_exists("status", $body) && array_key_exists("data", $body)) { | ||
$body = $body["data"]; | ||
} | ||
|
||
$result = []; | ||
|
||
foreach ($body as $item) { | ||
$obj = call_user_func($this->activator); | ||
|
||
if (call_user_func_array($this->initializer, array(&$obj, &$item))) { | ||
$result[] = $obj; | ||
} | ||
/** | ||
* Undocumented function | ||
* | ||
* @return Activator | ||
*/ | ||
private static function getActivator() | ||
{ | ||
if (empty(self::$_activator)) { | ||
self::$_activator = new Activator(function () { | ||
return new Variant(); | ||
}, function (&$obj, &$body) { | ||
return self::initializeObject($obj, $body); | ||
}); | ||
} | ||
|
||
if (count($result) == 0) { | ||
return null; | ||
} else { | ||
return $result; | ||
} | ||
return self::$_activator; | ||
} | ||
} |