Completed
Pull Request — master (#119)
by Toby
65:35
created

Relationship::jsonSerialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.576

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 10
cp 0.6
cc 3
eloc 9
nc 3
nop 0
crap 3.576
1
<?php
2
3
/*
4
 * This file is part of JSON-API.
5
 *
6
 * (c) Toby Zerner <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tobscure\JsonApi;
13
14
use JsonSerializable;
15
16
class Relationship implements JsonSerializable
17
{
18
    use LinksTrait;
19
    use SelfLinkTrait;
20
    use RelatedLinkTrait;
21
    use PaginationLinksTrait;
22
    use MetaTrait;
23
24
    /**
25
     * The data object.
26
     *
27
     * @var \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null
28
     */
29
    protected $data;
30
31 21
    /**
32
     * Create a new relationship.
33 21
     *
34 21
     * @param \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null $data
35
     */
36
    public function __construct($data = null)
37
    {
38
        $this->data = $data;
39
    }
40
41 12
    /**
42
     * Get the data object.
43 12
     *
44
     * @return \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null
45
     */
46
    public function getData()
47
    {
48
        return $this->data;
49
    }
50
51
    /**
52
     * Set the data object.
53
     *
54
     * @param \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null $data
55
     */
56
    public function setData($data)
57
    {
58
        $this->data = $data;
59
    }
60
61
    /**
62
     * Build the relationship as an array.
63
     *
64
     * @return array
65 12
     */
66
    public function jsonSerialize()
67 12
    {
68
        $relationship = [];
69 12
70 12
        if ($this->data) {
71 12
            $relationship['data'] = is_array($this->data)
72
                ? array_map([$this, 'buildIdentifier'], $this->data)
73 12
                : $this->buildIdentifier($this->data);
74
        }
75
76
        return array_filter($relationship + [
77 12
            'meta' => $this->meta,
78
            'links' => $this->links
79
        ]);
80
    }
81 12
82
    /**
83
     * Build an idenitfier array for the given resource.
84
     *
85
     * @param ResourceInterface $resource
86
     *
87
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
88
     */
89
    private function buildIdentifier(ResourceInterface $resource)
90
    {
91
        return [
92
            'type' => $resource->getType(),
93
            'id' => $resource->getId()
94
        ];
95
    }
96
}
97