1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiSkeleton\Serialization; |
6
|
|
|
|
7
|
|
|
use Chubbyphp\Serialization\Accessor\PropertyAccessor; |
8
|
|
|
use Chubbyphp\Serialization\Link\LinkGeneratorInterface; |
9
|
|
|
use Chubbyphp\Serialization\Link\NullLink; |
10
|
|
|
use Chubbyphp\Serialization\Mapping\FieldMapping; |
11
|
|
|
use Chubbyphp\Serialization\Mapping\FieldMappingInterface; |
12
|
|
|
use Chubbyphp\Serialization\Mapping\LinkMapping; |
13
|
|
|
use Chubbyphp\Serialization\Mapping\LinkMappingInterface; |
14
|
|
|
use Chubbyphp\Serialization\Mapping\ObjectMappingInterface; |
15
|
|
|
use Chubbyphp\Serialization\Serializer\Field\CollectionFieldSerializer; |
16
|
|
|
use Chubbyphp\Serialization\Serializer\Field\ValueFieldSerializer; |
17
|
|
|
use Chubbyphp\Serialization\Serializer\Link\CallbackLinkSerializer; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
19
|
|
|
use Chubbyphp\ApiSkeleton\Search\CourseSearch; |
20
|
|
|
|
21
|
|
|
final class CourseSearchMapping implements ObjectMappingInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var LinkGeneratorInterface |
25
|
|
|
*/ |
26
|
|
|
private $linkGenerator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param LinkGeneratorInterface $linkGenerator |
30
|
|
|
*/ |
31
|
6 |
|
public function __construct(LinkGeneratorInterface $linkGenerator) |
32
|
|
|
{ |
33
|
6 |
|
$this->linkGenerator = $linkGenerator; |
34
|
6 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
1 |
|
public function getClass(): string |
40
|
|
|
{ |
41
|
1 |
|
return CourseSearch::class; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
1 |
|
public function getType(): string |
48
|
|
|
{ |
49
|
1 |
|
return 'course-search'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return FieldMappingInterface[] |
54
|
|
|
*/ |
55
|
1 |
|
public function getFieldMappings(): array |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
1 |
|
new FieldMapping('page', new ValueFieldSerializer( |
59
|
1 |
|
new PropertyAccessor('page'), |
60
|
1 |
|
ValueFieldSerializer::CAST_INT) |
61
|
|
|
), |
62
|
1 |
|
new FieldMapping('perPage', new ValueFieldSerializer( |
63
|
1 |
|
new PropertyAccessor('perPage'), |
64
|
1 |
|
ValueFieldSerializer::CAST_INT) |
65
|
|
|
), |
66
|
1 |
|
new FieldMapping('sort'), |
67
|
1 |
|
new FieldMapping('order'), |
68
|
1 |
|
new FieldMapping('count'), |
69
|
1 |
|
new FieldMapping('pages'), |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return FieldMappingInterface[] |
75
|
|
|
*/ |
76
|
1 |
|
public function getEmbeddedFieldMappings(): array |
77
|
|
|
{ |
78
|
|
|
return [ |
79
|
1 |
|
new FieldMapping('courses', new CollectionFieldSerializer(new PropertyAccessor('courses'))), |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return LinkMappingInterface[] |
85
|
|
|
*/ |
86
|
2 |
|
public function getLinkMappings(): array |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
2 |
|
new LinkMapping('self', new CallbackLinkSerializer( |
90
|
2 |
|
function (Request $request, CourseSearch $courseSearch, array $fields) { |
91
|
2 |
|
unset($fields['count'], $fields['pages']); |
92
|
|
|
|
93
|
2 |
|
return $this->linkGenerator->generateLink($request, 'course_search', [], $fields); |
94
|
2 |
|
} |
95
|
|
|
)), |
96
|
2 |
|
new LinkMapping('prev', new CallbackLinkSerializer( |
97
|
2 |
View Code Duplication |
function (Request $request, CourseSearch $courseSearch, array $fields) { |
|
|
|
|
98
|
2 |
|
if ($courseSearch->getPage() > 1) { |
99
|
1 |
|
$fields['page'] -= 1; |
100
|
|
|
|
101
|
1 |
|
unset($fields['count'], $fields['pages']); |
102
|
|
|
|
103
|
1 |
|
return $this->linkGenerator->generateLink($request, 'course_search', [], $fields); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return new NullLink(); |
107
|
2 |
|
} |
108
|
|
|
)), |
109
|
2 |
|
new LinkMapping('next', new CallbackLinkSerializer( |
110
|
2 |
View Code Duplication |
function (Request $request, CourseSearch $courseSearch, array $fields) { |
|
|
|
|
111
|
2 |
|
if ($fields['page'] < $courseSearch->getPages()) { |
112
|
1 |
|
$fields['page'] += 1; |
113
|
|
|
|
114
|
1 |
|
unset($fields['count'], $fields['pages']); |
115
|
|
|
|
116
|
1 |
|
return $this->linkGenerator->generateLink($request, 'course_search', [], $fields); |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
return new NullLink(); |
120
|
2 |
|
} |
121
|
|
|
)), |
122
|
2 |
|
new LinkMapping('create', new CallbackLinkSerializer( |
123
|
2 |
|
function (Request $request, CourseSearch $courseSearch, array $fields) { |
|
|
|
|
124
|
2 |
|
return $this->linkGenerator->generateLink($request, 'course_create'); |
125
|
2 |
|
} |
126
|
|
|
)), |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.