1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Serializers; |
4
|
|
|
|
5
|
|
|
use League\Fractal\Pagination\CursorInterface; |
6
|
|
|
use League\Fractal\Pagination\PaginatorInterface; |
7
|
|
|
use League\Fractal\Serializer\ArraySerializer; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A serializer class responsible for formatting success data. |
11
|
|
|
* |
12
|
|
|
* @package flugger/laravel-responder |
13
|
|
|
* @author Alexander Tømmerås <[email protected]> |
14
|
|
|
* @license The MIT License |
15
|
|
|
*/ |
16
|
|
|
class SuccessSerializer extends ArraySerializer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Serialize collection resources. |
20
|
|
|
* |
21
|
|
|
* @param string $resourceKey |
22
|
|
|
* @param array $data |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
25 |
|
public function collection($resourceKey, array $data) |
26
|
|
|
{ |
27
|
25 |
|
return ['data' => $data]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Serialize item resources. |
32
|
|
|
* |
33
|
|
|
* @param string $resourceKey |
34
|
|
|
* @param array $data |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
35 |
|
public function item($resourceKey, array $data) |
38
|
|
|
{ |
39
|
35 |
|
return ['data' => $data]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Serialize null resources. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
15 |
|
public function null() |
48
|
|
|
{ |
49
|
15 |
|
return ['data' => null]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Format meta data. |
54
|
|
|
* |
55
|
|
|
* @param array $meta |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
56 |
|
public function meta(array $meta) |
59
|
|
|
{ |
60
|
56 |
|
return $meta; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Format pagination data. |
65
|
|
|
* |
66
|
|
|
* @param \League\Fractal\Pagination\PaginatorInterface $paginator |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
3 |
|
public function paginator(PaginatorInterface $paginator) |
70
|
|
|
{ |
71
|
3 |
|
$pagination = parent::paginator($paginator)['pagination']; |
72
|
|
|
|
73
|
|
|
return [ |
74
|
|
|
'pagination' => [ |
75
|
3 |
|
'count' => $pagination['count'], |
76
|
3 |
|
'total' => $pagination['total'], |
77
|
3 |
|
'perPage' => $pagination['per_page'], |
78
|
3 |
|
'currentPage' => $pagination['current_page'], |
79
|
3 |
|
'totalPages' => $pagination['total_pages'], |
80
|
3 |
|
'links' => $pagination['links'], |
81
|
|
|
], |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Format cursor data. |
87
|
|
|
* |
88
|
|
|
* @param \League\Fractal\Pagination\CursorInterface $cursor |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
2 |
|
public function cursor(CursorInterface $cursor) |
92
|
|
|
{ |
93
|
|
|
return [ |
|
|
|
|
94
|
|
|
'cursor' => [ |
95
|
2 |
|
'current' => $cursor->getCurrent(), |
96
|
2 |
|
'previous' => $cursor->getPrev(), |
97
|
2 |
|
'next' => $cursor->getNext(), |
98
|
2 |
|
'count' => (int) $cursor->getCount(), |
99
|
|
|
], |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Merge includes into data. |
105
|
|
|
* |
106
|
|
|
* @param array $transformedData |
107
|
|
|
* @param array $includedData |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
21 |
|
public function mergeIncludes($transformedData, $includedData) |
111
|
|
|
{ |
112
|
21 |
|
foreach (array_keys($includedData) as $key) { |
113
|
21 |
|
$includedData[$key] = $includedData[$key]['data']; |
114
|
|
|
} |
115
|
|
|
|
116
|
21 |
|
return array_merge($transformedData, $includedData); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.