From 32211a7f780a481a520a8cf947cfabfc0dc70a1c Mon Sep 17 00:00:00 2001 From: labile <1a6i1e@gmail.com> Date: Thu, 2 Sep 2021 01:00:34 +0300 Subject: [PATCH] refactoring --- src/DataFetcher.php | 20 -------------------- src/Events/MessageEvent.php | 5 ++++- src/Events/MessageNew.php | 5 ++++- src/Events/WallPostNew.php | 5 ++++- src/Sort.php | 30 ++++++++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 src/Sort.php diff --git a/src/DataFetcher.php b/src/DataFetcher.php index 8318319..4b48047 100644 --- a/src/DataFetcher.php +++ b/src/DataFetcher.php @@ -21,7 +21,6 @@ class DataFetcher private string $type; private int $group_id; private string $event_id; - protected \Closure $callable_sort; /** @@ -58,25 +57,6 @@ public function getEventId(): string public function __construct(private ?object $data = null) { - $this->callable_sort = fn($data) => array_walk($data, function ($value, $property) { - if (property_exists($this, $property)) { - - if ($property === "peer_id" && $value - 2000000000 > 0) { - $this->chat_id = $value - 2000000000; - } - - if ($property === "payload") { - $this->payload = match (gettype($value)) { - "object" => (array) $value, - "string" => @json_decode($value, true), - "array" => $value, - }; - } else { - $this->$property = $value; - } - } - }); - if ($data?->type === "message_new") { $this->client_info = $data?->object->client_info; } diff --git a/src/Events/MessageEvent.php b/src/Events/MessageEvent.php index 82657a8..135aecb 100644 --- a/src/Events/MessageEvent.php +++ b/src/Events/MessageEvent.php @@ -10,6 +10,7 @@ namespace Astaroth\DataFetcher\Events; use Astaroth\DataFetcher\DataFetcher; +use Astaroth\DataFetcher\Sort; /** * Class MessageEvent @@ -18,6 +19,8 @@ */ final class MessageEvent extends DataFetcher { + use Sort; + private int $peer_id; private int $user_id; private int $conversation_message_id; @@ -29,7 +32,7 @@ final class MessageEvent extends DataFetcher public function __construct(?object $data = null) { parent::__construct($data); - ($this->callable_sort)($data?->object); + $this->sort($data?->object); } /** diff --git a/src/Events/MessageNew.php b/src/Events/MessageNew.php index 1fcb828..add6836 100644 --- a/src/Events/MessageNew.php +++ b/src/Events/MessageNew.php @@ -10,6 +10,7 @@ use Astaroth\DataFetcher\DataFetcher; +use Astaroth\DataFetcher\Sort; /** * Class MessageNew @@ -19,6 +20,8 @@ */ final class MessageNew extends DataFetcher { + use Sort; + private int $id; private int $out; private int $date; @@ -51,7 +54,7 @@ final class MessageNew extends DataFetcher public function __construct(?object $data = null) { parent::__construct($data); - ($this->callable_sort)($data?->object->message); + $this->sort($data?->object->message); } /** diff --git a/src/Events/WallPostNew.php b/src/Events/WallPostNew.php index 3748dd7..a9da164 100644 --- a/src/Events/WallPostNew.php +++ b/src/Events/WallPostNew.php @@ -8,6 +8,7 @@ namespace Astaroth\DataFetcher\Events; use Astaroth\DataFetcher\DataFetcher; +use Astaroth\DataFetcher\Sort; /** * Class WallPostNew @@ -15,6 +16,8 @@ */ final class WallPostNew extends DataFetcher { + use Sort; + private int $id; private int $from_id; private int $owner_id; @@ -33,7 +36,7 @@ final class WallPostNew extends DataFetcher public function __construct(?object $data = null) { parent::__construct($data); - ($this->callable_sort)($data?->object->message); + $this->sort($data?->object); } /** diff --git a/src/Sort.php b/src/Sort.php new file mode 100644 index 0000000..68a7dcb --- /dev/null +++ b/src/Sort.php @@ -0,0 +1,30 @@ + 0) { + $this->chat_id = $value - 2000000000; + } + + if ($property === "payload") { + $this->payload = match (gettype($value)) { + "object" => (array)$value, + "string" => @json_decode($value, true), + "array" => $value, + }; + } else { + $this->$property = $value; + } + } + }); + } +} \ No newline at end of file