Passed
Push — serializer ( f6b5ae...a67e7b )
by Alex
05:32
created

JsonApiSerializer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 2
cbo 1
dl 0
loc 153
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A scopeIncludes() 0 4 1
A addMeta() 0 4 2
A addLinks() 0 4 2
A toResourceObject() 0 6 1
A toResourceIdentifier() 0 7 1
A serialiseToObject() 0 9 4
A serializeToJson() 0 4 1
A getRecordType() 0 4 1
1
<?php
2
3
namespace Huntie\JsonApi\Serializers;
4
5
class JsonApiSerializer
6
{
7
    /**
8
     * The model instance to transform.
9
     *
10
     * @var \Illuminate\Database\Eloquent\Model
11
     */
12
    protected $record;
13
14
    /**
15
     * The subset of record attributes to return.
16
     *
17
     * @var array
18
     */
19
    protected $fields;
20
21
    /**
22
     * The record relations to include.
23
     *
24
     * @var array
25
     */
26
    protected $include;
27
28
    /**
29
     * Meta information to include.
30
     *
31
     * @var \Illuminate\Support\Collection
32
     */
33
    protected $meta;
34
35
    /**
36
     * Resource links to include.
37
     *
38
     * @var \Illuminate\Support\Collection
39
     */
40
    protected $links;
41
42
    /**
43
     * The loaded records to include.
44
     *
45
     * @var \Illuminate\Support\Collection
46
     */
47
    protected $included;
48
49
    /**
50
     * Create a new JSON API resource serializer.
51
     *
52
     * @param Model      $record  The model instance to serialise
53
     * @param array|null $fields  Subset of fields to return
54
     * @param array|null $include Relations to include
55
     */
56
    public function __construct($record, array $fields = [], array $include = [])
57
    {
58
        $this->record = $record;
0 ignored issues
show
Documentation Bug introduced by
It seems like $record of type object<Huntie\JsonApi\Serializers\Model> is incompatible with the declared type object<Illuminate\Database\Eloquent\Model> of property $record.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
        $this->fields = array_unique($fields);
60
        $this->include = array_unique(array_merge($record->getRelations(), $include));
61
        $this->meta = collect([]);
62
        $this->links = collect([]);
63
        $this->included = collect([]);
64
    }
65
66
    /**
67
     * Limit which relations can be included.
68
     *
69
     * @param array $include
70
     */
71
    public function scopeIncludes($include)
72
    {
73
        $this->include->intersect($include);
0 ignored issues
show
Bug introduced by
The method intersect cannot be called on $this->include (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
74
    }
75
76
    /**
77
     * Add meta information to the returned object.
78
     *
79
     * @param string|array    $key
80
     * @param string|int|null $value
81
     */
82
    public function addMeta($key, $value = null)
83
    {
84
        $this->meta->merge(is_array($key) ? $key : [$key => $value]);
85
    }
86
87
    /**
88
     * Add one or more links to the returned object.
89
     *
90
     * @param string|array    $key
91
     * @param string|int|null $value
92
     */
93
    public function addLinks($key, $value = null)
94
    {
95
        $this->links->merge(is_array($key) ? $key : [$key => $value]);
96
    }
97
98
    /**
99
     * Return a JSON API resource object for the primary record.
100
     *
101
     * @return array
102
     */
103
    public function toResourceObject()
104
    {
105
        return array_merge($this->toResourceIdentifier(), [
106
            // TODO
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
107
        ]);
108
    }
109
110
    /**
111
     * Return a JSON API resource identifier object for the primary record.
112
     *
113
     * @return array
114
     */
115
    public function toResourceIdentifier()
116
    {
117
        return [
118
            'type' => $this->getRecordType(),
119
            'id' => $this->record->id,
120
        ];
121
    }
122
123
    /**
124
     * Serialise complete JSON API document to an array.
125
     *
126
     * @return array
127
     */
128
    public function serialiseToObject()
129
    {
130
        return array_filter([
131
            'data' => $this->toResourceObject(),
132
            'included' => $this->included->isEmpty() ? null : $this->included->toArray(),
133
            'links' => $this->links->isEmpty() ? null : $this->links->toArray(),
134
            'meta' => $this->meta->isEmpty() ? null : $this->meta->toArray(),
135
        ]);
136
    }
137
138
    /**
139
     * Serialise complete JSON API document to a JSON string.
140
     *
141
     * @return array
142
     */
143
    public function serializeToJson()
144
    {
145
        return json_encode($this->serialiseToObject());
146
    }
147
148
    /**
149
     * Return the primary record type name.
150
     *
151
     * @return string
152
     */
153
    protected function getRecordType()
154
    {
155
        return str_slug(str_plural(get_class($this->record)));
156
    }
157
}
158