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