-
Notifications
You must be signed in to change notification settings - Fork 9
/
Line.php
153 lines (126 loc) · 3.68 KB
/
Line.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace TalisOrm\AggregateRepositoryTest;
use Doctrine\DBAL\Schema\Schema;
use TalisOrm\AggregateId;
use TalisOrm\ChildEntity;
use TalisOrm\ChildEntityBehavior;
use TalisOrm\Schema\SpecifiesSchema;
final class Line implements ChildEntity, SpecifiesSchema
{
use ChildEntityBehavior;
/**
* @var OrderId
*/
private $orderId;
/**
* @var LineNumber
*/
private $lineNumber;
/**
* @var ProductId
*/
private $productId;
/**
* @var Quantity
*/
private $quantity;
/**
* @var int
*/
private $quantityPrecision;
private function __construct()
{
}
public static function create(
OrderId $orderId,
LineNumber $lineNumber,
ProductId $productId,
Quantity $quantity,
int $quantityPrecision
): Line {
$line = new self();
$line->orderId = $orderId;
$line->lineNumber = $lineNumber;
$line->productId = $productId;
$line->quantity = $quantity;
$line->quantityPrecision = $quantityPrecision;
return $line;
}
public function update(ProductId $productId, Quantity $quantity): void
{
$this->productId = $productId;
$this->quantity = $quantity;
}
public function lineNumber(): LineNumber
{
return $this->lineNumber;
}
public function orderId(): OrderId
{
return $this->orderId;
}
public function productId(): ProductId
{
return $this->productId;
}
public function quantity(): Quantity
{
return $this->quantity;
}
public function quantityPrecision(): int
{
return $this->quantityPrecision;
}
public function state(): array
{
return [
'order_id' => $this->orderId->orderId(),
'company_id' => $this->orderId->companyId(),
'line_number' => $this->lineNumber->asInt(),
'product_id' => $this->productId->productId(),
'quantity' => $this->quantity->asInt()
];
}
public static function fromState(array $state, array $aggregateState): Line
{
$line = new self();
$line->orderId = new OrderId($state['order_id'], (int)$state['company_id']);
$line->lineNumber = new LineNumber((int)$state['line_number']);
$line->productId = new ProductId($state['product_id'], (int)$state['company_id']);
$line->quantity = new Quantity((int)$state['quantity']);
$line->quantityPrecision = $aggregateState['quantityPrecision'];
return $line;
}
public static function tableName(): string
{
return 'lines';
}
public function identifier(): array
{
return [
'order_id' => $this->orderId->orderId(),
'company_id' => $this->orderId->companyId(),
'line_number' => $this->lineNumber->asInt()
];
}
public static function identifierForQuery(AggregateId $aggregateId): array
{
if (!$aggregateId instanceof OrderId) {
throw new \InvalidArgumentException('Expected an instance of OrderId');
}
return [
'order_id' => $aggregateId->orderId(),
'company_id' => $aggregateId->companyId()
];
}
public static function specifySchema(Schema $schema): void
{
$table = $schema->createTable('lines');
$table->addColumn('order_id', 'string');
$table->addColumn('company_id', 'integer');
$table->addColumn('line_number', 'integer');
$table->addColumn('product_id', 'string');
$table->addColumn('quantity', 'integer');
$table->setPrimaryKey(['order_id', 'company_id', 'line_number']);
}
}