-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.php
More file actions
123 lines (101 loc) · 2.56 KB
/
sample.php
File metadata and controls
123 lines (101 loc) · 2.56 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Foo\Bar;
use Goodby\Assertion\Assert;
use Goodby\DDDSupport\EventTracking\EventSourcedRootEntity;
use Goodby\EventSourcing\Event;
require __DIR__ . '/vendor/autoload.php';
class User
{
use EventSourcedRootEntity;
private $userId;
private $name;
private $password;
/**
* @param string $userId
* @param string $name
* @param string $password
*/
public function __construct($userId, $name, $password)
{
Assert::argumentNotEmpty($userId, 'User ID is required');
Assert::argumentNotEmpty($name, 'User name is required');
Assert::argumentNotEmpty($password, 'User password is required');
$this->apply(new UserRegistered($userId, $name, $password));
}
/**
* @param UserRegistered $event
*/
private function whenUserRegistered(UserRegistered $event)
{
$this->userId = $event->userId();
$this->name = $event->name();
$this->password = $event->password();
}
}
class UserRegistered implements Event
{
private $eventVersion;
private $occurredOn;
private $userId;
private $name;
private $password;
public function __construct($userId, $name, $password)
{
$this->eventVersion = 1;
$this->occurredOn = time();
$this->userId = $userId;
$this->name = $name;
$this->password = $password;
}
/**
* @return int
*/
public function eventVersion()
{
return $this->eventVersion;
}
/**
* @return \DateTime
*/
public function occurredOn()
{
return $this->occurredOn;
}
/**
* Must return a primitive key-value set which is serializable.
* @return mixed[]
*/
public function toContractualData()
{
return [
'eventVersion' => $this->eventVersion,
'occurredOn' => $this->occurredOn,
'userId' => $this->userId,
'name' => $this->name,
'password' => $this->password,
];
}
/**
* @param mixed[] $data
* @return Event
*/
public static function fromContractualData(array $data)
{
$self = new self($data['userId'], $data['name'], $data['password']);
return $self;
}
public function userId()
{
return $this->userId;
}
public function name()
{
return $this->name;
}
public function password()
{
return $this->password;
}
}
$user = new User('0cc175b9c0f1b6a831c399e269772661', 'alice', 'p@ssW0rd');
var_dump($user);