SerializerAbstract   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 136
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
collection() 0 1 ?
paginator() 0 1 ?
cursor() 0 1 ?
item() 0 1 ?
null() 0 1 ?
includedData() 0 1 ?
meta() 0 1 ?
A mergeIncludes() 0 10 2
A sideloadIncludes() 0 4 1
A injectData() 0 4 1
A injectAvailableIncludeData() 0 4 1
A filterIncludes() 0 4 1
A getMandatoryFields() 0 4 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 implements Serializer
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 49
    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 49
        if (! $this->sideloadIncludes()) {
89 11
            return array_merge($transformedData, $includedData);
90
        }
91
92 38
        return $transformedData;
93
    }
94
95
    /**
96
     * Indicates if includes should be side-loaded.
97
     *
98
     * @return bool
99
     */
100 21
    public function sideloadIncludes()
101
    {
102 21
        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 9
    public function injectAvailableIncludeData($data, $availableIncludes)
127
    {
128 9
        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
    /**
145
     * Get the mandatory fields for the serializer
146
     *
147
     * @return array
148
     */
149 6
    public function getMandatoryFields()
150
    {
151 6
        return [];
152
    }
153
}
154