PHP code example of slick / cqrs-tools

1. Go to this page and download the library: Download slick/cqrs-tools 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/ */

    

slick / cqrs-tools example snippets




use Ramsey\Uuid\Uuid;
use Slick\CQRSTools\Event\AbstractEvent;
use Slick\CQRSTools\Event;

class UserWasCreated extends AbstractEvent implements Event
{
    /** @var Uuid */
    private $userId;
    
    /** @var string */
    private $name;
    
    public function __construct(Uuid $userId, string  $name)
    {
        parent::__construct();
        $this->userId = $userId;
        $this->name = $name;
    }
    
    public function jsonSerialize()
    {
        return [
            'userId' => $this->userId,
            'name' => $this->name    
        ];
    }
    
    public function unserializeEvent($data): void
    {
        $this->userId = Uuid::fromString($data->userId);
        $this->name = $data->name;
    }
    
}



use Ramsey\Uuid\Uuid;

final class User
{
    /** @var Uuid */
    private $userId;
    
    /** @var string */
    private $name;
    
    public function __construct(string $name) {
        $this->name = $name;
        $this->userId = Uuid::uuid4();
    }
    
    public function userId(): Uuid
    {
        return $this->userId;
    }
    
    public function name(): string
    {
        return $this->name;
    }
}



use Ramsey\Uuid\Uuid;
use Slick\CQRSTools\Event\EventGenerator;
use Slick\CQRSTools\Event\EventGeneratorMethods;

final class User implements EventGenerator
{
    // ... other code
    
    use EventGeneratorMethods;
    
    public function __construct(string $name) {
        $this->name = $name;
        $this->userId = Uuid::uuid4();
        // Create the event
        $this->recordThat(new UserWasCreated($this->userId, $name));
    }
    
}