Passed
Push — master ( df92be...623891 )
by Alexander
15:39 queued 51s
created

SuccessSerializer::includedData()   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
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
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 1
    public function collection($resourceKey, array $data)
27
    {
28 1
        return ['data' => $data];
29
    }
30
31
    /**
32
     * Serialize item resources.
33
     *
34
     * @param  string $resourceKey
35
     * @param  array  $data
36
     * @return array
37
     */
38 1
    public function item($resourceKey, array $data)
39
    {
40 1
        return ['data' => $data];
41
    }
42
43
    /**
44
     * Serialize null resources.
45
     *
46
     * @return array
47
     */
48 1
    public function null()
49
    {
50 1
        return ['data' => null];
51
    }
52
53
    /**
54
     * Format meta data.
55
     *
56
     * @param  array $meta
57
     * @return array
58
     */
59 1
    public function meta(array $meta)
60
    {
61 1
        return $meta;
62
    }
63
64
    /**
65
     * Format pagination data.
66
     *
67
     * @param  \League\Fractal\Pagination\PaginatorInterface $paginator
68
     * @return array
69
     */
70 1
    public function paginator(PaginatorInterface $paginator)
71
    {
72 1
        $pagination = parent::paginator($paginator)['pagination'];
73
74
        return [
75
            'pagination' => [
76 1
                'count' => $pagination['count'],
77 1
                'total' => $pagination['total'],
78 1
                'perPage' => $pagination['per_page'],
79 1
                'currentPage' => $pagination['current_page'],
80 1
                'totalPages' => $pagination['total_pages'],
81 1
                '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 1
    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 1
                'current' => $cursor->getCurrent(),
97 1
                'previous' => $cursor->getPrev(),
98 1
                'next' => $cursor->getNext(),
99 1
                'count' => (int) $cursor->getCount(),
100
            ],
101
        ];
102
    }
103
104
    /**
105
     * Merge includes into data.
106
     *
107
     * @param  array $transformedData
108
     * @param  array $includedData
109
     * @return array
110
     */
111 1
    public function mergeIncludes($transformedData, $includedData)
112
    {
113 1
        foreach (array_keys($includedData) as $key) {
114 1
            $includedData[$key] = $includedData[$key]['data'];
115
        }
116
117 1
        return array_merge($transformedData, $includedData);
118
    }
119
}
120