Completed
Pull Request — master (#119)
by Toby
64:53
created

Relationship::toArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.5923

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 18
ccs 8
cts 12
cp 0.6667
rs 9.2
c 1
b 1
f 0
cc 4
eloc 9
nc 8
nop 0
crap 4.5923

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Relationship::getData() 0 4 1
A Relationship::setData() 0 4 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
    private function __construct()
32
    {
33 21
    }
34 21
35
    public static function fromMeta($meta)
36
    {
37
        $r = new self;
38
        $r->replaceMeta($meta);
39
        return $r;
40
    }
41 12
42
    public static function fromSelfLink($link)
43 12
    {
44
        $r = new self;
45
        $r->setSelfLink($link);
46
        return $r;
47
    }
48
49
    public static function fromRelatedLink($link)
50
    {
51
        $r = new self;
52
        $r->setRelatedLink($link);
53
        return $r;
54
    }
55
56
    public static function fromData($data)
57
    {
58
        $r = new self;
59
        $r->data = $data;
60
        return $r;
61
    }
62
63
    /**
64
     * Get the data object.
65 12
     *
66
     * @return \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null
67 12
     */
68
    public function getData()
69 12
    {
70 12
        return $this->data;
71 12
    }
72
73 12
    /**
74
     * Set the data object.
75
     *
76
     * @param \Tobscure\JsonApi\ResourceInterface|\Tobscure\JsonApi\ResourceInterface[]|null $data
77 12
     */
78
    public function setData($data)
79
    {
80
        $this->data = $data;
81 12
    }
82
83
    /**
84
     * Build the relationship as an array.
85
     *
86
     * @return array
87
     */
88
    public function jsonSerialize()
89
    {
90
        $relationship = [];
91
92
        if ($this->data) {
93
            $relationship['data'] = is_array($this->data)
94
                ? array_map([$this, 'buildIdentifier'], $this->data)
95
                : $this->buildIdentifier($this->data);
96
        }
97
98
        return array_filter($relationship + [
99
            'meta' => $this->meta,
100
            'links' => $this->links
101
        ]);
102
    }
103
104
    /**
105
     * Build an idenitfier array for the given resource.
106
     *
107
     * @param ResourceInterface $resource
108
     *
109
     * @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...
110
     */
111
    private function buildIdentifier(ResourceInterface $resource)
112
    {
113
        return [
114
            'type' => $resource->getType(),
115
            'id' => $resource->getId()
116
        ];
117
    }
118
}
119