-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentProtocol.php
70 lines (62 loc) · 1.88 KB
/
StudentProtocol.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
<?php
declare(strict_types=1);
namespace Suin\Marshaller\ExampleModel;
use Suin\Marshaller\StandardProtocol;
class StudentProtocol extends StandardProtocol
{
public function __construct()
{
parent::__construct(
$this->studentIdFormat(),
$this->gradeFormat()
);
}
private function studentIdFormat()
{
return new class {
public function read(int $value): StudentId
{
return new StudentId($value);
}
public function write(StudentId $id): int
{
return $id->id();
}
};
}
private function gradeFormat()
{
return new class {
public function read(string $grade): Grade
{
switch ($grade) {
case 'freshman':
return Grade::freshman();
case 'sophomore':
return Grade::sophomore();
case 'junior':
return Grade::junior();
case 'senior':
return Grade::senior();
default:
throw new \LogicException("Unknown grade: ${grade}");
}
}
public function write(Grade $grade): string
{
switch (true) {
case $grade->isFreshman():
return 'freshman';
case $grade->isSophomore():
return 'sophomore';
case $grade->isJunior():
return 'junior';
case $grade->isSenior():
return 'senior';
default:
throw new \LogicException("Unknown grade: ${grade}");
}
}
};
}
}