PHP code example of koco / avro-regy

1. Go to this page and download the library: Download koco/avro-regy 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/ */

    

koco / avro-regy example snippets


return [
    // ...
    Koco\AvroRegy\AvroRegyBundle::class => ['all' => true],
];

namespace App\Catalogue\Infrastructure\Messenger\Serializer;

use App\Catalogue\Domain\Model\Event\ProductCreated;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;

class ProductCreatedSerializer implements SerializerInterface
{
    public function decode(array $encodedEnvelope): Envelope
    {
        $record = $encodedEnvelope['body'];

        return new Envelope(new ProductCreated(
            $record['id'],
            $record['name'],
            $record['description'],
        ));
    }

    public function encode(Envelope $envelope): array
    {
        /** @var ProductCreated $event */
        $event = $envelope->getMessage();
        
        return [
            'key' => $event->getId(),
            'headers' => [],
            'body' => [
                'id' => $event->getId(),
                'name' => $event->getName(),
                'description' => $event->getDescription(),
            ],
        ];
    }
}