Skip to content

Commit 32211a7

Browse files
committed
refactoring
1 parent 5bf3694 commit 32211a7

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

src/DataFetcher.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class DataFetcher
2121
private string $type;
2222
private int $group_id;
2323
private string $event_id;
24-
protected \Closure $callable_sort;
2524

2625

2726
/**
@@ -58,25 +57,6 @@ public function getEventId(): string
5857

5958
public function __construct(private ?object $data = null)
6059
{
61-
$this->callable_sort = fn($data) => array_walk($data, function ($value, $property) {
62-
if (property_exists($this, $property)) {
63-
64-
if ($property === "peer_id" && $value - 2000000000 > 0) {
65-
$this->chat_id = $value - 2000000000;
66-
}
67-
68-
if ($property === "payload") {
69-
$this->payload = match (gettype($value)) {
70-
"object" => (array) $value,
71-
"string" => @json_decode($value, true),
72-
"array" => $value,
73-
};
74-
} else {
75-
$this->$property = $value;
76-
}
77-
}
78-
});
79-
8060
if ($data?->type === "message_new") {
8161
$this->client_info = $data?->object->client_info;
8262
}

src/Events/MessageEvent.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Astaroth\DataFetcher\Events;
1111

1212
use Astaroth\DataFetcher\DataFetcher;
13+
use Astaroth\DataFetcher\Sort;
1314

1415
/**
1516
* Class MessageEvent
@@ -18,6 +19,8 @@
1819
*/
1920
final class MessageEvent extends DataFetcher
2021
{
22+
use Sort;
23+
2124
private int $peer_id;
2225
private int $user_id;
2326
private int $conversation_message_id;
@@ -29,7 +32,7 @@ final class MessageEvent extends DataFetcher
2932
public function __construct(?object $data = null)
3033
{
3134
parent::__construct($data);
32-
($this->callable_sort)($data?->object);
35+
$this->sort($data?->object);
3336
}
3437

3538
/**

src/Events/MessageNew.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
use Astaroth\DataFetcher\DataFetcher;
13+
use Astaroth\DataFetcher\Sort;
1314

1415
/**
1516
* Class MessageNew
@@ -19,6 +20,8 @@
1920
*/
2021
final class MessageNew extends DataFetcher
2122
{
23+
use Sort;
24+
2225
private int $id;
2326
private int $out;
2427
private int $date;
@@ -51,7 +54,7 @@ final class MessageNew extends DataFetcher
5154
public function __construct(?object $data = null)
5255
{
5356
parent::__construct($data);
54-
($this->callable_sort)($data?->object->message);
57+
$this->sort($data?->object->message);
5558
}
5659

5760
/**

src/Events/WallPostNew.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
namespace Astaroth\DataFetcher\Events;
99

1010
use Astaroth\DataFetcher\DataFetcher;
11+
use Astaroth\DataFetcher\Sort;
1112

1213
/**
1314
* Class WallPostNew
1415
* @package Astaroth\DataFetcher\Events
1516
*/
1617
final class WallPostNew extends DataFetcher
1718
{
19+
use Sort;
20+
1821
private int $id;
1922
private int $from_id;
2023
private int $owner_id;
@@ -33,7 +36,7 @@ final class WallPostNew extends DataFetcher
3336
public function __construct(?object $data = null)
3437
{
3538
parent::__construct($data);
36-
($this->callable_sort)($data?->object->message);
39+
$this->sort($data?->object);
3740
}
3841

3942
/**

src/Sort.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Astaroth\DataFetcher;
6+
7+
trait Sort
8+
{
9+
private function sort($data): void
10+
{
11+
array_walk($data, function ($value, $property) {
12+
if (property_exists($this, $property)) {
13+
14+
if ($property === "peer_id" && $value - 2000000000 > 0) {
15+
$this->chat_id = $value - 2000000000;
16+
}
17+
18+
if ($property === "payload") {
19+
$this->payload = match (gettype($value)) {
20+
"object" => (array)$value,
21+
"string" => @json_decode($value, true),
22+
"array" => $value,
23+
};
24+
} else {
25+
$this->$property = $value;
26+
}
27+
}
28+
});
29+
}
30+
}

0 commit comments

Comments
 (0)