Test Failed
Push — resource-links ( fc9d53...94266a )
by Alex
06:24
created

JsonApiSerializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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