-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventType.php
More file actions
60 lines (51 loc) · 1.97 KB
/
Copy pathEventType.php
File metadata and controls
60 lines (51 loc) · 1.97 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
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace TinyBlocks\BuildingBlocks\Event;
use ReflectionClass;
use TinyBlocks\BuildingBlocks\Exceptions\InvalidEventType;
use TinyBlocks\Vo\ValueObject;
use TinyBlocks\Vo\ValueObjectBehavior;
final readonly class EventType implements ValueObject
{
use ValueObjectBehavior;
private const string PATTERN = '/^[A-Z][A-Za-z0-9]+$/';
private function __construct(public string $value)
{
if (!preg_match(self::PATTERN, $value)) {
throw new InvalidEventType(value: $value, pattern: self::PATTERN);
}
}
/**
* Creates an EventType from a raw type identifier.
*
* @param string $value The PascalCase type identifier.
* @return EventType The created instance.
* @throws InvalidEventType If the value does not match the required pattern.
*/
public static function fromString(string $value): EventType
{
return new EventType(value: $value);
}
/**
* Creates an EventType from a domain event using its short class name.
*
* @param DomainEvent $event The domain event whose class name carries the type.
* @return EventType The created instance.
* @throws InvalidEventType If the resolved class name does not match the required pattern.
*/
public static function fromDomainEvent(DomainEvent $event): EventType
{
return new EventType(value: new ReflectionClass(objectOrClass: $event)->getShortName());
}
/**
* Creates an EventType from an integration event using its short class name.
*
* @param IntegrationEvent $event The integration event whose class name carries the type.
* @return EventType The created instance.
* @throws InvalidEventType If the resolved class name does not match the required pattern.
*/
public static function fromIntegrationEvent(IntegrationEvent $event): EventType
{
return new EventType(value: new ReflectionClass(objectOrClass: $event)->getShortName());
}
}