SuccessSerializer::paginator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 1
rs 9.7666
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\Serializer\ArraySerializer;
8
9
/**
10
 * A serializer class responsible for formatting success data.
11
 *
12
 * @package flugger/laravel-responder
13
 * @author  Alexander Tømmerås <[email protected]>
14
 * @license The MIT License
15
 */
16
class SuccessSerializer extends ArraySerializer
17
{
18
    /**
19
     * Serialize collection resources.
20
     *
21
     * @param  string $resourceKey
22
     * @param  array  $data
23
     * @return array
24
     */
25 25
    public function collection($resourceKey, array $data)
26
    {
27 25
        return ['data' => $data];
28
    }
29
30
    /**
31
     * Serialize item resources.
32
     *
33
     * @param  string $resourceKey
34
     * @param  array  $data
35
     * @return array
36
     */
37 35
    public function item($resourceKey, array $data)
38
    {
39 35
        return ['data' => $data];
40
    }
41
42
    /**
43
     * Serialize null resources.
44
     *
45
     * @return array
46
     */
47 15
    public function null()
48
    {
49 15
        return ['data' => null];
50
    }
51
52
    /**
53
     * Format meta data.
54
     *
55
     * @param  array $meta
56
     * @return array
57
     */
58 56
    public function meta(array $meta)
59
    {
60 56
        return $meta;
61
    }
62
63
    /**
64
     * Format pagination data.
65
     *
66
     * @param  \League\Fractal\Pagination\PaginatorInterface $paginator
67
     * @return array
68
     */
69 3
    public function paginator(PaginatorInterface $paginator)
70
    {
71 3
        $pagination = parent::paginator($paginator)['pagination'];
72
73
        return [
74
            'pagination' => [
75 3
                'count' => $pagination['count'],
76 3
                'total' => $pagination['total'],
77 3
                'perPage' => $pagination['per_page'],
78 3
                'currentPage' => $pagination['current_page'],
79 3
                'totalPages' => $pagination['total_pages'],
80 3
                'links' => $pagination['links'],
81
            ],
82
        ];
83
    }
84
85
    /**
86
     * Format cursor data.
87
     *
88
     * @param  \League\Fractal\Pagination\CursorInterface $cursor
89
     * @return array
90
     */
91 2
    public function cursor(CursorInterface $cursor)
92
    {
93
        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...
94
            'cursor' => [
95 2
                'current' => $cursor->getCurrent(),
96 2
                'previous' => $cursor->getPrev(),
97 2
                'next' => $cursor->getNext(),
98 2
                'count' => (int) $cursor->getCount(),
99
            ],
100
        ];
101
    }
102
103
    /**
104
     * Merge includes into data.
105
     *
106
     * @param  array $transformedData
107
     * @param  array $includedData
108
     * @return array
109
     */
110 21
    public function mergeIncludes($transformedData, $includedData)
111
    {
112 21
        foreach (array_keys($includedData) as $key) {
113 21
            $includedData[$key] = $includedData[$key]['data'];
114
        }
115
116 21
        return array_merge($transformedData, $includedData);
117
    }
118
}
119