Completed
Push — master ( 141581...0bfbf4 )
by Alexander
03:12
created

ApiSerializer::includedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Flugg\Responder\Serializers;
4
5
use League\Fractal\Pagination\PaginatorInterface;
6
use League\Fractal\Resource\ResourceInterface;
7
use League\Fractal\Serializer\ArraySerializer;
8
9
/**
10
 * Laravel Responder's own default implementation of Fractal's serializers.
11
 *
12
 * @package Laravel Responder
13
 * @author  Alexander Tømmerås <[email protected]>
14
 * @license The MIT License
15
 */
16
class ApiSerializer extends ArraySerializer
17
{
18
    /**
19
     * Serialize a collection.
20
     *
21
     * @param  string $resourceKey
22
     * @param  array  $data
23
     * @return array
24
     */
25
    public function collection( $resourceKey, array $data )
26
    {
27
        return $this->item( $resourceKey, $data );
0 ignored issues
show
Best Practice introduced by
The expression return $this->item($resourceKey, $data); seems to be an array, but some of its elements' types (boolean|null) are incompatible with the return type of the parent method League\Fractal\Serialize...ySerializer::collection 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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
28
    }
29
30
    /**
31
     * Serialize an item.
32
     *
33
     * @param  string $resourceKey
34
     * @param  array  $data
35
     * @return array
36
     */
37
    public function item( $resourceKey, array $data )
38
    {
39
        return array_merge( $this->null(), [
40
            'data' => $data
41
        ] );
42
    }
43
44
    /**
45
     * Serialize a null resource.
46
     *
47
     * @return array
48
     */
49
    public function null()
50
    {
51
        return [
52
            'success' => true,
53
            'data' => null
54
        ];
55
    }
56
57
    /**
58
     * Serialize the meta.
59
     *
60
     * @param  array $meta
61
     * @return array
62
     */
63
    public function meta( array $meta )
64
    {
65
        return $meta;
66
    }
67
68
    /**
69
     * Serialize the paginator.
70
     *
71
     * @param PaginatorInterface $paginator
72
     *
73
     * @return array
74
     */
75
    public function paginator( PaginatorInterface $paginator )
76
    {
77
        $pagination = parent::paginator( $paginator )[ 'pagination' ];
78
79
        $data = [
80
            'total' => $pagination[ 'total' ],
81
            'count' => $pagination[ 'count' ],
82
            'perPage' => $pagination[ 'per_page' ],
83
            'currentPage' => $pagination[ 'current_page' ],
84
            'totalPages' => $pagination[ 'total_pages' ],
85
        ];
86
87
        return [ 'pagination' => $data ];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('pagination' => $data); (array<string,array>) is incompatible with the return type of the parent method League\Fractal\Serialize...aySerializer::paginator of type array<string,array<string,integer|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...
88
    }
89
90
    /**
91
     * Indicates if includes should be side-loaded.
92
     *
93
     * @return bool
94
     */
95
    public function sideloadIncludes()
96
    {
97
        return true;
98
    }
99
100
    /**
101
     * Merges any relations into the data. The 'data' field is also removed.
102
     *
103
     * @param  array $transformedData
104
     * @param  array $includedData
105
     * @return array
106
     */
107
    public function mergeIncludes( $transformedData, $includedData )
108
    {
109
        $resourceKey = key( $includedData );
110
111
        if ( $resourceKey ) {
112
            $includedData[ $resourceKey ] = $includedData[ $resourceKey ][ 'data' ];
113
        }
114
115
        return array_merge( $transformedData, $includedData );
116
    }
117
118
    /**
119
     * Serialize the included data.
120
     *
121
     * @param ResourceInterface $resource
122
     * @param array             $data
123
     *
124
     * @return array
125
     */
126
    public function includedData( ResourceInterface $resource, array $data )
127
    {
128
        return [ ];
129
    }
130
}