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

Relationship   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 65%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 81
rs 10
c 3
b 2
f 0
ccs 13
cts 20
cp 0.65

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getData() 0 4 1
A setData() 0 4 1
A jsonSerialize() 0 15 3
A buildIdentifier() 0 7 1
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