|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Event Sourcing implementation |
|
5
|
|
|
* |
|
6
|
|
|
* @author Maksim Masiukevich <[email protected]> |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace ServiceBus\EventSourcing\Indexes; |
|
14
|
|
|
|
|
15
|
|
|
use ServiceBus\EventSourcing\Indexes\Exceptions\IndexNameCantBeEmpty; |
|
16
|
|
|
use ServiceBus\EventSourcing\Indexes\Exceptions\ValueKeyCantBeEmpty; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The key for the value stored in the index |
|
20
|
|
|
* |
|
21
|
|
|
* @property-read string $indexName |
|
22
|
|
|
* @property-read string $valueKey |
|
23
|
|
|
*/ |
|
24
|
|
|
final class IndexKey |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
public $indexName; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
public $valueKey; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $indexName |
|
38
|
|
|
* @param string $valueKey |
|
39
|
|
|
* |
|
40
|
|
|
* @return self |
|
41
|
|
|
* |
|
42
|
|
|
* @throws \ServiceBus\EventSourcing\Indexes\Exceptions\IndexNameCantBeEmpty |
|
43
|
|
|
* @throws \ServiceBus\EventSourcing\Indexes\Exceptions\ValueKeyCantBeEmpty |
|
44
|
|
|
*/ |
|
45
|
4 |
|
public static function create(string $indexName, string $valueKey): self |
|
46
|
|
|
{ |
|
47
|
4 |
|
self::assertIndexNameIsNotEmpty($indexName); |
|
48
|
4 |
|
self::assertValueKeyIsNotEmpty($valueKey); |
|
49
|
|
|
|
|
50
|
4 |
|
return new self($indexName, $valueKey); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $indexName |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \ServiceBus\EventSourcing\Indexes\Exceptions\IndexNameCantBeEmpty |
|
59
|
|
|
*/ |
|
60
|
4 |
|
private static function assertIndexNameIsNotEmpty(string $indexName): void |
|
61
|
|
|
{ |
|
62
|
4 |
|
if('' === $indexName) |
|
63
|
|
|
{ |
|
64
|
|
|
throw new IndexNameCantBeEmpty('Index name can\'t be empty'); |
|
65
|
|
|
} |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $valueKey |
|
70
|
|
|
* |
|
71
|
|
|
* @return void |
|
72
|
|
|
* |
|
73
|
|
|
* @throws \ServiceBus\EventSourcing\Indexes\Exceptions\ValueKeyCantBeEmpty |
|
74
|
|
|
*/ |
|
75
|
4 |
|
private static function assertValueKeyIsNotEmpty(string $valueKey): void |
|
76
|
|
|
{ |
|
77
|
4 |
|
if('' === $valueKey) |
|
78
|
|
|
{ |
|
79
|
|
|
throw new ValueKeyCantBeEmpty('Value key can\'t be empty'); |
|
80
|
|
|
} |
|
81
|
4 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $indexName |
|
85
|
|
|
* @param string $valueKey |
|
86
|
|
|
*/ |
|
87
|
4 |
|
private function __construct(string $indexName, string $valueKey) |
|
88
|
|
|
{ |
|
89
|
4 |
|
$this->indexName = $indexName; |
|
|
|
|
|
|
90
|
4 |
|
$this->valueKey = $valueKey; |
|
|
|
|
|
|
91
|
4 |
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|