daikon-cqrs/event-sourcing

View on GitHub
src/EventStore/Stream/StreamMap.php

Summary

Maintainability
A
0 mins
Test Coverage
F
0%
<?php declare(strict_types=1);
/**
 * This file is part of the daikon-cqrs/event-sourcing project.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Daikon\EventSourcing\EventStore\Stream;

use Daikon\DataStructure\TypedMap;
use Daikon\EventSourcing\Aggregate\AggregateIdInterface;
use Daikon\Interop\MakeEmptyInterface;
use Daikon\Interop\ToNativeInterface;

final class StreamMap extends TypedMap implements MakeEmptyInterface, ToNativeInterface
{
    public function __construct(iterable $streams = [])
    {
        $this->init($streams, [StreamInterface::class]);
    }

    public function register(StreamInterface $stream): self
    {
        return $this->with((string)$stream->getAggregateId(), $stream);
    }

    public function unregister(AggregateIdInterface $aggregateId): self
    {
        return $this->without((string)$aggregateId);
    }

    public static function makeEmpty(): self
    {
        return new self;
    }

    public function toNative(): array
    {
        $this->assertInitialized();
        $streams = [];
        foreach ($this as $key => $stream) {
            $streams[$key] = $stream->toNative();
        }
        return $streams;
    }
}