Test Failed
Push — resource-links ( a0cf03...696503 )
by Alex
02:38
created

JsonApiSerializer::getIncluded()   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 0
1
<?php
2
3
namespace Huntie\JsonApi\Serializers;
4
5
use JsonSerializable;
6
7
abstract class JsonApiSerializer implements JsonSerializable
8
{
9
    /**
10
     * The JSON API version being implemented.
11
     *
12
     * @var string
13
     */
14
    const JSON_API_VERSION = '1.0';
15
16
    /**
17
     * Meta information to include.
18
     *
19
     * @var array
20
     */
21
    protected $meta = [];
22
23
    /**
24
     * Resource links to include.
25
     *
26
     * @var array
27
     */
28
    protected $links = [];
29
30
    /**
31
     * Return primary data for the JSON API document.
32
     *
33
     * @return mixed
34
     */
35
    abstract protected function getPrimaryData();
36
37
    /**
38
     * Return any secondary included resource objects.
39
     *
40
     * @return \Illuminate\Support\Collection
41
     */
42
    public function getIncluded()
43
    {
44
        return collect();
45
    }
46
47
    /**
48
     * Add included meta information.
49
     *
50
     * @param string|array    $key
51
     * @param string|int|null $value
52
     */
53
    public function addMeta($key, $value = null)
54
    {
55
        $this->meta = array_merge($this->meta, is_array($key) ? $key : [$key => $value]);
56
    }
57
58
    /**
59
     * Add one or more included links.
60
     *
61
     * @param string|array    $key
62
     * @param string|int|null $value
63
     */
64
    public function addLinks($key, $value = null)
65
    {
66
        $this->links = array_merge($this->links, is_array($key) ? $key : [$key => $value]);
67
    }
68
69
    /**
70
     * Serialise JSON API document to an array.
71
     *
72
     * @return array
73
     */
74
    public function serializeToObject()
75
    {
76
        return array_filter([
77
            'data' => $this->getPrimaryData(),
78
            'links' => $this->links,
79
            'meta' => $this->meta,
80
            'included' => array_filter($this->getIncludedData()),
81
            'jsonapi' => $this->getDocumentMeta(),
82
        ]);
83
    }
84
85
    /**
86
     * Convert the object into something JSON serializable.
87
     *
88
     * @return array
89
     */
90
    public function jsonSerialize()
91
    {
92
        return $this->serializeToObject();
93
    }
94
95
    /**
96
     * Serialise JSON API document to a JSON string.
97
     *
98
     * @return array
99
     */
100
    public function serializeToJson()
101
    {
102
        return json_encode($this->jsonSerialize());
103
    }
104
105
    /**
106
     * Return JSON API implementation information.
107
     *
108
     * @return array
109
     */
110
    private function getDocumentMeta()
111
    {
112
        return array_filter([
113
            'version' => config('jsonapi.include_version') ? self::JSON_API_VERSION : null,
114
        ]);
115
    }
116
}
117