1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Shared Kernel library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-present LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace LIN3S\SharedKernel\Event; |
15
|
|
|
|
16
|
|
|
use LIN3S\SharedKernel\Domain\Model\Identity\Id; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Beñat Espiña <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class StreamName |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
private $name; |
24
|
|
|
private $aggregateId; |
25
|
|
|
|
26
|
|
|
public static function fromName(string $name) : self |
27
|
|
|
{ |
28
|
|
|
list($name, $aggregateId) = explode('-', $name, 2); |
29
|
|
|
|
30
|
|
|
return new self($aggregateId, $name); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function from(Id $aggregateId, string $name) : self |
34
|
|
|
{ |
35
|
|
|
return new self($aggregateId->id(), $name); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function __construct(string $aggregateId, string $name) |
39
|
|
|
{ |
40
|
|
|
$this->setName($name); |
41
|
|
|
$this->aggregateId = $aggregateId; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function setName(string $name) : void |
45
|
|
|
{ |
46
|
|
|
$this->checkNameIsValid($name); |
47
|
|
|
$this->name = $name; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function checkNameIsValid(string $name) : void |
51
|
|
|
{ |
52
|
|
|
if ('' === $name) { |
53
|
|
|
throw new StreamNameIsEmpty(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function name() : string |
58
|
|
|
{ |
59
|
|
|
return sprintf('%s-%s', $this->name, $this->aggregateId); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function aggregateId() : string |
63
|
|
|
{ |
64
|
|
|
return $this->aggregateId; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function __toString() : string |
68
|
|
|
{ |
69
|
|
|
return (string) $this->name(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|