Test Setup Failed
Push — v2 ( 74186a...c94b75 )
by Alexander
06:56
created

SuccessSerializer::null()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Resource\ResourceInterface;
8
use League\Fractal\Serializer\ArraySerializer;
9
10
/**
11
 * A serializer class responsible for formatting success data.
12
 *
13
 * @package flugger/laravel-responder
14
 * @author  Alexander Tømmerås <[email protected]>
15
 * @license The MIT License
16
 */
17
class SuccessSerializer extends ArraySerializer
18
{
19
    /**
20
     * Serialize collection resources.
21
     *
22
     * @param  string $resourceKey
23
     * @param  array  $data
24
     * @return array
25
     */
26
    public function collection($resourceKey, array $data)
27
    {
28
        return ['data' => $data];
29
    }
30
31
    /**
32
     * Serialize item resources.
33
     *
34
     * @param  string $resourceKey
35
     * @param  array  $data
36
     * @return array
37
     */
38
    public function item($resourceKey, array $data)
39
    {
40
        return ['data' => $data];
41
    }
42
43
    /**
44
     * Serialize null resources.
45
     *
46
     * @return array
47
     */
48
    public function null()
49
    {
50
        return ['data' => null];
51
    }
52
53
    /**
54
     * Format meta data.
55
     *
56
     * @param  array $meta
57
     * @return array
58
     */
59
    public function meta(array $meta)
60
    {
61
        return $meta;
62
    }
63
64
    /**
65
     * Format pagination data.
66
     *
67
     * @param  \League\Fractal\Pagination\PaginatorInterface $paginator
68
     * @return array
69
     */
70
    public function paginator(PaginatorInterface $paginator)
71
    {
72
        $pagination = parent::paginator($paginator)['pagination'];
73
74
        return [
75
            'pagination' => [
76
                'total' => $pagination['total'],
77
                'count' => $pagination['count'],
78
                'perPage' => $pagination['per_page'],
79
                'currentPage' => $pagination['current_page'],
80
                'totalPages' => $pagination['total_pages'],
81
                'links' => $pagination['links'],
82
            ],
83
        ];
84
    }
85
86
    /**
87
     * Format cursor data.
88
     *
89
     * @param  \League\Fractal\Pagination\CursorInterface $cursor
90
     * @return array
91
     */
92
    public function cursor(CursorInterface $cursor)
93
    {
94
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('cursor' =>... $cursor->getCount())); (array<string,array>) is incompatible with the return type of the parent method League\Fractal\Serializer\ArraySerializer::cursor of type array<string,array>.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
95
            'cursor' => [
96
                'current' => $cursor->getCurrent(),
97
                'previous' => $cursor->getPrev(),
98
                'next' => $cursor->getNext(),
99
                'count' => (int) $cursor->getCount(),
100
            ],
101
        ];
102
    }
103
104
    /**
105
     * Indicates if includes should be side-loaded.
106
     *
107
     * @return bool
108
     */
109
    public function sideloadIncludes()
110
    {
111
        return true;
112
    }
113
114
    /**
115
     * Merge includes into data.
116
     *
117
     * @param  array $transformedData
118
     * @param  array $includedData
119
     * @return array
120
     */
121
    public function mergeIncludes($transformedData, $includedData)
122
    {
123
        foreach (array_keys($includedData) as $key) {
124
            $includedData[$key] = $includedData[$key]['data'];
125
        }
126
127
        return array_merge($transformedData, $includedData);
128
    }
129
130
    /**
131
     * Format the included data.
132
     *
133
     * @param  \League\Fractal\Resource\ResourceInterface $resource
134
     * @param  array                                      $data
135
     * @return array
136
     */
137
    public function includedData(ResourceInterface $resource, array $data)
138
    {
139
        return [];
140
    }
141
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
142