Completed
Pull Request — master (#273)
by Gonçalo
03:51
created

SerializerAbstract::collection()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
/*
4
 * This file is part of the League\Fractal package.
5
 *
6
 * (c) Phil Sturgeon <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\Fractal\Serializer;
13
14
use League\Fractal\Pagination\CursorInterface;
15
use League\Fractal\Pagination\PaginatorInterface;
16
use League\Fractal\Resource\ResourceInterface;
17
18
abstract class SerializerAbstract
19
{
20
    /**
21
     * Serialize a collection.
22
     *
23
     * @param string $resourceKey
24
     * @param array  $data
25
     *
26
     * @return array
27
     */
28
    abstract public function collection($resourceKey, array $data);
29
30
    /**
31
     * Serialize an item.
32
     *
33
     * @param string $resourceKey
34
     * @param array  $data
35
     *
36
     * @return array
37
     */
38
    abstract public function item($resourceKey, array $data);
39
40
    /**
41
     * Serialize the included data.
42
     *
43
     * @param ResourceInterface $resource
44
     * @param array             $data
45
     *
46
     * @return array
47
     */
48
    abstract public function includedData(ResourceInterface $resource, array $data);
49
50
    /**
51
     * Serialize the meta.
52
     *
53
     * @param array $meta
54
     *
55
     * @return array
56
     */
57
    abstract public function meta(array $meta);
58
59
    /**
60
     * Serialize the paginator.
61
     *
62
     * @param PaginatorInterface $paginator
63
     *
64
     * @return array
65
     */
66
    abstract public function paginator(PaginatorInterface $paginator);
67
68
    /**
69
     * Serialize the cursor.
70
     *
71
     * @param CursorInterface $cursor
72
     *
73
     * @return array
74
     */
75
    abstract public function cursor(CursorInterface $cursor);
76
77 33
    public function mergeIncludes($transformedData, $includedData)
78
    {
79
        // If the serializer does not want the includes to be side-loaded then
80
        // the included data must be merged with the transformed data.
81 33
        if (! $this->sideloadIncludes()) {
82 7
            return array_merge($transformedData, $includedData);
83
        }
84
85 26
        return $transformedData;
86
    }
87
88
    /**
89
     * Indicates if includes should be side-loaded.
90
     *
91
     * @return bool
92
     */
93 13
    public function sideloadIncludes()
94
    {
95 13
        return false;
96
    }
97
98
    /**
99
     * Hook for the serializer to inject custom data based on the relationships of the resource.
100
     *
101
     * @param array $data
102
     * @param array $rawIncludedData
103
     *
104
     * @return array
105
     */
106 1
    public function injectData($data, $rawIncludedData)
107
    {
108 1
        return $data;
109
    }
110
111
    /**
112
     * Hook for the serializer to modify the final list of includes.
113
     *
114
     * @param array             $includedData
115
     * @param array             $data
116
     *
117
     * @return array
118
     */
119 1
    public function filterIncludes($includedData, $data)
120
    {
121 1
        return $includedData;
122
    }
123
124
    /**
125
     * Get the mandatory fields for the serializer
126
     *
127
     * @return array
128
     */
129
    public function getMandatoryFields()
130
    {
131
        return [];
132
    }
133
}
134