Passed
Push — resource-links ( 4708d5 )
by Alex
03:00
created

JsonApiSerializer::getPrimaryData()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
nc 1
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
     * Add included meta information.
39
     *
40
     * @param string|array    $key
41
     * @param string|int|null $value
42
     */
43
    public function addMeta($key, $value = null)
44
    {
45
        $this->meta = array_merge($this->meta, is_array($key) ? $key : [$key => $value]);
46
    }
47
48
    /**
49
     * Add one or more included links.
50
     *
51
     * @param string|array    $key
52
     * @param string|int|null $value
53
     */
54
    public function addLinks($key, $value = null)
55
    {
56
        $this->links = array_merge($this->links, is_array($key) ? $key : [$key => $value]);
57
    }
58
59
    /**
60
     * Serialise JSON API document to an array.
61
     *
62
     * @return array
63
     */
64
    public function serializeToObject()
65
    {
66
        return array_filter([
67
            'data' => $this->getPrimaryData(),
68
            'links' => $this->links,
69
            'meta' => $this->meta,
70
            'included' => array_filter($this->getIncludedData()),
71
            'jsonapi' => $this->getDocumentMeta(),
72
        ]);
73
    }
74
75
    /**
76
     * Convert the object into something JSON serializable.
77
     *
78
     * @return array
79
     */
80
    public function jsonSerialize()
81
    {
82
        return $this->serializeToObject();
83
    }
84
85
    /**
86
     * Serialise JSON API document to a JSON string.
87
     *
88
     * @return array
89
     */
90
    public function serializeToJson()
91
    {
92
        return json_encode($this->jsonSerialize());
93
    }
94
95
    /**
96
     * Return any secondary included resource data.
97
     *
98
     * @return array
99
     */
100
    protected function getIncludedData()
101
    {
102
        return [];
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