Skip to content

Commit

Permalink
Remove client in serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostzero committed May 16, 2022
1 parent 8574a22 commit d26611b
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,55 @@
namespace GhostZero\Tmi\Events;

use GhostZero\Tmi\Client;
use ReflectionClass;
use ReflectionException;
use Serializable;

class Event
class Event implements Serializable
{
/**
* @var Client Client object
* @var Client|null Client object. Can be null if serialized.
*/
public Client $client;
public ?Client $client = null;

public function signature(): ?string
{
return null;
}

/**
* Override serializer, since we cannot serialize the client object.
*
* @inheritdoc
*/
public function serialize()
{
$data = [];
$reflect = new ReflectionClass($this);
$props = $reflect->getProperties();
foreach ($props as $prop) {
if ($prop->getName() === 'client') {
continue;
}
$data[$prop->getName()] = ['s' => $prop->isStatic(), 'v' => $prop->getValue($this)];
}
return serialize($data);
}

/**
* @inheritdoc
* @throws ReflectionException
*/
public function unserialize($data)
{
$props = unserialize($data);
$reflect = new ReflectionClass($this);
foreach ($props as $name => $prop) {
if ($prop['s']) {
$reflect->getProperty($name)->setValue($prop['v']);
} else {
$reflect->getProperty($name)->setValue($this, $prop['v']);
}
}
}
}

0 comments on commit d26611b

Please sign in to comment.