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

Commit f426acf

Browse files
committed
???
1 parent a441c00 commit f426acf

File tree

1 file changed

+58
-61
lines changed

1 file changed

+58
-61
lines changed

src/Components/Objects/Variant.php

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,76 @@
11
<?php
22

3-
namespace FortniteApi\Components\Objects\Reflection;
3+
namespace MichelPi\FortniteApi\Components\Objects;
44

5-
use FortniteApi\Components\JsonSerializer;
5+
use Exception;
6+
use MichelPi\FortniteApi\Components\Objects\Reflection\Activator;
67

7-
class Activator
8+
class Variant
89
{
9-
private $activator;
10-
private $initializer;
11-
12-
public function __construct($activator, $initializer)
10+
/**
11+
* Undocumented variable
12+
*
13+
* @var Activator
14+
*/
15+
private static $_activator;
16+
17+
/**
18+
* Undocumented variable
19+
*
20+
* @var null|string
21+
*/
22+
public $type;
23+
/**
24+
* Undocumented variable
25+
*
26+
* @var null|Option[]|array
27+
*/
28+
public $options;
29+
30+
public static function createObject($body)
1331
{
14-
$this->activator = $activator;
15-
$this->initializer = $initializer;
32+
return self::getActivator()->createObjectFromBody($body);
1633
}
1734

18-
public function createObjectFromBody($body)
35+
public static function createObjectArray($body)
1936
{
20-
if (empty($body)) {
21-
return null;
22-
}
23-
24-
if (is_string($body)) {
25-
$body = JsonSerializer::deserialize($body);
26-
27-
if ($body === false) {
28-
return null;
29-
}
30-
}
31-
32-
if (array_key_exists("status", $body) && array_key_exists("data", $body)) {
33-
$body = $body["data"];
34-
}
35-
36-
$obj = call_user_func($this->activator);
37-
38-
if (call_user_func_array($this->initializer, array(&$obj, &$body))) {
39-
return $obj;
40-
} else {
41-
return null;
42-
}
37+
return self::getActivator()->createArrayFromBody($body);
4338
}
4439

45-
public function createArrayFromBody($body)
40+
/**
41+
* Undocumented function
42+
*
43+
* @param Variant $obj
44+
* @param array|mixed $body
45+
* @return bool
46+
*/
47+
private static function initializeObject(&$obj, &$body)
4648
{
47-
if (empty($body)) {
48-
return null;
49+
try {
50+
$obj->type = $body["type"];
51+
$obj->options = Option::createObjectArray($body["options"]);
52+
53+
return true;
54+
} catch (Exception $ex) {
55+
return false;
4956
}
57+
}
5058

51-
if (is_string($body)) {
52-
$body = JsonSerializer::deserialize($body);
53-
54-
if ($body === false) {
55-
return null;
56-
}
57-
}
58-
59-
if (array_key_exists("status", $body) && array_key_exists("data", $body)) {
60-
$body = $body["data"];
61-
}
62-
63-
$result = [];
64-
65-
foreach ($body as $item) {
66-
$obj = call_user_func($this->activator);
67-
68-
if (call_user_func_array($this->initializer, array(&$obj, &$item))) {
69-
$result[] = $obj;
70-
}
59+
/**
60+
* Undocumented function
61+
*
62+
* @return Activator
63+
*/
64+
private static function getActivator()
65+
{
66+
if (empty(self::$_activator)) {
67+
self::$_activator = new Activator(function () {
68+
return new Variant();
69+
}, function (&$obj, &$body) {
70+
return self::initializeObject($obj, $body);
71+
});
7172
}
7273

73-
if (count($result) == 0) {
74-
return null;
75-
} else {
76-
return $result;
77-
}
74+
return self::$_activator;
7875
}
7976
}

0 commit comments

Comments
 (0)