Completed
Pull Request — master (#331)
by Matt
03:42
created

SerializerAbstract::injectAvailableIncludeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 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 null resource.
42
     *
43
     * @return array
44
     */
45
    abstract public function null();
46
47
    /**
48
     * Serialize the included data.
49
     *
50
     * @param ResourceInterface $resource
51
     * @param array             $data
52
     *
53
     * @return array
54
     */
55
    abstract public function includedData(ResourceInterface $resource, array $data);
56
57
    /**
58
     * Serialize the meta.
59
     *
60
     * @param array $meta
61
     *
62
     * @return array
63
     */
64
    abstract public function meta(array $meta);
65
66
    /**
67
     * Serialize the paginator.
68
     *
69
     * @param PaginatorInterface $paginator
70
     *
71
     * @return array
72
     */
73
    abstract public function paginator(PaginatorInterface $paginator);
74
75
    /**
76
     * Serialize the cursor.
77
     *
78
     * @param CursorInterface $cursor
79
     *
80
     * @return array
81
     */
82
    abstract public function cursor(CursorInterface $cursor);
83
84 37
    public function mergeIncludes($transformedData, $includedData)
85
    {
86
        // If the serializer does not want the includes to be side-loaded then
87
        // the included data must be merged with the transformed data.
88 37
        if (! $this->sideloadIncludes()) {
89 7
            return array_merge($transformedData, $includedData);
90
        }
91
92 30
        return $transformedData;
93
    }
94
95
    /**
96
     * Indicates if includes should be side-loaded.
97
     *
98
     * @return bool
99
     */
100 15
    public function sideloadIncludes()
101
    {
102 15
        return false;
103
    }
104
105
    /**
106
     * Hook for the serializer to inject custom data based on the relationships of the resource.
107
     *
108
     * @param array $data
109
     * @param array $rawIncludedData
110
     *
111
     * @return array
112
     */
113 1
    public function injectData($data, $rawIncludedData)
114
    {
115 1
        return $data;
116
    }
117
118
    /**
119
     * Hook for the serializer to inject custom data based on the available includes of the resource.
120
     *
121
     * @param array $data
122
     * @param array $availableIncludes
123
     *
124
     * @return array
125
     */
126 7
    public function injectAvailableIncludeData($data, $availableIncludes)
127
    {
128 7
        return $data;
129
    }
130
131
    /**
132
     * Hook for the serializer to modify the final list of includes.
133
     *
134
     * @param array             $includedData
135
     * @param array             $data
136
     *
137
     * @return array
138
     */
139 1
    public function filterIncludes($includedData, $data)
140
    {
141 1
        return $includedData;
142
    }
143
}
144