PHP code example of andreo / eventsauce-upcasting

1. Go to this page and download the library: Download andreo/eventsauce-upcasting library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andreo / eventsauce-upcasting example snippets



use Andreo\EventSauce\Upcasting\MessageUpcaster\MessageUpcaster;
use EventSauce\EventSourcing\Message;

final class FooUpcaster implements MessageUpcaster
{
    public function upcast(Message $message): Message
    {
        $event = $message->payload();
        if (!$event instanceof FooEvent)) { 
            return $message;
        }

        return new Message(new FooEventV2()); 
    }
}


use Andreo\EventSauce\Upcasting\MessageUpcasterChain;

new MessageUpcasterChain(
    new SomeUpcaster(),
    new AnotherUpcaster(),
)
    


use Andreo\EventSauce\Upcasting\UpcastingMessageObjectSerializer;

new UpcastingMessageObjectSerializer(
    messageSerializer: $messageSerializer, // default EventSauce\EventSourcing\Serialization\MessageSerializer
    upcaster: new MessageUpcasterChain(new SomeUpcaster())
)
    

use EventSauce\EventSourcing\Message;
use Andreo\EventSauce\Upcasting\MessageUpcaster\MessageUpcaster;
use Andreo\EventSauce\Upcasting\MessageUpcaster\Event;

final class FooUpcaster implements MessageUpcaster
{
    #[Event(event: FooEvent::class)]
    public function upcast(Message $message): Message
    {
        $event = $message->payload();
        assert($event instanceof FooEvent);

        return new Message(new FooEventV2()); 
    }
}

use EventSauce\EventSourcing\AggregateRoot;
use EventSauce\EventSourcing\AggregateRootBehaviour;

final class FooAggregate implements AggregateRoot
{
    use AggregateRootBehaviour;
    
    // before applyFooEvent
    public function applyFooEventV2(FooEventV2 $event): void
    {
    }
}