Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
labi-le committed Sep 1, 2021
1 parent 5bf3694 commit 32211a7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
20 changes: 0 additions & 20 deletions src/DataFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class DataFetcher
private string $type;
private int $group_id;
private string $event_id;
protected \Closure $callable_sort;


/**
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Events/MessageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Astaroth\DataFetcher\Events;

use Astaroth\DataFetcher\DataFetcher;
use Astaroth\DataFetcher\Sort;

/**
* Class MessageEvent
Expand All @@ -18,6 +19,8 @@
*/
final class MessageEvent extends DataFetcher
{
use Sort;

private int $peer_id;
private int $user_id;
private int $conversation_message_id;
Expand All @@ -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);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Events/MessageNew.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


use Astaroth\DataFetcher\DataFetcher;
use Astaroth\DataFetcher\Sort;

/**
* Class MessageNew
Expand All @@ -19,6 +20,8 @@
*/
final class MessageNew extends DataFetcher
{
use Sort;

private int $id;
private int $out;
private int $date;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Events/WallPostNew.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
namespace Astaroth\DataFetcher\Events;

use Astaroth\DataFetcher\DataFetcher;
use Astaroth\DataFetcher\Sort;

/**
* Class WallPostNew
* @package Astaroth\DataFetcher\Events
*/
final class WallPostNew extends DataFetcher
{
use Sort;

private int $id;
private int $from_id;
private int $owner_id;
Expand All @@ -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);
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/Sort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Astaroth\DataFetcher;

trait Sort
{
private function sort($data): void
{
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;
}
}
});
}
}

0 comments on commit 32211a7

Please sign in to comment.