PHP code example of phpgears / aggregate

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

    

phpgears / aggregate example snippets




use Gears\Aggregate\AbstractAggregateRoot;
use Gears\Identity\Identity;

class CustomAggregate extends AbstractAggregateRoot
{
    public static function instantiate(Identity $identity): self
    {
        return new self($identity);
    }
}

use Gears\Aggregate\AbstractAggregateRoot;
use Gears\Identity\Identity;

class CustomAggregate extends AbstractAggregateRoot
{
    public static function instantiate(Identity $identity): self
    {
        return new self($identity);
    }

    public function doSomething(): void
    {
        // do something

        $this->recordEvent(new SomethingHappened());
    }
}

$customAggregate = CustomAggregate::instantiate(
    UuidIdentity::fromString('4c4316cb-b48b-44fb-a034-90d789966bac')
);
$customAggregate->doSomething();

foreach ($customAggregate->collectRecordedEvents() as $event) {
    /** @var \Gears\Event\EventBus $eventBus */
    $eventBus->dispatch($event);
}