|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/cqrs project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Daikon\EventSourcing\Aggregate; |
|
12
|
|
|
|
|
13
|
|
|
use Assert\Assertion; |
|
14
|
|
|
use Daikon\Entity\ValueObject\ValueObjectInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class AggregatePrefix implements AggregateIdInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
private $prefix; |
|
20
|
|
|
|
|
21
|
4 |
|
public static function fromNative($prefix): ValueObjectInterface |
|
22
|
|
|
{ |
|
23
|
4 |
|
return new static(trim($prefix)); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
public static function makeEmpty(): ValueObjectInterface |
|
27
|
|
|
{ |
|
28
|
1 |
|
throw new \Exception("Creating empty aggregate prefixes is not supported."); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function fromFqcn(string $fqcn): ValueObjectInterface |
|
32
|
|
|
{ |
|
33
|
|
|
Assertion::classExists($fqcn); |
|
34
|
|
|
|
|
35
|
|
|
$parts = explode('\\', $fqcn, 4); |
|
36
|
|
|
return static::fromNative( |
|
37
|
|
|
sprintf( |
|
38
|
|
|
'%s.%s.%s', |
|
39
|
|
|
static::asSnakeCase($parts[0]), |
|
|
|
|
|
|
40
|
|
|
static::asSnakeCase($parts[1]), |
|
|
|
|
|
|
41
|
|
|
static::asSnakeCase($parts[2]) |
|
|
|
|
|
|
42
|
|
|
) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
public function toNative() |
|
47
|
|
|
{ |
|
48
|
2 |
|
return $this->prefix; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function equals(ValueObjectInterface $streamPrefix): bool |
|
52
|
|
|
{ |
|
53
|
1 |
|
Assertion::isInstanceOf($streamPrefix, static::class); |
|
54
|
1 |
|
return $this->prefix === $streamPrefix->toNative(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function isEmpty(): bool |
|
58
|
|
|
{ |
|
59
|
1 |
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function __toString(): string |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->prefix; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private static function asSnakeCase(string $value): string |
|
68
|
|
|
{ |
|
69
|
|
|
return ctype_lower($value) |
|
70
|
|
|
? $value |
|
71
|
|
|
: mb_strtolower(preg_replace('/(.)([A-Z])/', '$1_$2', $value)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
4 |
|
private function __construct(string $prefix) |
|
75
|
|
|
{ |
|
76
|
4 |
|
Assertion::notEmpty($prefix); |
|
77
|
4 |
|
$this->prefix = $prefix; |
|
78
|
4 |
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
Late static binding only has effect in subclasses. A
finalclass cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::withself::.To learn more about late static binding, please refer to the PHP core documentation.