Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
enable json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed May 18, 2018
1 parent 7675114 commit 1b60d7e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -22,7 +22,9 @@
"illuminate/console": "5.6.*",
"illuminate/database": "5.6.*",
"illuminate/events": "5.6.*",
"illuminate/support": "5.6.*"
"illuminate/support": "5.6.*",
"symfony/property-access": "^4.0",
"symfony/serializer": "^4.0"
},
"require-dev": {
"mockery/mockery": "^1.1",
Expand Down
37 changes: 37 additions & 0 deletions src/EventSerializer.php
@@ -0,0 +1,37 @@
<?php

namespace Spatie\EventProjector;

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class EventSerializer
{
/** @var \Symfony\Component\Serializer\Serializer */
protected $serializer;

public function __construct()
{
$encoders = array(new JsonEncoder());
$normalizers = array(new ObjectNormalizer());

$this->serializer = new Serializer($normalizers, $encoders);
}

public function serialize(ShouldBeStored $event): string
{
$event->__sleep();

$json = $this->serializer->serialize($event, 'json');

return $json;
}

public function deserialize(string $eventClass, string $json): ShouldBeStored
{
$restoredEvent = $this->serializer->deserialize($json, $eventClass, 'json');

return unserialize(serialize($restoredEvent));
}
}
15 changes: 10 additions & 5 deletions src/StoredEvent.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;


class StoredEvent extends Model
{
public $guarded = [];
Expand All @@ -14,14 +15,18 @@ class StoredEvent extends Model

public static function createForEvent(ShouldBeStored $event): self
{
return static::create([
'event_class' => get_class($event),
'serialized_event' => serialize(clone $event),
]);
$storedEvent = new StoredEvent();
$storedEvent->event_class = get_class($event);
$storedEvent->attributes['serialized_event'] = (new EventSerializer())->serialize(clone $event);
$storedEvent->save();

return $storedEvent;
}

public function getEventAttribute(): ShouldBeStored
{
return unserialize($this->serialized_event);
return (new EventSerializer())->deserialize($this->event_class, $this->getOriginal('serialized_event'));


}
}
4 changes: 2 additions & 2 deletions tests/Console/ReplayEventsCommandTest.php
Expand Up @@ -18,7 +18,7 @@ public function setUp()

$account = Account::create();

foreach(range(1,10) as $i) {
foreach(range(1,3) as $i) {
event(new MoneyAdded($account, 1234));
}
}
Expand All @@ -28,7 +28,7 @@ public function it_will_replay_events_to_the_given_projectors()
{
$projector = Mockery::mock(BalanceProjector::class);

$projector->shouldReceive('onMoneyAdded')->andReturnNull()->times(10);
$projector->shouldReceive('onMoneyAdded')->andReturnNull()->times(3);

EventProjectionist::addProjector($projector);

Expand Down

0 comments on commit 1b60d7e

Please sign in to comment.