|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -namespace FortniteApi\Components\Objects\Reflection; |
| 3 | +namespace MichelPi\FortniteApi\Components\Objects; |
4 | 4 |
|
5 |
| -use FortniteApi\Components\JsonSerializer; |
| 5 | +use Exception; |
| 6 | +use MichelPi\FortniteApi\Components\Objects\Reflection\Activator; |
6 | 7 |
|
7 |
| -class Activator |
| 8 | +class Variant |
8 | 9 | {
|
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) |
13 | 31 | {
|
14 |
| - $this->activator = $activator; |
15 |
| - $this->initializer = $initializer; |
| 32 | + return self::getActivator()->createObjectFromBody($body); |
16 | 33 | }
|
17 | 34 |
|
18 |
| - public function createObjectFromBody($body) |
| 35 | + public static function createObjectArray($body) |
19 | 36 | {
|
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); |
43 | 38 | }
|
44 | 39 |
|
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) |
46 | 48 | {
|
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; |
49 | 56 | }
|
| 57 | + } |
50 | 58 |
|
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 | + }); |
71 | 72 | }
|
72 | 73 |
|
73 |
| - if (count($result) == 0) { |
74 |
| - return null; |
75 |
| - } else { |
76 |
| - return $result; |
77 |
| - } |
| 74 | + return self::$_activator; |
78 | 75 | }
|
79 | 76 | }
|
0 commit comments