Completed
Pull Request — master (#304)
by Benoît
03:33
created

SerializerAbstract   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 4 Features 4
Metric Value
wmc 6
c 11
b 4
f 4
lcom 0
cbo 0
dl 0
loc 115
ccs 10
cts 10
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
collection() 0 1 ?
item() 0 1 ?
null() 0 1 ?
includedData() 0 1 ?
meta() 0 1 ?
paginator() 0 1 ?
cursor() 0 1 ?
A mergeIncludes() 0 10 2
A sideloadIncludes() 0 4 1
A injectData() 0 4 2
A filterIncludes() 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
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
     * @param string $resourceKey
44
     *
45
     * @return array|null
46
     */
47
    abstract public function null($resourceKey);
48
49
    /**
50
     * Serialize the included data.
51
     *
52
     * @param ResourceInterface $resource
53
     * @param array             $data
54
     *
55
     * @return array
56
     */
57
    abstract public function includedData(ResourceInterface $resource, array $data);
58
59
    /**
60
     * Serialize the meta.
61
     *
62
     * @param array $meta
63
     *
64
     * @return array
65
     */
66
    abstract public function meta(array $meta);
67
68
    /**
69
     * Serialize the paginator.
70
     *
71
     * @param PaginatorInterface $paginator
72
     *
73
     * @return array
74
     */
75
    abstract public function paginator(PaginatorInterface $paginator);
76
77
    /**
78
     * Serialize the cursor.
79
     *
80
     * @param CursorInterface $cursor
81
     *
82
     * @return array
83
     */
84
    abstract public function cursor(CursorInterface $cursor);
85
86 33
    public function mergeIncludes($transformedData, $includedData)
87
    {
88
        // If the serializer does not want the includes to be side-loaded then
89
        // the included data must be merged with the transformed data.
90 33
        if (! $this->sideloadIncludes()) {
91 7
            return array_merge($transformedData, $includedData);
92
        }
93
94 26
        return $transformedData;
95
    }
96
97
    /**
98
     * Indicates if includes should be side-loaded.
99
     *
100
     * @return bool
101
     */
102 16
    public function sideloadIncludes()
103
    {
104 16
        return false;
105
    }
106
107
    /**
108
     * Hook for the serializer to inject custom data based on the relationships of the resource.
109
     *
110
     * @param array|null $data
111
     * @param array $rawIncludedData
112
     *
113
     * @return array
114
     */
115 1
    public function injectData($data, $rawIncludedData)
116
    {
117 1
        return $data ?: [];
118
    }
119
120
    /**
121
     * Hook for the serializer to modify the final list of includes.
122
     *
123
     * @param array             $includedData
124
     * @param array             $data
125
     *
126
     * @return array
127
     */
128 1
    public function filterIncludes($includedData, $data)
129
    {
130 1
        return $includedData;
131
    }
132
}
133