-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAggregateRootMetadata.php
More file actions
48 lines (39 loc) · 1.42 KB
/
AggregateRootMetadata.php
File metadata and controls
48 lines (39 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcing\Metadata\AggregateRoot;
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use function str_contains;
use function str_replace;
/** @template T of AggregateRoot */
final class AggregateRootMetadata
{
public readonly string $streamName;
public function __construct(
/** @var class-string<T> */
public readonly string $className,
public readonly string $name,
public readonly string $idProperty,
/** @var array<class-string, string> */
public readonly array $applyMethods,
/** @var array<class-string, true> */
public readonly array $suppressEvents,
public readonly bool $suppressAll,
public readonly Snapshot|null $snapshot,
/** @var list<string> */
public readonly array $childAggregates = [],
string|null $streamName = null,
public readonly string|null $autoInitializeMethod = null,
) {
$this->streamName = $streamName ?? $this->name . '-{id}';
}
public function streamName(string|null $aggregateId = null): string
{
if ($aggregateId === null) {
if (str_contains($this->streamName, '{id}')) {
throw new MissingAggregateIdForStreamName($this->streamName);
}
return $this->streamName;
}
return str_replace('{id}', $aggregateId, $this->streamName);
}
}