1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiSkeleton\Serialization; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Serialization\Mapping\FieldMapping; |
8
|
|
|
use Chubbyphp\Serialization\Mapping\FieldMappingInterface; |
9
|
|
|
use Chubbyphp\Serialization\Mapping\LinkMapping; |
10
|
|
|
use Chubbyphp\Serialization\Mapping\LinkMappingInterface; |
11
|
|
|
use Chubbyphp\Serialization\Mapping\ObjectMappingInterface; |
12
|
|
|
use Chubbyphp\Serialization\Serializer\Link\CallbackLinkSerializer; |
13
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
14
|
|
|
use Chubbyphp\ApiSkeleton\Model\Course; |
15
|
|
|
|
16
|
|
|
final class CourseMapping implements ObjectMappingInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var LinkGenerator |
20
|
|
|
*/ |
21
|
|
|
private $linkGenerator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param LinkGenerator $linkGenerator |
25
|
|
|
*/ |
26
|
|
|
public function __construct(LinkGenerator $linkGenerator) |
27
|
|
|
{ |
28
|
|
|
$this->linkGenerator = $linkGenerator; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getClass(): string |
35
|
|
|
{ |
36
|
|
|
return Course::class; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getType(): string |
43
|
|
|
{ |
44
|
|
|
return 'course'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return FieldMappingInterface[] |
49
|
|
|
*/ |
50
|
|
|
public function getFieldMappings(): array |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
new FieldMapping('id'), |
54
|
|
|
new FieldMapping('name'), |
55
|
|
|
new FieldMapping('level'), |
56
|
|
|
new FieldMapping('progress'), |
57
|
|
|
new FieldMapping('active'), |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return FieldMappingInterface[] |
63
|
|
|
*/ |
64
|
|
|
public function getEmbeddedFieldMappings(): array |
65
|
|
|
{ |
66
|
|
|
return []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return LinkMappingInterface[] |
71
|
|
|
*/ |
72
|
|
|
public function getLinkMappings(): array |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
new LinkMapping('read', new CallbackLinkSerializer( |
76
|
|
|
function (Request $request, Course $course) { |
77
|
|
|
return $this->linkGenerator->generateLink('course_read', ['id' => $course->getId()]); |
78
|
|
|
} |
79
|
|
|
)), |
80
|
|
|
new LinkMapping('update', new CallbackLinkSerializer( |
81
|
|
|
function (Request $request, Course $course) { |
82
|
|
|
return $this->linkGenerator->generateLink('course_update', ['id' => $course->getId()]); |
83
|
|
|
} |
84
|
|
|
)), |
85
|
|
|
new LinkMapping('delete', new CallbackLinkSerializer( |
86
|
|
|
function (Request $request, Course $course) { |
87
|
|
|
return $this->linkGenerator->generateLink('course_delete', ['id' => $course->getId()]); |
88
|
|
|
} |
89
|
|
|
)), |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|